记录编号 84503 评测结果 AAAAAAAAAAAAA
题目名称 [USACO Nov13] 不设找零 最终得分 100
用户昵称 GravatarChenyao2333 是否通过 通过
代码语言 C++ 运行时间 0.309 s
提交时间 2013-12-14 15:17:29 内存使用 1.67 MiB
显示代码纯文本
  1. #include<stdio.h>
  2.  
  3. const int MAXN=100000+10;
  4. const int MAXK=16+2;
  5.  
  6. typedef int LL;
  7.  
  8. int K,N;
  9. LL S[MAXN]={0};
  10. LL val[MAXK]={0};
  11. LL ans=0;
  12. LL have_m=0;
  13.  
  14. inline LL max(LL a,LL b){
  15. return a>b?a:b;
  16. }
  17.  
  18. void read(){
  19. //int t;
  20. scanf("%d %d",&K,&N);
  21. for(int i=1;i<=K;i++){
  22. scanf("%d",&val[i]);
  23. have_m+=val[i];
  24. }
  25. for(int i=1;i<=N;i++){
  26. scanf("%d",&S[i]);
  27. S[i]+=S[i-1];
  28. }
  29. }
  30.  
  31. inline LL sum(int star,int end){
  32. return S[end]-S[star-1];
  33. }
  34.  
  35. LL get_cost(int s){
  36. LL cost=0;
  37. for(int i=K;i>0;i--){
  38. if(s%2)cost+=val[i];
  39. s/=2;
  40. }
  41. return cost;
  42. }
  43.  
  44. int find(LL cost,int w){
  45. int left=w,right=N;
  46. while(left<right){
  47. int m=(left+right)/2;
  48. if(sum(w,m)<=cost)left=m+1;
  49. else right=m;
  50. }
  51. if(sum(w,right)>cost)right--;
  52. return right;
  53. }
  54.  
  55. LL is_s[1<<MAXK]={0};
  56. LL solve(int s){
  57. if(is_s[s])return is_s[s];
  58. if(s==0)return 1;
  59. LL ret=0;
  60. for(int p=1;p<(1<<K);p=p<<1){
  61. if((s&p)==0)continue;
  62. int w=solve(s^p);
  63. if(w>N){
  64. ret=N+1;
  65. continue;
  66. }
  67. LL cost=get_cost(p);
  68. int l=find(cost,w);
  69. if(l>=N){
  70. ans=max(have_m-get_cost(s),ans);
  71. ret=N+1;
  72. }else ret=max(ret,l+1);
  73. }
  74. is_s[s]=ret;
  75. return ret;
  76. }
  77.  
  78. void open(){
  79. freopen("nochange.in","r",stdin);
  80. freopen("nochange.out","w",stdout);
  81. }
  82.  
  83. int main(){
  84. open();
  85. read();
  86. int w=solve((1<<K)-1);
  87. if(w>N)printf("%d",ans);
  88. else printf("-1");
  89. return 0;
  90. }