当前位置:网站首页>K gold Chef (two conditions, two points and difference)

K gold Chef (two conditions, two points and difference)

2022-06-26 14:23:00 Honestbutter-

link
Two points + Difference
Two point maximum ,check The solution satisfying two conditions is obtained .( What we used to do was to meet one condition , Pay attention to the difference )
Topic purpose : Make the common interval as long as possible , And the number of common intervals should be as many as possible .
We get two points each time mid, Refers to the number of people , Also refers to length .
And then the first one for The length of the circular record shall meet ≥mid Prefix and number of people , the second for The cyclic difference is consistent with ≥mid The length of the number , If the number of people sum[i]≥mid,return true,( Two conditions are met ).

#include<iostream>
#include<cstring>
using namespace std;
const int N=3e5+1000;
typedef pair<int,int> PII;
PII a[N];
int n,m,sum[N];
// Two point maximization feasible answer , For every time mid, Differential maintenance determines whether there is a continuous length not less than mid And the number of people is not less than mid The range of 
bool check(int mid)    // The number of ≥mid, length ≥mid
{
    
    memset(sum,0,sizeof sum);
    for(int i=0;i<m;i++)
    {
    
        int len=a[i].second-a[i].first+1;
        if(len>=mid)	// The first condition 
        {
    
            sum[a[i].first+mid-1]++;
            sum[a[i].second+1]--;
        }
    }
    
    for(int i=1;i<=n;i++)
    {
    
        sum[i]+=sum[i-1];
        if(sum[i]>=mid) 	// The second condition 
        	return true;
    }
    return false;
}
int main()
{
    
    cin>>n>>m;
    for(int i=0;i<m;i++) cin>>a[i].first>>a[i].second;
    
    int l=0,r=m-1;
    while(l<r)
    {
    
        int mid=(l+r+1)/2;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    
    cout<<l<<endl;
    return 0;
}
原网站

版权声明
本文为[Honestbutter-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170508103672.html