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 @   Code-dream  阅读(676)  评论(0编辑  收藏  举报
编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· C# 13 中的新增功能实操
· Vue3封装支持Base64导出的电子签名组件
· 万字长文详解Text-to-SQL
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
点击右上角即可分享
微信分享提示