题目 | Freedom of Choice
Codeforces Round 908 (Div. 2)
E. Freedom of Choice
https://codeforces.com/contest/1894/problem/E
3 seconds / 512 megabytes
standard input / standard output
Problem
Let's define the anti-beauty of a multiset
You are given
Let's create a multiset
- Choose some
such that - Choose any
numbers from the -th multiset and add them to the multiset .
You need to choose
Input
Each test consists of multiple test cases. The first line contains a single integer
The first line of each test case contains a single integer
Then, for each
The first line of each block contains three integers
The second line of the block contains
The third line of the block contains
It is guaranteed that the sum of the values of
Output
For each test case, output the minimum possible anti-beauty of the multiset
Example
Input
7
3
3 5 6
10 11 12
3 3 1
1 1 3
12
4
2 4 4
12 13
1 5
1
7 1000 1006
1000 1001 1002 1003 1004 1005 1006
147 145 143 143 143 143 142
1
2 48 50
48 50
25 25
2
1 1 1
1
1
1 1 1
2
1
1
1 1 1
1
2
2
1 1 1
1
1
2 1 1
1 2
1 1
2
4 8 10
11 12 13 14
3 3 3 3
2 3 4
11 12
2 2
Output
1
139
0
1
1
0
0
Note
In the first test case, the multisets have the following form:
. From this multiset, you need to select between and numbers. . From this multiset, you need to select between and numbers. . From this multiset, you need to select numbers.
You can select the elements
题解
首先,若第
而我们在所有集合中,按照限制条件能够选择的元素数量范围为
那么容易得到,如果
因此,我们首先特判
这个特判的最大意义是缩小数据范围,剩余的情况是
接下来,问题就转换为,若规定选择
比较容易想到的是遍历
但是这种情况,对于每个选择数目
但是可以转换一下思路,我们将包含有数
代码
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
void solve()
{
int m;
cin >> m;
vector<int> n(m), l(m), r(m), s(m);
vector<vector<int>> a(m), c(m);
int sl = 0, sr = 0, sn = 0;
for (int i = 0; i < m; i++)
{
cin >> n[i] >> l[i] >> r[i];
sn += n[i];
sl += l[i];
sr += r[i];
a[i].resize(n[i]);
c[i].resize(n[i]);
for (int j = 0; j < n[i]; j++)
cin >> a[i][j];
for (int j = 0; j < n[i]; j++)
cin >> c[i][j];
s[i] = accumulate(c[i].begin(), c[i].end(), 0LL);
}
if (sr - sl + 1 > sn)
{
cout << 0 << endl;
return;
}
map<int, int> sum;
map<int, vector<pair<int, int>>> idx;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n[i]; j++)
{
sum[a[i][j]] += r[i];
idx[a[i][j]].push_back({i, j});
}
}
int ans = INT64_MAX;
for (int i = sl; i <= sr; i++)
{
int need = 0, size = sr - sum[i];
for (auto &[p, q] : idx[i])
{
int t = s[p] - c[p][q];
if (t < l[p])
{
need += l[p] - t;
size += l[p];
}
else
{
size += min(t, r[p]);
}
}
ans = min(ans, need + max(i - size, 0LL));
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi