2017ccpc秦皇岛

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int seat[N];
typedef long long LL;
LL idt[N];//  第i次相对参照物品1的不开心值
int main()
{
    int t;scanf("%d",&t);
    while(t--){
        int n,m,q;
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=n;i++) scanf("%d",&seat[i]);
        //暂时从0出发(1时刻在1位置),把作为一个参照物
        LL sum=0;
        for(int i=1;i<=q;i++){
            LL a,b;//位置和出题时间
            scanf("%lld%lld",&a,&b);
            idt[i] = ((seat[a]-1-b+m)%m+m)%m;
            sum+=idt[i];
        }
        sort(idt+1,idt+q+1);
        LL ans=sum;
        //cout<<ans<<endl;
        for(int i=1;i<=q;i++) //以消灭第i个不开心值的出题位置为起点-1
        {
            ans=min(ans,sum-(q-i+1)*idt[i]+(i-1)*(m-idt[i]));
            //ans=min(ans,sum+m*(i-1)-idt[i]*q);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

先贴个代码,说实话,网上那些博客错的递推式能推出正确结论,我也是服了
s u m ( q i + 1 ) i d t [ i ] + ( i 1 ) ( m i d t [ i ] ) sum-(q-i+1)*idt[i]+(i-1)*(m-idt[i])

猜你喜欢

转载自blog.csdn.net/wyxxzsy/article/details/83592230
今日推荐