瞎搞-貪心-NOIP前42天-入門-噴水裝置
阿新 • • 發佈:2018-11-11
作死三部曲:
Step 1
#include<bits/stdc++.h> #define rep(i,l,r) for(int i=(l);i<=(r);i++) #define per(i,r,l) for(int i=(r);i>=(l);i--) #define random(l,r) ((l)+rand()%((r)-(l)+1)) using namespace std; typedef unsigned long long ull; typedef long long ll; const int inf=1e9+10,N=20000; const double eps=1e-6; struct node{double a,b;}p[N]; bool compare(node a,node b){ return a.a<b.a;} int t,n; double l,w,x,r; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--){ memset(p,0,sizeof(p)); double last=0,now=0; int ans=0; cin>>n>>l>>w; rep(i,1,n){ cin>>x>>r; if(r*2<=w) continue; p[i].a=x-sqrt((double)r*r-w*w/4.0f); p[i].b=x+sqrt((double)r*r-w*w/4.0f); } sort(p+1,p+1+n,compare); rep(i,1,n){ if(p[i].a>last){ if(now>0) ans++,last=now,now=0; else{ ans=-1; break; } } if(p[i].b>=last) now=max(now,p[i].b); } if(last<l) cout<<0<<endl; else cout<<ans<<endl; } return 0; }
到最後的時候now裡可能還有值,也就是最後的那個噴水裝置沒被使用,因為最後一個不會在迴圈中被更新。因此導致答案錯誤。(本來可以覆蓋的結果變成不能覆蓋的了)。
Step 2:
#include<bits/stdc++.h> #define rep(i,l,r) for(int i=(l);i<=(r);i++) #define per(i,r,l) for(int i=(r);i>=(l);i--) #define random(l,r) ((l)+rand()%((r)-(l)+1)) using namespace std; typedef unsigned long long ull; typedef long long ll; const int inf=1e9+10,N=20000; const double eps=1e-6; struct node{double a,b;}p[N]; bool compare(node a,node b){ return a.a<b.a;} int t,n; double l,w,x,r; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--){ memset(p,0,sizeof(p)); double last=0,now=0; int ans=0; cin>>n>>l>>w; rep(i,1,n){ cin>>x>>r; if(r*2<=w) continue; p[i].a=x-sqrt((double)r*r-w*w/4.0f); p[i].b=x+sqrt((double)r*r-w*w/4.0f); } sort(p+1,p+1+n,compare); rep(i,1,n){ if(p[i].a>last){ if(now>0) ans++,last=now,now=0; else{ ans=-1; break; } } if(p[i].b>=last) now=max(now,p[i].b); } ***if(now>0) ans++,last=now,now=0;*** if(last<l) cout<<0<<endl; else cout<<ans<<endl; } return 0; }
將最後本該使用的噴水裝置使用了,以為要AC了,結果又WA了。。因為其實有時候最後這一個噴水裝置不用的話也已經完全覆蓋了,再多用答案就錯了。
Step 3 AC程式碼:
#include<bits/stdc++.h> #define rep(i,l,r) for(int i=(l);i<=(r);i++) #define per(i,r,l) for(int i=(r);i>=(l);i--) #define random(l,r) ((l)+rand()%((r)-(l)+1)) using namespace std; typedef unsigned long long ull; typedef long long ll; const int inf=1e9+10,N=20000; const double eps=1e-6; struct node{double a,b;}p[N]; bool compare(node a,node b){ return a.a<b.a;} int t,n; double l,w,x,r; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--){ memset(p,0,sizeof(p)); double last=0,now=0; int ans=0; cin>>n>>l>>w; rep(i,1,n){ cin>>x>>r; if(r*2<=w) continue; p[i].a=x-sqrt((double)r*r-w*w/4.0f); p[i].b=x+sqrt((double)r*r-w*w/4.0f); } sort(p+1,p+1+n,compare); rep(i,1,n){ if(p[i].a>last){ if(now>0) ans++,last=now,now=0; else{ ans=-1; break; } } if(p[i].b>=last) now=max(now,p[i].b); } ***if((now>0)&&(last<l)) ans++,last=now,now=0;*** if(last<l) cout<<0<<endl; else cout<<ans<<endl; } return 0; }