赞
踩
- #define x first
- #define y second
- typedef pair<int, int>pii;
- pii seg[N];
-
- sort(seg,seg+n);
- int l=seg[0].x,r=seg[0].y;
- for (int i=1;i<n;i++){
- if (seg[i].x<=r) r=max(r,seg[i].y);
- else l=seg[i].x,r=seg[i].y;
- }
- #include<iostream>
- #include<algorithm>
- #define x first
- #define y second
- using namespace std;
- const int N=5005;
- typedef pair<int,int>pii;
- pii seg[N];
- int n,ans1,ans2;
- int main(){
- cin>>n;
- for(int i=0;i<n;i++) cin>>seg[i].x>>seg[i].y;
- sort(seg,seg+n);
- int l=seg[0].x,r=seg[0].y;
- for(int i=0;i<n;i++){
- if(seg[i].x<=r){
- r=max(r,seg[i].y);
- ans1=max(ans1,r-l);
- }
- else{
- ans2=max(ans2,seg[i].x-r);
- l=seg[i].x,r=seg[i].y;
- ans1=max(ans1,r-l);
- }
- }
- cout<<ans1<<" "<<ans2<<endl;
- return 0;
- }
- #include<iostream>
- #include<algorithm>
- #define x first
- #define y second
- using namespace std;
- const int N=1e5+5;
- typedef pair<int,int>pii;
- pii seg[N];
- long long ans=1;
- int main(){
- int n;cin>>n;
- for(int i=0;i<n;i++) cin>>seg[i].x>>seg[i].y;
- sort(seg,seg+n);
- int l=seg[0].x,r=seg[0].y;
- for(int i=0;i<n;i++){
- if(seg[i].x<=r) r=max(r,seg[i].y);
- else l=seg[i].x,r=seg[i].y,ans++;
- }
- cout<<ans<<endl;
- return 0;
- }
- #include<iostream>
- #include<algorithm>
- #define x first
- #define y second
- using namespace std;
- const int N=105;
- typedef pair<int,int>pii;
- pii seg[N];
- int ans;
- int main(){
- int len,n;cin>>len>>n;
- for(int i=0;i<n;i++) cin>>seg[i].x>>seg[i].y;
- sort(seg,seg+n);
- int l=seg[0].x,r=seg[0].y;
- for(int i=0;i<n;i++){
- if(seg[i].x<=r) r=max(r,seg[i].y);
- else ans+=r-l+1,l=seg[i].x,r=seg[i].y;
- }
- ans+=r-l+1;
- cout<<len+1-ans<<endl;
- return 0;
- }
- #include<iostream>
- #include<algorithm>
- #define x first
- #define y second
- using namespace std;
- const int N=1e5+5;
- int n,len,id[N],timee[N];
- typedef pair<int,int>pii;
- pii seg[N];
- bool check(long long value){
- int j=0,flag=1;
- //划分区间
- for(int i=1;i<=n;i++){
- if(timee[i]<=value){
- //flag进行标记
- flag=0;
- int temp=value-timee[i];
- seg[j].x=max(1,id[i]-temp),seg[j].y=min(len,id[i]+temp),j++;
- }
- }
- //如果flag==1则该时间绝对不可能
- if(flag) return false;
- //排序
- sort(seg,seg+j);
- //合并区间
- int l=seg[0].x,r=seg[0].y,cnt=0;
- for(int i=0;i<j;i++){
- if(seg[i].x<=r+1) r=max(r,seg[i].y);
- else{
- //cnt计数
- cnt+=r-l+1;
- l=seg[i].x;
- r=seg[i].y;
- }
- //最后一次合并区间后,cnt计数
- if(i==j-1) cnt+=r-l+1;
- }
- //check判断bool
- if(cnt==len) return true;
- else return false;
- }
- int main(){
- //输入
- cin>>n>>len;
- for(int i=1;i<=n;i++) cin>>id[i]>>timee[i];
- //二分查找
- int left=1,right=2e9;//注意right的值为2e9
- while(left<right){
- //注意mid开浪浪,否则最大测试数据会爆
- long long mid=(long long)left+right>>1;
- if(check(mid)) right=mid;
- else left=mid+1;
- }
- //输出
- cout<<left<<endl;
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。