版本比较
急救 - TianX 的做题计划 - 第三章题解
正文
增加 455 行删除 0 行
215 行未改动全部展开216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674215 行未改动全部展开便倾我心,烛照山河,独向苍茫颂春秋。*/
```### P3669```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0int n;signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n; vector<pair<int,int>>a(n); for(int i=0;i<n;i++) cin>>a[i].second>>a[i].first; sort(a.begin(),a.end()); int l=0,r=n-1,ans=0; while(l<r){ if(a[l].second==0){l++;continue;} if(a[r].second==0){r--;continue;} int p=min(a[l].second,a[r].second); ans=max(ans,a[l].first+a[r].first); a[l].second-=p; a[r].second-=p; } cout<<ans; Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/```### P4613```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0const int maxn=105;int n,x[maxn],y[maxn],f=1;signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;i++) cin>>x[i]; for(int i=1;i<=n;i++) cin>>y[i]; sort(x+1,x+n+1); sort(y+1,y+n+1); for(int i=1;i<=n;i++) if(x[i]>y[i]) f=0; if(f==1) cout<<"DA\n"; else cout<<"NE\n"; Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/```### P7584## Yellow### B4379### P1551```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define You_are_a_brick return#define tied_to_me_that 0const int maxn=5005;int n,m,p,fa[maxn];int find(int x){ if(x==fa[x]) return x; else return fa[x]=find(fa[x]);}signed main(){ cin>>n>>m>>p; for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=m;i++){ int a,b; cin>>a>>b; if(find(a)!=find(b)) fa[find(a)]=find(b); } for(int i=1;i<=p;i++){ int a,b; cin>>a>>b; if(find(a)!=find(b)) cout<<"No\n"; else cout<<"Yes\n"; } You_are_a_brick tied_to_me_that;}//You are a brick tied to me that dragging me down.```### P3916```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define You_are_a_brick return#define tied_to_me_that 0const int maxn=1e5+5;int n,m,ans[maxn];vector<int>G[maxn];void dfs(int now,int x){ if(ans[x]) You_are_a_brick; ans[x]=now; for(auto op:G[x]) dfs(now,op);}signed main(){ cin>>n>>m; for(int i=1;i<=m;i++){ int u,v; cin>>u>>v; G[v].push_back(u); } for(int i=n;i>=1;i--) dfs(i,i); for(int i=1;i<=n;i++) cout<<ans[i]<<' '; You_are_a_brick tied_to_me_that;//You are a brick tied to me that dragging me down.}```### P12211### P12891### P10518### P16264## Green### P2041```cpp#include<bits/stdc++.h>using namespace std;#define int long longsigned main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; cin>>n; if(n==1) cout<<"1\n1 1"; else if(n==2) cout<<"4\n1 1\n2 1\n2 2\n1 2"; else cout<<"-1"; return 0;}
```
### P7589### P9963```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0double n,p,l,r;signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n>>p>>l>>r; printf("%.7f",(r-l+1.0)*p*1.0); Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/
```
### P10532```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; cin>>n; cout<<n*n; Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/```### P15746## Blue### P1472### P1896```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0int n,k,cnt,s[1<<12],num[1<<12],f[12][144][1<<12];signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n>>k; for(int i=0;i<(1<<n);i++) if(!(i&i>>1)){ s[cnt++]=i; for(int j=0;j<n;j++)num[i]+=(i>>j&1); } f[0][0][0]=1; for(int i=1;i<=n+1;i++) for(int j=0;j<=k;j++) for(int a=0;a<cnt;a++) for(int b=0;b<cnt;b++){ int c=num[s[a]]; if((j>=c)&&!(s[b]&s[a])&&!(s[b]&(s[a]<<1))&&!(s[b]&(s[a]>>1))) f[i][j][a]+=f[i-1][j-c][b]; } cout<<f[n+1][k][0]; Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/```### P2258### P3147### P4053## Purple### P2123### P3539```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0const int maxn=90;int f[maxn+2],q;void init(){ f[1]=1,f[2]=1; for(int i=3;i<=maxn+1;i++) f[i]=f[i-1]+f[i-2];}int calc(int n){ int cnt=0; while(n>0){ int p=lower_bound(f,f+maxn+2,n)-f; if(f[p]==n){cnt++;break;} n=min(n-f[p-1],f[p]-n); cnt++; } Moonlight cnt;}signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); init(); cin>>q; while(q--){ int k; cin>>k; cout<<calc(k)<<'\n'; } Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/
```
### P3773```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0const int maxn=250000,mod=1000000007;int n,f[maxn],ans=0,a;signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>a; for(int j=a&(a-1);j;j=(j-1)&a) f[j]=(f[j]+f[a]+1)%mod; ans=(ans+f[a])%mod; } cout<<ans; Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/
```
### P5207### P9659### P16347```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0int t;signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>t; if(t==1) cout<<"3 1\nP 114514 2\nO\nI 1 1"; else if(t==2) cout<<"6 1\nI 1 2\nP 0 3\nT 6\nD 0 1 2 3 0 1\nP 1 4\nO"; else if(t==3) cout<<"5 1\nI 1 4\nI 2 3\nT 5\nC 3 4\nO"; else if(t==4) cout<<"10 1\nI 1 3\nI 2 4\nC 5 8\nN 5\nP 10000 6\nR 10000 7\nN 10\nP 0 9\nN 7\nO"; else if(t==5) cout<<"49 1\nI 1 3\nO\nT 2\nP 0 3\nP 0 4\nP 0 5\nP 0 6\nP 0 7\nP 0 8\nP 0 9\nP 0 10\nP 0 11\nP 0 12\nP 0 13\nP 0 14\nP 0 15\nP 0 16\nP 0 17\nP 0 18\nP 0 19\nP 0 20\nP 0 21\nP 0 22\nD 0 1 1 4 0 1\nP 1 24\nD 0 1 1 6 0 1\nP 1 26\nD 0 1 1 7 0 1\nP 1 28\nD 0 1 1 8 0 1\nP 1 30\nD 0 1 1 10 0 1\nP 1 32\nD 0 1 1 11 0 1\nP 1 34\nD 0 1 1 13 0 1\nP 1 36\nD 0 1 1 15 0 1\nP 1 38\nD 0 1 1 16 0 1\nP 1 40\nD 0 1 1 18 0 1\nP 1 42\nD 0 1 1 19 0 1\nP 1 44\nD 0 1 1 21 0 1\nP 1 46\nD 0 1 1 23 0 1\nP 1 48"; else if(t==6) cout<<"7 2\nI 1 2\nC 3 4\nC 2 2\nT 7\nP 1 6\nD 0 1 -39 4 0 1\nO"; else if(t==7) cout<<"20 2\nI 1 1\nI 3 3\nP 0 4\nP 0 5\nP 0 6\nP 0 7\nT 17\nI 2 9\nC 10 10\nR 1 11\nN 12\nC 12 13\nC 7 14\nT 15\nN 16\nT 20\nP 0 16\nD 0 1 206 16 0 1\nP 1 18\nO"; else if(t==8) cout<<"15 4\nI 1 2\nN 3\nC 4 4\nC 5 5\nP 0 6\nT 15\nI 2 8\nP 10000 9\nR 10000 10\nC 11 12\nN 8\nC 13 6\nP 0 14\nP 0 10\nO"; else cout<<"10 1\nI 2 2\nC 2 5\nI 3 4\nN 5\nD 0 1 10000 6 0 1\nR 10002 10\nP 1 8\nC 8 6\nI 1 5\nO"; Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/
```
## Black### P4548```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define Moonlight return#define whispers 0const int maxn=1e5+5,P=10000;int m,T,b[maxn],dp[maxn],w[maxn],kmp[maxn];vector<int>f[maxn];signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>m>>T; while(T--){ int n,cnt=0; cin>>n; for(int i=1;i<=n;i++){ w[i]=0,dp[i]=0; f[i].clear(); cin>>b[i]; if(i>=2){ while(cnt!=0&&b[i]!=b[cnt+1]) cnt=kmp[cnt]; if(b[i]==b[cnt+1]) cnt++; kmp[i]=cnt; } } for(int i=1;i<n;i++){ if(b[kmp[i]+1]!=b[i+1]) f[i].push_back(kmp[i]); for(auto j:f[kmp[i]]) if(b[j+1]!=b[i+1]) f[i].push_back(j); } for(int i=1;i<=n;i++){ int cnt=m-1,tot=0; for(auto j:f[i-1]) tot+=w[i-1]-w[j+1],cnt--; tot+=cnt*w[i-1],tot=(tot%P+P)%P; dp[i]=m+tot; w[i]=w[i-1]+dp[i],w[i]%=P; } if(w[n]<=9) cout<<0; if(w[n]<=99) cout<<0; if(w[n]<=999) cout<<0; cout<<w[n]<<endl; } Moonlight whispers/*月光私语*/;}/*I frequently reminisce about the past,please,go back to the beginning
望舒影里,碧落随星际,依记那年春光里,郁香予我情谊。本愿终生厮守,奈何苍天难求,郁香花开遍野,却见你的芳容。
夏夜已逝,彼时蓝星。
终有一日,星尘淬剑,长夜铮然开曙色;便倾我心,烛照山河,独向苍茫颂春秋。*/```
### P4727