codeforces 289 C Sums of Digits
2015-02-01 10:52
357 查看
贪心,做了两个半小时没做出来!主要在于求有k位且恰比某个数大的最小值,从左到右检查可以加1的位,若后面的位满足条件,再根据solve中的方法满足条件的最小值。
#include<iostream> #include<string> #include<cstring> #include<cstdio> #include<cmath> #include<iomanip> #include<map> #include<algorithm> #include<queue> #include<set> #define inf 10000000 #define pi acos(-1.0) #define eps 1e-8 #define seed 131 using namespace std; typedef pair<int,int> pii; typedef unsigned long long ULL; typedef long long LL; const int maxn=100005; int n; int b[305]; int a[305][1000]; void solve(int k,int u,int p) { int tot=0; for(int i=1;i<=k;i++) { a[p][i]=9; tot+=9; } for(int i=1;i<=k;i++) { if(i==1&&k!=1) { if(tot-u>8) { a[p][i]-=8; tot-=8; } else { a[p][i]-=(tot-u); return; } } else { if(tot-u>9) { a[p][i]-=9; tot-=9; } else { a[p][i]-=(tot-u); return; } } } } bool so(int k,int u,int p) { int tot=0; for(int i=1;i<=k;i++) { a[p][i]=a[p-1][i]; tot+=a[p][i]; } if(tot<u) { for(int i=k;i>=1;i--) { if(u-tot>9-a[p][i]) { tot+=9-a[p][i]; a[p][i]=9; } else { a[p][i]+=u-tot; return true; } } return false; } else if(tot==u) { int m=0; for(int i=k;i>=1;i--) { if(a[p][i]<9&&m>0) { a[p][i]++; int e=0; m--; for(int j=i+1;j<=k;j++) { a[p][j]=9; e+=9; } for(int j=i+1;j<=k;j++) { if(e-m>9) { e-=9; a[p][j]=0; } else { a[p][j]-=(e-m); return true; } } } m+=a[p][i]; } return false; } else { int cha=tot-u; cha++; int m=0; for(int i=k;i>=1;i--) { if(a[p][i]<9&&m>=cha) { a[p][i]++; m-=cha; int e=0; for(int j=i+1;j<=k;j++) { a[p][j]=9; e+=9; } for(int j=i+1;j<=k;j++) { if(e-m>9) { e-=9; a[p][j]=0; } else { a[p][j]-=(e-m); return true; } } } m+=a[p][i]; } return false; } } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&b[i]); int wei[305]; int k; k=b[1]/9; if(b[1]%9!=0) k++; if(b[1]==0) k=1; solve(k,b[1],1); wei[1]=k; int ans=k; for(int i=2;i<=n;i++) { k=b[i]/9; if(b[i]%9!=0) k++; if(k<=ans) { k=ans; if(so(k,b[i],i)) { wei[i]=k; continue; } else { ans=k+1; solve(ans,b[i],i); wei[i]=ans; } } else { ans=k; solve(ans,b[i],i); wei[i]=ans; } } for(int i=1;i<=n;i++) { for(int j=1;j<=wei[i];j++) printf("%d",a[i][j]); printf("\n"); } return 0; }
相关文章推荐
- codeforces 509C C. Sums of Digits(数位dp+贪心+枚举)
- 【codeforces 509C】Sums of Digits
- Codeforces 509C Sums of Digits 贪心
- Codeforces 509c Sums of Digits 贪心
- Sums of Digits - CodeForces 509 C
- Codeforces Round #289 C. Sums of Digits(构造)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) C. Sums of Digits
- codeforces#277.5 C. Given Length and Sum of Digits
- [CF]Sums of Digits
- CodeForces 489C (贪心) Given Length and Sum of Digits...
- codeforces#277.5 C. Given Length and Sum of Digits
- [CodeForces-585F]Digits of Number Pi
- Codeforces-489C-Given Length and Sum of Digits...
- C - Sums of Digits
- codeforces 585F - Digits of Number Pi (dp + acauto)
- Codeforces - 277.5 (Div. 2)C - Given Length and Sum of Digits...(模拟 or dfs)
- codeforces—— 770B —— Maximize Sum of Digits
- Codeforces 489C - Given Length and Sum of Digits...(贪心)
- [cf509C]Sums of Digits
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)C. Sums of Digits