简单贪心即可
1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector> 10 #include<string> 11 #include<cmath> 12 #include<ctime> 13 #include<stack> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define random(a,b) (rand()%(b-a+1)+a) 19 #define ll long long 20 #define ull unsigned long long 21 #define e 2.71828182 22 #define Pi acos(-1.0) 23 #define ls(rt) (rt<<1) 24 #define rs(rt) (rt<<1|1) 25 #define lowbit(x) (x&(-x)) 26 using namespace std; 27 const int MAXM=1e3+5; 28 int read() 29 { 30 int s=1,x=0; 31 char ch=getchar(); 32 while(!isdigit(ch)) {if(ch==‘-‘) s=-1;ch=getchar();} 33 while(isdigit(ch)) {x=10*x+ch-‘0‘;ch=getchar();} 34 return x*s; 35 } 36 int num[MAXM][6]; 37 int maxmum[MAXM]; 38 int main() 39 { 40 int n=read(),m=read(); 41 int t; 42 string answer; 43 for(int i=1;i<=n;++i) 44 { 45 cin>>answer; 46 for(int j=0;j<m;++j) 47 { 48 t=answer[j]-‘A‘+1; 49 num[j][t]++; 50 if(num[j][t]>maxmum[j]) maxmum[j]=num[j][t]; 51 } 52 } 53 int ans=0; 54 for(int i=0;i<m;++i) 55 { 56 t=read(); 57 ans+=t*maxmum[i]; 58 } 59 cout<<ans<<endl; 60 }
思维
1、总和需为偶数
2、最大值不大于其余n-1个数的和
1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector> 10 #include<string> 11 #include<cmath> 12 #include<ctime> 13 #include<stack> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define random(a,b) (rand()%(b-a+1)+a) 19 #define ll long long 20 #define ull unsigned long long 21 #define e 2.71828182 22 #define Pi acos(-1.0) 23 #define ls(rt) (rt<<1) 24 #define rs(rt) (rt<<1|1) 25 #define lowbit(x) (x&(-x)) 26 using namespace std; 27 const int MAXN=1e5+5; 28 ll a[MAXN],sum[MAXN],n; 29 ll read() 30 { 31 ll s=1,x=0; 32 char ch=getchar(); 33 while(!isdigit(ch)) {if(ch==‘-‘) s=-1;ch=getchar();} 34 while(isdigit(ch)) {x=10*x+ch-‘0‘;ch=getchar();} 35 return x*s; 36 } 37 int main() 38 { 39 n=read(); 40 for(int i=1;i<=n;++i) 41 a[i]=read(); 42 sort(a+1,a+n+1); 43 ll sum=0; 44 for(int i=1;i<n;++i) 45 sum+=a[i]; 46 if(sum>=a[n]&&(sum+a[n])%2==0) 47 cout<<"YES\n"; 48 else cout<<"NO\n"; 49 return 0; 50 }
也有点贪心的味道
1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector> 10 #include<string> 11 #include<cmath> 12 #include<ctime> 13 #include<stack> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define random(a,b) (rand()%(b-a+1)+a) 19 #define ll long long 20 #define ull unsigned long long 21 #define e 2.71828182 22 #define Pi acos(-1.0) 23 #define ls(rt) (rt<<1) 24 #define rs(rt) (rt<<1|1) 25 #define lowbit(x) (x&(-x)) 26 using namespace std; 27 const int MAXN=2e5+5; 28 ll a[MAXN]; 29 ll read() 30 { 31 int s=1,x=0; 32 char ch=getchar(); 33 while(!isdigit(ch)) {if(ch==‘-‘) s=-1;ch=getchar();} 34 while(isdigit(ch)) {x=10*x+ch-‘0‘;ch=getchar();} 35 return x*s; 36 } 37 int main() 38 { 39 int n=read(),k=read(); 40 for(int i=1;i<=n;++i) a[i]=read(); 41 sort(a+1,a+n+1); 42 int mid=n/2+1; 43 44 int ans=a[mid]; 45 46 for(int i=mid+1;i<=n;++i)//将a[mid]到a[i-1]补刀a[i] 47 { 48 ll need=(a[i]-a[i-1])*(i-mid); 49 //cout<<need<<endl; 50 if(k>=need) 51 { 52 k-=need; 53 ans=a[i]; 54 } 55 else 56 { 57 ans+=k/(i-mid); 58 k=0; 59 break; 60 } 61 } 62 ans+=k/mid; 63 cout<<ans<<endl; 64 }
有看到别人用二分写。如果最优值类问题实在想不出可以试试二分,主要是写出判断函数。
二分:
1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector> 10 #include<string> 11 #include<cmath> 12 #include<ctime> 13 #include<stack> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define random(a,b) (rand()%(b-a+1)+a) 19 #define ll long long 20 #define ull unsigned long long 21 #define e 2.71828182 22 #define Pi acos(-1.0) 23 #define ls(rt) (rt<<1) 24 #define rs(rt) (rt<<1|1) 25 #define lowbit(x) (x&(-x)) 26 using namespace std; 27 const int MAXN=2e5+5; 28 ll a[MAXN],n,m; 29 ll read() 30 { 31 int s=1,x=0; 32 char ch=getchar(); 33 while(!isdigit(ch)) {if(ch==‘-‘) s=-1;ch=getchar();} 34 while(isdigit(ch)) {x=10*x+ch-‘0‘;ch=getchar();} 35 return x*s; 36 } 37 bool C(ll k)//a[mid]为k是否ok 38 { 39 if(a[n/2+1]>=k) return true; 40 ll need=0; 41 for(int i=n/2+1;i<=n;++i) 42 { 43 if(a[i]<k) 44 { 45 need+=k-a[i]; 46 if(need>m) return false; 47 } 48 } 49 return true; 50 } 51 int main() 52 { 53 n=read(),m=read(); 54 for(int i=1;i<=n;++i) a[i]=read(); 55 if(n==1) 56 { 57 cout<<a[1]+m<<endl; 58 return 0; 59 } 60 sort(a+1,a+n+1); 61 ll l=-1,r=a[n/2+1]+m+233; 62 while(l+1!=r) 63 { 64 ll mid=(l+r)>>1; 65 if(C(mid)) l=mid; 66 else r=mid; 67 } 68 cout<<l<<endl; 69 return 0; 70 }
原文地址:https://www.cnblogs.com/wangzhebufangqi/p/11333408.html
时间: 2024-10-11 17:37:56