ZOJ - 3981 Balloon Robot——思维

版权声明:欢迎大家转载,转载请注明出处 https://blog.csdn.net/hao_zong_yin/article/details/83154775

先选1作为起始点跑一遍不高兴值,按不高兴值从小到大排序,然后顺序扫描,当要把第i个位置变为0时,实际上就是将i前面的元素+m,然后整体-unhappy[i]

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
int t, n, q;
ll m, s[maxn], a[maxn], b[maxn];
ll unhappy[maxn];
int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%d%lld%d", &n, &m, &q);
        for (int i = 1; i <= n; i++) scanf("%lld", &s[i]);
        for (int i = 1; i <= q; i++) scanf("%lld%lld", &a[i], &b[i]);
        for (int i = 1; i <= q; i++) {
            ll x = (s[a[i]]-2+m)%m+1;
            if (x >= b[i]) {
                unhappy[i] = x - b[i];
            }
            else {
                ll k = ceil((1.0*b[i]-x)/m);
                unhappy[i] = k*m+ x - b[i];
            }
        }
        sort(unhappy+1, unhappy+1+q);
        ll sum = 0;
        for (int i = 1; i <= q; i++) sum += unhappy[i];
        ll ans = sum;
        for (int i = 1; i <= q; i++) {
            ans = min(ans, sum+(i-1)*m-q*unhappy[i]);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hao_zong_yin/article/details/83154775