ZOJ 3981(Balloon Robot)

题目链接

题目意思:n支队伍,一张桌子有m个座位顺时针编号1~m。p个预言。预言a队伍在b时刻ac一道题目。机器人每个时刻都顺时针走一步,有需要气球的就发。每支队伍,ac后多少时刻没收到气球就有多少不开心的值,问机器人起始位置在哪里可以使得总的不高兴值最小。输出最小的不高兴值。

思路:把每次预言转一下,a,b。那就让a队伍位置往前推b的位置值加一。那么不高兴值就是机器人花费时间乘以值求和。然后换起始点可以O(1)转。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <map>

using namespace std;
typedef long long int LL;


const int maxn=1e6+10;
int n,m,p;
map<int,int>val;
int pos[maxn];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&p);
        val.clear();
        for(int i=1;i<=n;i++) scanf("%d",&pos[i]);
        for(int i=1;i<=p;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            LL p=((pos[x]%m)*1ll-y+(1000000000ll/m+1)*m)%m;
            val[(int)(p)]++;
        }
        LL sum=0,base=0,ans;
        int st=val.begin()->first;;
        for(map<int,int>::iterator i=val.begin();i!=val.end();i++)
        {
            sum+= i->second;
            base+=(1ll*i->second*(i->first - st));
        }
        ans=base;
        int prekey;
        map<int,int>::iterator it=val.begin();
        prekey=it->first;
        int preval=it->second;
        it++;
        for(;it!=val.end();it++)
        {
            int dt=it->first-prekey;
            base=base-((sum-preval)*dt)+(m-dt)*preval;
            ans=min(base,ans);
            prekey=it->first;
            preval=it->second;
        }
        cout<<ans<<endl;
    }
    return 0;
}


posted @ 2017-10-31 17:41  Code-dream  阅读(658)  评论(0编辑  收藏  举报