Gym - 101102C

题目链接

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int data[maxn],last[maxn],ans;
struct D {
    int id;
    int sc;
    int time;
    D(int id0=0,int sc0=0,int time0=0){
        id=id0;
        sc=sc0;
        time=time0;
    }
};
bool operator < (D a,D b) {
    if(a.sc!=b.sc) return a.sc<b.sc;
    if(a.id!=b.id) return a.id>b.id;
    return a.time<b.time;
}
int main()
{
    int t,n,q,x,p;
    scanf("%d",&t);
    while(t--)
    {
        priority_queue<D> que;
        int ansid=0,anssum=0,ans=0;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++) que.push(D(i,0,0));
        //printf("%d\n",que.top().id);
        memset(data,0,sizeof(data));
        memset(last,0,sizeof(last));
        ans=1;
        int ansT=0;
        for(int i=1;i<=q;i++)
        {
            scanf("%d%d",&x,&p);
            data[x]+=p;
            last[x]=i;
            while(!que.empty() && que.top().time != last[que.top().id]) que.pop();
            if(x==ans&&p<0) {
                //printf("%d %d---\n",ans,que.top().id);
                //D tmp = que.top();
                //que.pop();
                while(!que.empty() && que.top().time != last[que.top().id]) que.pop();
                //printf("%d %d---\n",ans,que.top().id);
                if(!que.empty()&&(data[ans]<que.top().sc||(data[ans]==que.top().sc && que.top().id<ans))) {
                    ansT = i;
                    ans = que.top().id;
                }
                //que.push(tmp);
            }
            else if(p>=0&&x!=ans&&(data[x]>data[ans]||(data[x]==data[ans]&&x<ans))) {
                ans = x;
                ansT = i;
            }
            que.push(D(x,data[x],i));
        }
        printf("%d\n",ansT);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Ritchie/p/6209350.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值