upc test discretization + difference

Exam
time limit: 1 Sec memory limit: 128 MB

Title description
S conducted provincial selection training, and the strength of n players was uneven. As we all know, if the title is too water, then someone will play the game after the AK leaves, if the title is too difficult, then someone will play the game after decadent departure. As the person who asked the question, you naturally do n’t want too many people to go out to play games, otherwise ob will be very angry. So you need to set the difficulty of the topic, try to let the least people go out to play the game, and at the same time the topic is as difficult as possible.
Enter
a positive integer n in the first line, indicating that a total of n players will participate; in the
next n lines, two non-negative integers Ai, Bi indicate that the acceptable difficulty range of the i-th player is between Ai ~ Bi, if you If the difficulty of the set question exceeds this range, the player will leave the game.

The output is a
line with a number ans, indicating that the difficulty you set is ans, which should be made as large as possible while leaving the least people out of the field.
Sample input Copy
3
1 5
95 105
5 110
Sample output Copy
105
prompt The
sample explains that
there are three players participating. For convenience, the first player is named X, the second player is named Q, and the third player is named P.
If the difficulty of the question is to satisfy Konjac X, then Q will definitely be decadent after AK. In order to take into account the small P, the difficulty has to be set to 5. At this time, 1 person leaves the field.
If you want to meet the big guy Xiao Q, then anyway, Xiao X will feel that the problem is too difficult to leave the court. In order to take care of the small P, the maximum difficulty can be set to 105, at this time 1 person leaves the court.
In summary, the maximum difficulty of the question is 105.
[Data range]
For 30% data: n ≤ 10
For 50% data: n ≤ 1000
For another 20% data: 0 ≤ Ai ≤ Bi ≤ 100000
For 100% data: n ≤ 100000, 0 ≤ Ai, Bi ≤ 1000000000

The idea is to give a range of difficulty that everyone can withstand. If this range is exceeded, the person will leave the field and find the greatest difficulty among the minimum number of people leaving the field. That is to find a point, this point is covered as many times as possible and as large as possible, and the optimal solution for this point must be on the endpoint, it can be proved that for each point that is not on the endpoint, one can be found on the endpoint Click to replace. For each player, add one to the tolerance range [l, r], which means that each difficulty in this interval has one more classmate who can compete and find the endpoint with the largest number. It is not difficult to think of the difference in the interval addend, but the range of this question l and r is relatively large, and it needs to be discretized.

The title is very similar to the team's new book, and it should be able to get rid of it with the scanning line.

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#define X first
#define Y second
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N=200010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
int a[N],cnt=0;
int idx,ans;
vector<int>v;
PII p[N];//存区间

int find(int x)
{
	return lower_bound(v.begin(),v.end(),x)-v.begin();
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

	scanf("%d",&n);
	
	for(int i=1;i<=n;i++)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		v.push_back(l);
		v.push_back(r);
		p[i]={l,r};
	}

	sort(v.begin(),v.end());
	v.erase(unique(v.begin(),v.end()),v.end());

	for(int i=1;i<=n;i++)
	{
		int l=p[i].X,r=p[i].Y;
		l=find(l),r=find(r);
		a[l]+=1,a[r+1]-=1;
	}
	
	n=v.size();
	for(int i=1;i<n;i++)
		a[i]+=a[i-1];
	
	idx=0,ans=a[0];
	for(int i=1;i<n;i++)
	{
		if(a[i]>=ans)
			ans=a[i],idx=i;
	}
	
	cout<<v[idx]<<endl;





	return 0;
}










Published 46 original articles · Like 3 · Visitor 3241

Guess you like

Origin blog.csdn.net/DaNIelLAk/article/details/105642605