ZOJ 3981(Balloon Robot)

本文介绍了一种通过模拟和优化策略来最小化比赛中各队伍不高兴值的方法。具体实现上,利用了C++进行编程,并使用了数据结构和算法解决了一个机器人在不同位置发放气球的问题,以降低因延迟发放气球导致的不满情绪。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

题目意思: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;
}

转载于:https://www.cnblogs.com/coded-ream/p/7762718.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值