Niu Niu and the competition awards

Insert picture description here
Insert picture description here
Input:
10 10
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10
10 10

Output:
1 2 2
Insert picture description here
Idea: Core: Difference prefix sum.
Start to see the interval, difference a wave, and then sum the prefixes, look at the number of questions in ac one by one, and then allocate them according to the number of gold and silver medals.
Here we need to perform a discrete operation on the interval. If there is an interval [1,10000] to find the prefix sum, it needs to be added from 1 to 10000, because in the end, there is no need to find the number of ac questions for a specific team. In fact, it is enough to count the length of the interval. Number of intervals, statistical interval length, optimization algorithm.

//离散+差分+前缀和

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[100010]; //做出i个题的人数
struct ty
{
    
    
 int pos,num; //存区间下标和是加1还是减1
}a[200010];

bool cmp(ty a, ty b) //将点按照从左往右,先减后加排序
{
    
    
    if( a.pos!= b.pos)  return a.pos< b.pos;
    else return a.num<b.num;
}
int main()
{
    
    

    int n,m;
    cin>>n>>m;

    for(int i=1 ;i<=m ;i++)
    {
    
    
        int l,r;//离散化
        cin>>l>>r;
        a[i].num=1; //左端点
        a[i].pos=l;
        a[i+m].num=-1; //右端点
        a[i+m].pos=r+1; //差分,往后一位
    }
    //将2m个点排序
    sort(a+1,a+1+m+m,cmp);
    
    int sum=0;  //记录目前ac的题数
    int maxn=1; //ac最多题的人数
    for(int i=1 ;i<=m+m; i++)
    {
    
    
        //看区间两端
        if( a[i].pos -a[i-1].pos!=0 )
        cnt[sum]+=a[i].pos-a[i-1].pos; //记录ac sum道题的人数

        sum+=a[i].num;
        maxn=max(maxn,sum); //更新最大
    }

    int j=-1 ,y=-1 ,t=-1;
    for(int i=maxn ;i>=0 ;i--)
    {
    
    
        cnt[i]+=cnt[i+1];  //前缀和,大于等于i题的人数
        if( j==-1 && (cnt[i] >= (n+9)/10) )  j=i;
        if( y==-1 && (cnt[i] >= (n+3)/4) )  y=i;
        if( t==-1 && (cnt[i] >= (n+1)/2) )  t=i;
    }
    //因为获得奖牌的必要条件是ac一题
    j=max(j,1);
    y=max(y,1);
    t=max(t,1);

    cout<<cnt[j]<<" "<<cnt[y]-cnt[j]<<" "<<cnt[t]-cnt[y];
}

Guess you like

Origin blog.csdn.net/m0_53688600/article/details/113870859
Recommended