决战 1
标程:http://blog.csdn.net/chlxyd/article/details/8800366
C DP
代码: 150ms
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int N=33; 6 const int M=404; 7 int dp[M][M]; 8 int v[N]; 9 int n,m,sum; 10 int solve() 11 { 12 for(int i=0; i<n; i++) 13 for(int j=m; j>=0; j--) 14 for(int k=m; k>=0; k--) 15 { 16 if(j>=v[i]&&dp[j-v[i]][k]+v[i]>dp[j][k]) 17 dp[j][k]=dp[j-v[i]][k]+v[i]; 18 if(k>=v[i]&&dp[j][k-v[i]]+v[i]>dp[j][k]) 19 dp[j][k]=dp[j][k-v[i]]+v[i]; 20 if(sum-dp[j][k]<=m) 21 return 1; 22 } 23 return 0; 24 } 25 int main() 26 { 27 int t1,t2=0; 28 scanf("%d",&t1); 29 while(t1--) 30 { 31 t2++; 32 scanf("%d%d",&n,&m); 33 sum=0; 34 bool flag=1; 35 for(int i=0; i<n; ++i) 36 { 37 scanf("%d",v+i); 38 sum+=v[i]; 39 if(v[i]>m) 40 flag=0; 41 } 42 for(int i=0; i<=m; i++) 43 for(int j=0; j<=m; j++) 44 dp[i][j]=0; 45 printf("Case %d: ",t2); 46 if(flag&&solve()) 47 puts("Yes"); 48 else 49 puts("No"); 50 } 51 return 0; 52 }
I
代码: 510ms
1 #include <stdio.h> 2 #include <iostream> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 int a[100005],b[100005]; 7 int main() 8 { 9 int T,n,i,minn,j,t,w=0; 10 scanf("%d",&T); 11 while(T--) 12 { 13 w++; 14 minn=999999; 15 memset(b,0,sizeof(b)); 16 scanf("%d",&n); 17 for(i=1;i<=n;i++) 18 scanf("%d",&a[i]); 19 sort(a+1,a+1+n); 20 for(i=1;i<=n;i++) 21 { 22 for(j=1;j<=a[i];) 23 { 24 if((a[i]&j)==j) 25 b[i]++; 26 j=j*2; 27 } 28 if(b[i]<minn) 29 minn=b[i]; 30 } 31 for(i=1;i<=n;i++) 32 if(b[i]==minn) 33 { 34 t=a[i]; 35 break; 36 } 37 printf("Case %d: %d\n",w,t); 38 } 39 return 0; 40 }
J
代码:2020Ms
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 typedef long long lld; 7 const lld N = 1010110; 8 const lld mod = 1000000007; 9 lld a[N]; 10 int main() 11 { 12 lld t, cas = 0; 13 scanf("%lld",&t); 14 while(t--) 15 { 16 lld n; 17 scanf("%lld",&n); 18 lld all=0; 19 for(lld i=1;i<=n;++i) 20 scanf("%lld",&a[i]),all=all+a[i]; 21 sort(a+1,a+1+n); 22 lld sum = 0, ans = 0; 23 for(lld i = 1; i <= n; ++i) 24 { 25 all=all-a[i]; 26 ans=(ans+all%mod*(a[i]-1)-(n-i)*((a[i]-1)*(a[i]-2)/2%mod))%mod;// 27 ans=(ans+sum)%mod; 28 sum=(sum+((1+a[i])*a[i]/2)%mod)%mod; //当后面只剩下一个数时不用加 29 } 30 if(ans<0) 31 ans=ans+mod; 32 printf("Case %lld: %lld\n",++cas,ans); 33 } 34 }