题解:P8267 [USACO22OPEN] Counting Liars B & U208878 晴天

Leo2011 大气压强

其实,这个题,只需要最简单的枚举,加上最简单的二分查找即可~


?枚举吧~

咋枚举?显然,最好状态下 Bessie 的位置一定是某个 ,否则差一个就会导致有个奶牛要说谎。所以我们枚举(理论来讲要先去个重,这样快一点,不过貌似数据没有重的~),每次遍历这帮奶牛看看有多少不说实话的不就得了?时间复杂度显然

ACCode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Problem: P8267 [USACO22OPEN] Counting Liars B
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P8267
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

/*Code by Leo2011*/
#include <bits/stdc++.h>

#define log printf
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define FOR(i, l, r) for (int(i) = (l); (i) <= (r); ++(i))
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);

using namespace std;

typedef __int128 i128;
typedef long long ll;
typedef pair<int, int> PII;

const int N = 1010;
int n, t[N], ans = INF;
vector<int> pls;
char op[N];

template <typename T>

inline T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
return sum * fl;
}

template <typename T>

inline void write(T x) {
if (x < 0) {
putchar('-'), write<T>(-x);
return;
}
static T sta[35];
int top = 0;
do { sta[top++] = x % 10, x /= 10; } while (x);
while (top) putchar(sta[--top] + 48);
}

int main() {
IOS cin >> n;
FOR(i, 1, n) {
cin >> op[i] >> t[i];
pls.push_back(t[i]); // 数组 p
}
unique(pls.begin(), pls.end()); // 去重
for (auto i : pls) {
int tmp = 0;
FOR(j, 1, n) if ((op[j] == 'L' && i > t[j]) || (op[j] == 'G' && i < t[j]))++ tmp; // 满足条件就过
ans = min(ans, tmp);
}
write<int>(ans);
return 0;
}

AC 记录~


本来该消停了,直到我在讨论区里 发现了个加强版

加强版把 上调至 ,显然,刚才的方法挂了。实测 确实不行。咋办?

注意看标签得到二分。外面显然没法优化,里边可以不?

必须滴!

你查找‘L’中说谎的,不就是在查小于这玩意儿的吗?你查找‘G’当中说谎的,不就是再查大于这玩意儿的吗?小于的直接 lower_bound,大于的就相当于总数 - 小于等于的,也就是总数 - upper_bound

时间复杂度显然为 ,可以过。

完。


ACCode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Problem: U208878 晴天
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/U208878
// Memory Limit: 128 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

/*Code by Leo2011*/
#include <bits/stdc++.h>

#define log printf
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define FOR(i, l, r) for (int(i) = (l); (i) <= (r); ++(i))
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);

using namespace std;

typedef __int128 i128;
typedef long long ll;
typedef pair<int, int> PII;

const int N = 2e5 + 10;
int n, t, ans = INF;
vector<int> pls, lhs, rhs;
char op;

template <typename T>

inline T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
return sum * fl;
}

template <typename T>

inline void write(T x) {
if (x < 0) {
putchar('-'), write<T>(-x);
return;
}
static T sta[35];
int top = 0;
do { sta[top++] = x % 10, x /= 10; } while (x);
while (top) putchar(sta[--top] + 48);
}

int main() {
IOS cin >> n;
FOR(i, 1, n) {
cin >> op >> t;
pls.push_back(t);
if (op == 'L') lhs.push_back(t);
else rhs.push_back(t);
}
sort(pls.begin(), pls.end());
sort(lhs.begin(), lhs.end());
sort(rhs.begin(), rhs.end());
for (auto i : pls) {
int tmpg = rhs.size() - (upper_bound(rhs.begin(), rhs.end(), i) - rhs.begin()), tmpl = lower_bound(lhs.begin(), lhs.end(), i) - lhs.begin();
cerr << i << ' ' << tmpg << ' ' << tmpl << endl;
ans = min(ans, tmpg + tmpl);
}
write<int>(ans);
return 0;
}

AC 记录(加强版)~

  • 标题: 题解:P8267 [USACO22OPEN] Counting Liars B & U208878 晴天
  • 作者: Leo2011
  • 创建于 : 2024-05-25 21:50:30
  • 更新于 : 2024-08-29 14:09:12
  • 链接: https://leo2011.eu.org/2024/05/25/ti-jie-p8267-usaco22open-counting-liars-b-u208878-qing-tian/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
题解:P8267 [USACO22OPEN] Counting Liars B & U208878 晴天