C. Two Movies
think: 如果一个人对两部电影看法不同,那显然应该去评价他觉得更好的电影.对于每个人我们发现,如果他对两部电影的评分不同的话.肯定要把评分高的那个算到贡献里面.如果评分相同的话,我们先记录下(add表示某个人对两部电影都给出1的评分,sub表示某个人对两部电影都给出-1的评分,0就不用记录了).然后最后把add和sub分给两部电影,使得两部电影的评分尽可能接近.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define int long long
const int N = 1e6 + 10;
typedef pair<int, int> pii;
int n, a[N], b[N];
void solve()
{
cin >> n;
int ans1 = 0, ans2 = 0, add = 0, sub = 0;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ ) cin >> b[i];
for (int i = 1; i <= n; i ++ ) {
if (a[i] == b[i]) {
if (a[i] == 1) add ++;
else if (a[i] == -1) sub ++;
} else if (a[i] > b[i]) ans1 += a[i];
else ans2 += b[i];
}
while (add -- ) {
if (ans1 <= ans2) ans1 ++;
else ans2 ++;
}
while (sub -- ) {
if (ans1 <= ans2) ans2 --;
else ans1 --;
}
cout << min(ans1, ans2) << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int _ = 1;
cin >> _;
while (_ -- )
{
solve();
}
return 0;
}
D. Smithing Skill
think:由于本题a[i]一定大于b[i]所以我们应该锻造武器之后再把它融化然后对答案的贡献是2.首先就是按照(a[i]-b[i], a[i])存到w中然后进行排序,对于其中w[i].fi== w[i-1].fi && w[i].se>=w[i-1].se的应该删除(因为这些操作是没意义的).剩下就是线性dp了.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define int long long
const int N = 1e6 + 10;
typedef pair<int, int> pii;
int n, m, a[N], b[N];
pii w[N];
vector<pii> alls;
int find(int x) {
int l = 0, r = alls.size() - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (alls[mid].se <= x) l = mid;
else r = mid - 1;
}
return r;
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ ) {
cin >> b[i];
w[i] = {a[i] - b[i], a[i]};
}
int pre = 1e9;
sort (w + 1, w + n + 1);
for (int i = 1; i <= n; i ++ ) {
if (w[i].se < pre) {
alls.push_back({w[i].fi, w[i].se});
pre = w[i].se;
}
}
alls.push_back({0, 0});
reverse (alls.begin(), alls.end());
vector<int> dp(1e6 + 10); dp[0] = 0;
for (int i = 1; i <= 1e6; i ++ ) {
int t = find(i);
if (! t) dp[i] = 0;
else dp[i] = dp[i - alls[t].fi] + 1;
}
int ans = 0;
while (m -- ) {
int x;
cin >> x;
int t = find(x);
if (t) {
int tt = (x - alls[t].se) / alls[t].fi + 1;
ans += dp[x - tt * alls[t].fi] + tt;
}
}
cout << ans * 2 << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int _ = 1;
// cin >> _;
while (_ -- )
{
solve();
}
return 0;
}