icpc:problems:usaco22open_counting_liars_b
problems | |
---|---|
名称 | Counting Liars B |
题目编号 | USACO22OPEN_B2 |
题目链接 | luogu.com.cn/… |
来源 | USACO |
算法分类 | 排序, 双指针 |
难易程度 | 容易 |
Counting Liars B
想法
不同的L与G会划分出不同的区间,题目的解法是枚举区间,算说谎最小值。对于L,说谎的人会在左边;对于G,说谎的人会在右边。
先将L与G排序,然后均从头开始取,会面临2种情况:
- L[i] < G[j] :无论数怎么取,$L[i] <= x < G[j]$ 或 $L[i] < x <= G[j]$,都会产生$i + glen - j$个说谎的人。之后将i往后移动。
- L[i] >= G[j] :$L[i] <= x <= G[j]$,会产生 $i + glen - j - 1$ 个说谎的人。之后将j往后移动。
代码实现
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int n, p; char method; cin >> n; vector<int>L, G; for (int i = 0; i < n; ++i) { cin >> method >> p; if(method == 'L') L.push_back(p); else G.push_back(p); } sort(L.begin(), L.end()); sort(G.begin(), G.end()); int i = 0, j = 0, l_len = L.size(), g_len = G.size(); int ans = min(l_len, g_len); while(i < l_len && j < g_len) { if(L[i] < G[j]) { ans = min(ans, i + g_len - j); i++; } else { ans = min(ans, i + g_len - j - 1); j++; } } cout << ans; return 0; }
/app/www/public/data/pages/icpc/problems/usaco22open_counting_liars_b.txt · 最后更改: 2023/02/09 08:00 由 温婕莺