• Gym 101102C---Bored Judge(区间最大值)


    题目链接

    http://codeforces.com/gym/101102/problem/C

    problem description

    Judge Bahosain was bored at ACM AmrahCPC 2016 as the winner of the contest had the first rank from the second hour until the end of the contest.

    Bahosain is studying the results of the past contests to improve the problem sets he writes and make sure this won’t happen again.

    Bahosain will provide you with the log file of each contest, your task is to find the first moment after which the winner of the contest doesn’t change.

    The winner of the contest is the team with the highest points. If there’s more than one team with the same points, then the winner is the team with smallest team ID number.

    Input

    The first line of input contains a single integer T, the number of test cases.

    The first line of each test case contains two space-separated integers N and Q (1 ≤ N, Q ≤ 105), the number of teams and the number of events in the log file. Teams are numbered from 1 to N.

    Each of the following Q lines represents an event in the form: X P, which means team number X (1 ≤ X ≤ N) got P( - 100 ≤ P ≤ 100, P ≠ 0) points. Note that P can be negative, in this case it represents an unsuccessful hacking attempt.

    Log events are given in the chronological order.

    Initially, the score of each team is zero.

    Output

    For each test case, if the winner of the contest never changes during the contest, print 0. Otherwise, print the number of the first event after which the winner of the contest didn’t change. Log events are numbered from 1 to Q in the given order.

    Example
    input
    1
    5 7
    4 5
    3 4
    2 1
    1 10
    4 8
    3 -5
    4 2
    output
    5

    题意:有n个人参加活动,现在有Q次事件,标号从1~Q,每个事件为x p 表示第x个人加上p分(-100=<p<=100&&p!=0) 求到第几个事件之后冠军不再变化,冠军为得分最多的那个人,如果多个人得分相同,冠军为序号最小的那个人。

    思路:先遍历一遍事件,找到冠军tmp,然后再从第一个事件开始遍历,判断当前的冠军是否是tmp,如果不是则ans=i+1 第二次遍历时就是修改a[x[i]]的值,然后判断最大是是否还是tmp,故可以用RMQ或平衡二叉树(set集合也是平衡二叉树,需要自定义排序);

    代码如下:
    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    #include <string.h>
    #include <set>
    const int MAXN = 1e5+10;
    using namespace std;
    const int INF = 1e9;
    int a[MAXN], x[MAXN], p[MAXN];
    
    struct compare
    {
        bool operator() (const int s1, const int s2) const
        {
            if(a[s1]==a[s2]) return s1<s2;
            return a[s1]>a[s2];
        }
    };
    set<int,compare>s;
    set<int,compare>:: iterator it;
    
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            s.clear();
            int n, q;
            memset(a, 0, sizeof(a));
            scanf("%d%d",&n,&q);
            for(int i=1; i<=q; i++)
            {
                scanf("%d%d",&x[i],&p[i]);
                a[x[i]] += p[i];
            }
            int Max = -INF, tmp = -1;
            for(int i=1; i<=n; i++)
            {
                if(a[i] > Max)
                {
                    Max = a[i];
                    tmp = i;
                }
            }
            memset(a, 0, sizeof(a));
            for(int i=1;i<=n;i++)
                s.insert(i);
            //cout<<"++: "<<*s.begin()<<endl;
            int pos = 0;
            if(*s.begin()!=tmp) pos=1;
            for(int i=1; i<=q; i++)
            {
                s.erase(x[i]);
                a[x[i]] += p[i];
                s.insert(x[i]);
                if(*s.begin()!=tmp) pos=i+1;
            }
            printf("%d
    ",pos);
        }
        return 0;
    }
     
  • 相关阅读:
    EL表达式 (详解)
    宜信面试整理
    Java 合并两个排序数组
    动态规划初识(爬楼梯问题)
    二叉树的最小深度
    ElasticSearch 单字符串多字段查询评分问题
    ES 分词
    汽车之家 面试总结
    浪潮之巅读书笔记
    闲徕互娱 面试总结
  • 原文地址:https://www.cnblogs.com/chen9510/p/5923054.html
Copyright © 2020-2023  润新知