Codeforces Round #577 (Div. 2) (A、B、C)

A、Important Exam

简单贪心即可

 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 }

B、 Zero Array

思维

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 }

C、 Maximum Median

也有点贪心的味道

 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-08-04 05:11:04

Codeforces Round #577 (Div. 2) (A、B、C)的相关文章

Codeforces Round #302 (Div. 2) -- (A,B,C)

题目传送:Codeforces Round #302 (Div. 2) A. Set of Strings 思路:注意开头字母都不相同 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include &

Codeforces Round #575 (Div. 3) (A. Three Piles of Candies)(数学)

A. Three Piles of Candies time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as poss

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Codeforces Round #259 (Div. 2) (序列)

题目链接:http://codeforces.com/contest/454/problem/B B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a se

Codeforces Round #253 (Div. 1) (A, B, C)

Codeforces Round #253 (Div. 1) 题目链接 A:给定一些牌,然后现在要提示一些牌的信息,要求提示最少,使得所有牌可以被分辨出来. 思路:一共2^10种情况,直接暴力枚举,然后对于每种情况去判断,判断的时候只要两两张牌枚举出来判断即可.不得不说CF机子真心强大,2秒限制还是能跑10^8 B:给定n个发生概率,求选出其中一些事件,使得正好有一件发生的概率最大. 思路:贪心,从大到小排序概率,然后一个个概率进来判断有没有更大,有就加入该事件,没有就跳过 C:给定n个数字,要

Codeforces Round #301 (Div. 2) -- (A,B,C,D)

题目传送:Codeforces Round #301 (Div. 2) A. Combination Lock 水题,求最小移动次数,简单贪心一下即可 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #i

Codeforces Round #480 (Div. 2) C 贪心 D 数字、思维 E 树上倍增

Codeforces Round #480 (Div. 2) C. Posterized 题意: 给出 n 个数,都是区间 [0,255] 内的数,要你把 [0,255] 划分成多个长度 <=k 的不重叠的子区间.每个数必须包含在一个子区间内,且这个数的价值是这个子区间的左端点.要你输出这 n 数的价值,且这 n 个价值字典序要最小. tags: 首先很明显字典序最小,那对于第 i 个数 p[i] 定它的区间时,左端点肯定要尽可能小.所以我们直接枚举区间 [ p[i]-k+1, p[i] ] 定

Codeforces Round #315 (Div. 2) (ABCD题)

A. Music 题意: 一首歌长度为S秒,已经下载了T秒,下载速度为每q秒的现实时间能下载下来(q-1)秒 的歌曲.现在开始听歌,如果听到还没下载的地方就从0秒的地方开始replay,求一首歌听完需要从0秒听几次(包括一开始那次) 思路: 我们可以用路程-时间的思路来考虑这道题. 假设两位选手"播放"与"下载","播放"的起点是0m处,"下载"的起点是Tm处,终点在Sm处,"播放"的速度是1m/s,&qu

Codeforces Round #344 (Div. 2)(按位或运算)

Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following probl