题解:P8267 [USACO22OPEN] Counting Liars B & U208878 晴天
![题解:P8267 [USACO22OPEN] Counting Liars B & U208878 晴天](/_astro/P8267.Cq3ghUUl_Zj7y2r.webp)
828 字
4 分钟
题解:P8267 [USACO22OPEN] Counting Liars B & U208878 晴天
其实,这个题,只需要最简单的枚举,加上最简单的二分查找即可~
?枚举吧~
咋枚举?显然,最好状态下 Bessie 的位置一定是某个 ,否则差一个就会导致有个奶牛要说谎。所以我们枚举(理论来讲要先去个重,这样快一点,不过貌似数据没有重的~),每次遍历这帮奶牛看看有多少不说实话的不就得了?时间复杂度显然 。
AC Code:
// 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 Block_Fish*/#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;}加强版把 上调至 ,显然,刚才的方法挂了。实测 确实不行。咋办?
注意看标签得到 二分。外面显然没法优化,里边可以不?
必须滴!
你查找‘L’中说谎的,不就是在查小于这玩意儿的吗?你查找‘G’当中说谎的,不就是再查大于这玩意儿的吗?小于的直接 lower_bound,大于的就相当于总数 - 小于等于的,也就是总数 - upper_bound。
时间复杂度显然为 ,可以过。
完。
AC Code:
// 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 Block_Fish*/#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;}支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
题解:P8267 [USACO22OPEN] Counting Liars B & U208878 晴天
https://www.leo2011.eu.org/posts/luogu-p8267/相关文章智能推荐
1
题解:P9938 [USACO21OPEN] Acowdemia II B
题解讲解了不建图解决 USACO 2021 公开赛的铜组题目 Acowdemia II。
2
P10187 [USACO24FEB] Palindrome Game B 题解
题解讲解了 USACO 2024 二月场的铜组题目 Palindrome Game。
3
P9012 [USACO23JAN] Moo Operations B题解
题解讲解了 USACO 2023 一月场的铜组题目 Moo Operations。
4
P10189 [USACO24FEB] Maximizing Productivity B 题解
讲解了二分解决 USACO 2024 二月场的铜组题目 Maximizing Productivity。
5
题解:P16007 [CCO 2016 Day 1] Field Trip 郊游
题解讲解了如何解决洛谷 P16007。
随机文章随机推荐













