9/21 越努力越幸运-思维赛(4.0) 解题思路
A
题意:有两种水为1升和2升,给出这两种水的价格和要买的水的升数,求刚好买所需的升数花的最少钱
解题思路:判断水的奇偶和2升水价格的奇偶性,列出相应计算式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<math.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int main() 7 { 8 long long q,n,a,b,minn; 9 while(cin>>q) 10 { 11 while(q--) 12 { 13 cin>>n>>a>>b; 14 minn=a*n; 15 if(n%2==0) 16 { 17 if(b%2==0) 18 { 19 minn=min(minn,b/2*n); 20 } 21 else 22 { 23 minn=min(minn,n/2*b); 24 } 25 } 26 else 27 { 28 if(b%2==0) 29 { 30 minn=min(minn,b/2*(n-1)+a); 31 } 32 else 33 { 34 minn=min(minn,b*(n/2)+a); 35 } 36 } 37 cout<<minn<<endl; 38 } 39 } 40 return 0; 41 }
B
题意:给一个数组,数组中的数都是2的次方,两个相同的数可以相加重新存入数组,判断能否出现2048
解题思路:进行桶排序和建立新数组存相加后的数组,循环进行
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<math.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int main() 7 { 8 int q,n,a[10000],i,b[10000]; 9 cin>>q; 10 while(q--) 11 { 12 cin>>n; 13 for(i=0;i<n;i++) 14 cin>>a[i]; 15 for(i=0;i<10000;i++) 16 b[i]=0; 17 for(i=0;i<n;i++) 18 { 19 if(a[i]<=2048) 20 b[a[i]]++; 21 } 22 int flag=0; 23 for(i=1;i<=1024;i++) 24 { 25 while(b[i]>1) 26 { 27 b[i*2]++; 28 b[i]=b[i]-2; 29 } 30 } 31 if(b[2048]) 32 cout<<"YES"<<endl; 33 else 34 cout<<"NO"<<endl; 35 } 36 return 0; 37 }
C
题意:给一个数组,从数组中依次去掉一个数,判断剩下的奇数和偶数和是否相等
解题思路:先求出不去之前的奇偶之和,然后依次扫描数组,i代表去掉第i个数
奇数和=(i之前的奇数和+i之后的偶数和)
偶数和=(i之前的偶数和+i之后的奇数和)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<math.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int main() 7 { 8 long long n; 9 int a[200010],i,sum1=0,sum2=0,x=0; 10 while(cin>>n) 11 { 12 for(i=1;i<=n;i++) 13 cin>>a[i]; 14 for(i=1;i<=n;i++) 15 { 16 if(i%2!=0) 17 sum1+=a[i]; 18 else 19 sum2+=a[i]; 20 } 21 int newsum1=0,newsum2=0; 22 for(i=1;i<=n;i++) 23 { 24 if(i%2!=0) 25 { 26 sum1-=a[i]; 27 if(sum1+newsum2==sum2+newsum1) 28 x++; 29 newsum1+=a[i]; 30 } 31 else 32 { 33 sum2-=a[i]; 34 if(sum2+newsum1==sum1+newsum2) 35 x++; 36 newsum2+=a[i]; 37 } 38 } 39 cout<<x<<endl; 40 } 41 return 0; 42 }
E
题意:一个ICPC队需要三个人才能组队成功,其中必须要有coder and mathematician,给三个数代表三种人的个数,求最多能组成多少个队
解题思路:比较coder and mathematician和三个数之和/3的最小值即为所求
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<math.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int main() 7 { 8 int q,c,m,x,ans=0,minn; 9 cin>>q; 10 while(q--) 11 { 12 cin>>c>>m>>x; 13 minn=c; 14 if(c+m+x<3) 15 cout<<"0"<<endl; 16 else 17 { 18 minn=min(minn,m); 19 minn=min(minn,(c+m+x)/3); 20 cout<<minn<<endl; 21 } 22 } 23 return 0; 24 }