Codeforces Round #220 (Div. 2)
阿新 • • 發佈:2018-02-06
mod 越界 題意 sin 貪心 nat esp break class
鏈接
毒瘤場.....
A題:,真碼農題,直接幹爆,枚舉,註意越界問題,wa37的看這組數據1 10 1 5 2 2,應該是no
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #defineAfi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #definebase 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-12; const int N=1000000+10,maxn=2000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; int main() { int n,m,i,j,a,b; scanf("%d%d%d%d%d%d",&n,&m,&i,&j,&a,&b); int ans1=-1,ans2=-1,ans3=-1,ans4=-1; if(abs(i-1)%a==0&&abs(j-1)%b==0&&abs(i-1)/a%2==abs(j-1)/b%2) { if(abs(j-1)/b!=0&&(1<=i-a||i+a<=n)&&abs(i-1)/a==0)ans1=max(abs(i-1)/a,abs(j-1)/b); else if(abs(i-1)/a!=0&&(1<=j-b||j+b<=m)&&abs(j-1)/b!=0)ans1=max(abs(i-1)/a,abs(j-1)/b); else if(abs(j-1)/b!=0&&(1<=i-a||i+a<=n)&&abs(i-1)/a!=0&&(1<=j-b||j+b<=m))ans1=max(abs(i-1)/a,abs(j-1)/b); else if(abs(i-1)/a==0&&abs(j-1)/b==0)ans1=max(abs(i-1)/a,abs(j-1)/b); } if(abs(i-1)%a==0&&abs(j-m)%b==0&&abs(i-1)/a%2==abs(j-m)/b%2) { if(abs(j-m)/b!=0&&(1<=i-a||i+a<=n)&&abs(i-1)/a==0)ans2=max(abs(i-1)/a,abs(j-m)/b); else if(abs(i-1)/a!=0&&(1<=j-b||j+b<=m)&&abs(j-m)/b!=0)ans2=max(abs(i-1)/a,abs(j-m)/b); else if(abs(j-m)/b!=0&&(1<=i-a||i+a<=n)&&abs(i-1)/a!=0&&(1<=j-b||j+b<=m))ans2=max(abs(i-1)/a,abs(j-m)/b); else if(abs(i-1)/a==0&&abs(j-m)/b==0)ans2=max(abs(i-1)/a,abs(j-m)/b); } if(abs(i-n)%a==0&&abs(j-1)%b==0&&abs(i-n)/a%2==abs(j-1)/b%2) { if(abs(j-1)/b!=0&&(1<=i-a||i+a<=n)&&abs(i-n)/a==0)ans3=max(abs(i-n)/a,abs(j-1)/b); else if(abs(i-n)/a!=0&&(1<=j-b||j+b<=m)&&abs(j-1)/b!=0)ans3=max(abs(i-n)/a,abs(j-1)/b); else if(abs(j-1)/b!=0&&(1<=i-a||i+a<=n)&&abs(i-n)/a!=0&&(1<=j-b||j+b<=m))ans3=max(abs(i-n)/a,abs(j-1)/b); else if(abs(i-n)/a==0&&abs(j-1)/b==0)ans3=max(abs(i-n)/a,abs(j-1)/b); } if(abs(i-n)%a==0&&abs(j-m)%b==0&&abs(i-n)/a%2==abs(j-m)/b%2) { if(abs(j-m)/b!=0&&(1<=i-a||i+a<=n)&&abs(i-n)/a==0)ans4=max(abs(i-n)/a,abs(j-m)/b); else if(abs(i-n)/a!=0&&(1<=j-b||j+b<=m)&&abs(j-m)/b!=0)ans4=max(abs(i-n)/a,abs(j-m)/b); else if(abs(j-m)/b!=0&&(1<=i-a||i+a<=n)&&abs(i-n)/a!=0&&(1<=j-b||j+b<=m))ans4=max(abs(i-1)/a,abs(j-m)/b); else if(abs(i-n)/a==0&&abs(j-m)/b==0)ans4=max(abs(i-n)/a,abs(j-m)/b); } // printf("%d %d\n",abs(i-n)/a%2,abs(j-m)/b%2); if(ans1!=-1||ans2!=-1||ans3!=-1||ans4!=-1) { int ans=100000000; if(ans1!=-1)ans=min(ans,ans1); if(ans2!=-1)ans=min(ans,ans2); if(ans3!=-1)ans=min(ans,ans3); if(ans4!=-1)ans=min(ans,ans4); printf("%d\n",ans); } else puts("Poor Inna and pony!"); return 0; } /******************** 3 5 2 2 1 3 ********************/
B題:日常貪心不會寫,xjb寫成了dp,還寫搓了
題意:找相鄰的合成9,要求組出最多的9的方案數;
直接貪心的掃到最遠處,類似於72727這樣的,然後乘到答案上
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-12; const int N=100000+10,maxn=2000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; char s[N]; int main() { scanf("%s",s+1); int sz=strlen(s+1); ll ans=1,p=1; for(int i=2;i<=sz;i++) { if(s[i]-‘0‘+s[i-1]-‘0‘==9)p++; else { // printf("%d\n",p); if(p!=1&&p%2==1)ans*=(p+1)/2; p=1; } } // printf("%d\n",p); if(p!=1&&p%2==1)ans*=(p+1)/2; printf("%lld\n",ans); return 0; } /******************** ********************/B
C:有nm的矩陣,找最長的dima,轉化成dag上的dp,從d開始dp,然後找能走的最遠距離除4就是答案,註意這題要判環,用一個vis標記,-1表示當前正在訪問的這一條路,1表示訪問過了,0表示沒有訪問過,如果訪問到了一個正在訪問的地方,那麽就是有環
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-9; const int N=1000+10,maxn=2000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; char s[N][N]; int dp[N*N]; int n,m; int vis[N*N]; vector<int>v[N*N]; int dfs(int u) { // printf("%d\n",u); if(dp[u]!=-1)return dp[u]; dp[u]=1;vis[u]=-1; for(int i=0;i<v[u].size();i++) { int x=v[u][i]; if(vis[x]<0) { puts("Poor Inna!"); exit(0); } dp[u]=max(dp[u],dfs(x)+1); } vis[u]=1; return dp[u]; } int main() { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(s[i][j]==‘D‘) { if(i+1<n&&s[i+1][j]==‘I‘)v[i*m+j].pb((i+1)*m+j); if(i-1>=0&&s[i-1][j]==‘I‘)v[i*m+j].pb((i-1)*m+j); if(j+1<m&&s[i][j+1]==‘I‘)v[i*m+j].pb(i*m+j+1); if(j-1>=0&&s[i][j-1]==‘I‘)v[i*m+j].pb(i*m+j-1); } else if(s[i][j]==‘I‘) { if(i+1<n&&s[i+1][j]==‘M‘)v[i*m+j].pb((i+1)*m+j); if(i-1>=0&&s[i-1][j]==‘M‘)v[i*m+j].pb((i-1)*m+j); if(j+1<m&&s[i][j+1]==‘M‘)v[i*m+j].pb(i*m+j+1); if(j-1>=0&&s[i][j-1]==‘M‘)v[i*m+j].pb(i*m+j-1); } else if(s[i][j]==‘M‘) { if(i+1<n&&s[i+1][j]==‘A‘)v[i*m+j].pb((i+1)*m+j); if(i-1>=0&&s[i-1][j]==‘A‘)v[i*m+j].pb((i-1)*m+j); if(j+1<m&&s[i][j+1]==‘A‘)v[i*m+j].pb(i*m+j+1); if(j-1>=0&&s[i][j-1]==‘A‘)v[i*m+j].pb(i*m+j-1); } else if(s[i][j]==‘A‘) { if(i+1<n&&s[i+1][j]==‘D‘)v[i*m+j].pb((i+1)*m+j); if(i-1>=0&&s[i-1][j]==‘D‘)v[i*m+j].pb((i-1)*m+j); if(j+1<m&&s[i][j+1]==‘D‘)v[i*m+j].pb(i*m+j+1); if(j-1>=0&&s[i][j-1]==‘D‘)v[i*m+j].pb(i*m+j-1); } } } memset(dp,-1,sizeof dp); int ans=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { int res=dfs(i*m+j); if(s[i][j]==‘D‘) ans=max(ans,res/4); } if(ans==0)puts("Poor Dima!"); else printf("%d\n",ans); return 0; } /******************** ********************/C
D:有一個隊列,三種操作,1代表插入1,0代表插入0,-1代表刪除下標為a[i]的數,最後輸出隊列裏的數即可
套路題,樹狀數組維護前綴和,每次刪點就在樹狀數組裏的對應地方刪點,然後二分找對應在樹狀數組裏的下標
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-9; const int N=1000000+10,maxn=2000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; int sum[N],a[N],ans[N]; int cnt; void add(int i,int v) { while(i<N) { sum[i]+=v; i+=i&(-i); } } int query(int i) { int ans=0; while(i>0) { ans+=sum[i]; i-=i&(-i); } return ans; } int change(int x) { int l=0,r=cnt+1; while(l<r-1) { int m=(l+r)>>1; // printf("%d %d\n",m,query(m)); if(query(m)<x)l=m; else r=m; } if(query(r)<x)return -1; return r; } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++)scanf("%d",&a[i]); cnt=1; for(int i=0;i<n;i++) { int x; scanf("%d",&x); if(x==-1) { vector<int>v; for(int j=1;j<=m;j++) { int pos=change(a[j]); // printf("###%d!!!\n",pos); if(pos==-1)break; v.pb(pos); } for(int i=0;i<v.size();i++) add(v[i],-1); } else { add(cnt,1); ans[cnt++]=x; } } // printf("%d\n",change(1)); if(change(1)==-1)return 0*puts("Poor stack!"); for(int i=1;;i++) { int pos=change(i); if(pos==-1)break; printf("%d",ans[pos]); } return 0; } /******************** ********************/D
Codeforces Round #220 (Div. 2)