Looking at the data carefully, we can see that if the maximum height of each cow is required, just subtract 1 from the number in the given interval (note that it is an open interval). But only 50 points. Because we have to judge whether we have entered this range before, otherwise we will repeat the subtraction.
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int n,i,h,r; int a[10010]; int b[10010][10010]; int main() { scanf("%d%d%d%d",&n,&i,&h,&r); for(int i=1;i<=n;i++) { a[i]=h; } for(int i=1;i<=r;i++) { int x,y; cin>>x>>y; if(x==y||abs(x-y)==1||b[x][y]==1) continue; if(x<y) { for(int j=x+1;j<=y-1;j++) { a[j]--; } b[x][y]=b[y][x]=1; continue; } if(x>y) { for(int j=x-1;j>=y+1;j--) { a[j]--; } continue; } } for(int i=1;i<=n;i++) { cout<<a[i]<<endl; } return 0; }
Of course, the above method is not the best, because the array is too large, just because the online evaluation of how much space you use and how much space the machine gives you, the official competition will be all MLE. We also have the optimization of weight determination. It is to store the read interval in the structure and sort the structure according to the size of X. before subtraction, we scan the structure once. If the two adjacent numbers x and y are equal, then set x+1 and y+1 to 0 to achieve the weight judgment effect. After this process, we can get a faster positive solution
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int n,i,h,r; struct edge { int x; int y; }a[10010]; int t[10010]; bool cmp(const edge &a,const edge &b) { return a.x<b.x; } int main() { scanf("%d%d%d%d",&n,&i,&h,&r); for(int i=1;i<=n;i++) { t[i]=h; } for(int i=1;i<=r;i++) { cin>>a[i].x>>a[i].y; } sort(a+1,a+r+1,cmp); for(int i=1;i<=r;i++) { if(a[i].x==a[i+1].x&&a[i].y==a[i+1].y) { a[i+1].x=0; a[i+1].y=0; } } for(int i=1;i<=r;i++) { if(a[i].x==a[i].y||abs(a[i].x-a[i].y==1)) { continue; } if(a[i].x<a[i].y) { for(int j=a[i].x+1;j<=a[i].y-1;j++) { t[j]--; } continue; } if(a[i].x>a[i].y) { for(int j=a[i].x-1;j>=a[i].y+1;j--) { t[j]--; } continue; } } for(int i=1;i<=n;i++) { cout<<t[i]<<endl; } return 0; }