#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn = 100005; int n,m,cnt,d[maxn],h[maxn]; int main(){ freopen("note.in","r",stdin); freopen("note.out","w",stdout); cin>>n>>m; for(int i = 1;i <= m;i++){ scanf("%d%d",&d[i],&h[i]); } int ans = max(h[m] + n - d[m],h[1] + d[1] - 1); int hg,lw,div,mid; for(int i = 2;i <= m;i++){ div = h[i] - h[i-1]; if(div < 0) div = -div; if(div > d[i]-d[i-1]){ cout<<"IMPOSSIBLE"; return 0; } mid = (h[i] + h[i-1] + d[i] - d[i-1]) >> 1; ans = max(ans,mid); } cout<<ans; return 0; } #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<stack> #include<cstdlib> #include<string> #include<bitset> #define INF 200000000 #define N 100005 #define fi first #define se second #define debug(x) cout<<#x<<"="<<x<<endl #define MP(x,y) make_pair(x,y) using namespace std; typedef long long LL; typedef pair<int,int> pii; int a[N],b[N]; int main() { int n,m,ans=0,flag,t,l,r,mid,x,now,i; freopen("note.in","r",stdin); freopen("note.out","w",stdout); cin>>n>>m; for(i=1;i<=m;i++) { scanf("%d%d",&a[i],&b[i]); if(i==1) ans=b[i]+a[i]-1; else { t=a[i]-a[i-1]; x=b[i]-b[i-1]; if(abs(x)>t) { printf("IMPOSSIBLE\n"); return 0; } x=abs(x); ans=max(ans,max(b[i],b[i-1])); l=0,r=INF; now=0; while(l<=r) { mid=(l+r)>>1; if(mid+abs(mid-x)<=t) now=mid,l=mid+1; else r=mid-1; } ans=max(ans,min(b[i],b[i-1])+now); } if(i==m) ans=max(ans,b[i]+n-a[i]); } cout<<ans<<endl; return 0; } // davidlee1999WTK 2015/ // srO myk Orz //ios::sync_with_stdio(false); /* 100 1 10 10 */
时间: 2024-10-24 21:11:11