B题题意:

给你n个物品的价格,你需要找出来一个值b,使得每一个物品与这个b的差值的绝对值小于k。找到最大的b输出,如果找不到,那就输出-1

题解:

很简单嘛,找到上下限直接二分。下限就是所有物品中最小的价格。上限就是所有物品中最大价格加上k

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<map>
6 #include<math.h>
7 using namespace std;
8 typedef long long ll;
9 const int maxn=1e5+5;
10 const int mod=26;
11 const int INF=0x3f3f3f3f;
12 const int block=300;
13 int main()
14 {
15 int t;
16 scanf("%d",&t);
17 while(t--)
18 {
19 int b,n,k,minn=INF,maxx=0;
20 scanf("%d%d",&n,&k);
21 for(int i=1;i<=n;++i)
22 {
23 scanf("%d",&b);
24 minn=min(minn,b);
25 maxx=max(maxx,b);
26 }
27 int mid=(minn+maxx)/2;
28 if(mid-minn<=k && maxx-mid<=k)
29 {
30 int ans=mid,m,l=mid,r=maxx+k;
31 while(l<=r)
32 {
33 m=(l+r)/2;
34 if(m-minn<=k)
35 {
36 ans=m;
37 l=m+1;
38 }
39 else r=m-1;
40 }
41 printf("%d\n",ans);
42 }
43 else printf("-1\n");
44 }
45 return 0;
46 }

c题题意:

最开始有k电量,你需要玩n个回合的游戏,每一回合你可以有两个选择

1、玩游戏,但是要消耗a电量

2、边充电边玩,消耗b电量

还有一个限制:如果电量小于等于a,那就不可以玩游戏;如果电量小于等于b那就不可以边充边玩

如果玩游戏玩不了n个回合,就输出-1,否则就输出你玩游戏的次数(可不是边充边玩)

题解:

思路很容易想,但是就是实现难!

你可以先n个回合都选择第二种模式来玩游戏,看电量够不够。如果不够就直接输出-1

这里判断就可以先玩n-1个回合,看看剩下的电量大不大于b。如果大于那就证明可以玩够n个回合,否则就不可以

因为电量必须大于b才能边充边玩

然后就二分 选择了几次第一个选择(即消耗电量为a) ,每一次的判断都类似上面的判断方式

还有就是要用long long

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<map>
6 #include<math.h>
7 using namespace std;
8 typedef long long ll;
9 const int maxn=1e5+5;
10 const int mod=26;
11 const int INF=0x3f3f3f3f;
12 const int block=300;
13 ll k,n,a,b;
14 bool check(ll x)
15 {
16 ll cha,ci;
17 if(x)
18 {
19 if(k-(a*(x-1))<=a) return 0;
20 }
21
22 cha=k-a*x;
23 ci=n-x;
24 if(ci==0) return 1;
25 if(cha-(b*(ci-1))<=b) return 0;
26 else return 1;
27 }
28 int main()
29 {
30 ll t;
31 cin>>t;
32 while(t--)
33 {
34 cin>>k>>n>>a>>b;
35 if(k-(b*(n-1))<=b)
36 {
37 printf("-1\n");
38 continue;
39 }
40 else
41 {
42 ll l=0,r=n,m,ans=0;
43 while(l<=r)
44 {
45 m=(l+r)>>1;
46 if(check(m))
47 {
48 ans=m;
49 l=m+1;
50 }
51 else r=m-1;
52 }
53 printf("%I64d\n",ans);
54 }
55 }
56 return 0;
57 }

D题题意:

给你n个糖果,每一个糖果有一个种类,你需要用这些不同种类的糖果来弄成一个礼物

但是这个礼物里面各 种类 糖果的数量不能相同,你需要找出来这个礼物最多能由多少糖果构成

题解:

你会发现,如果一种糖果的个数是n,那么它可以拿出n-1、n-2、n-3来放入礼物中

注释+代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<map>
6 #include<math.h>
7 using namespace std;
8 typedef long long ll;
9 const int maxn=2e5+5;
10 const int mod=26;
11 const int INF=0x3f3f3f3f;
12 const int block=300;
13 int v[maxn],w[maxn];
14 int main()
15 {
16 int t;
17 scanf("%d",&t);
18 while(t--)
19 {
20 int n,a,maxx=0;
21 scanf("%d",&n);
22 for(int i=1;i<=n;++i) //这里不能用memset,要不然会超时
23 v[i]=w[i]=0;
24 for(int i=1;i<=n;++i)
25 {
26 scanf("%d",&a);
27 w[v[a]]--; //w是记录有多少种糖果且这种糖果的数量是v[a]
28 v[a]++; //v是记录每一种糖果的数量
29 w[v[a]]++;
30 //printf("%d %d %d\n",a,v[a],w[v[a]]);
31 maxx=max(maxx,v[a]);
32 }
33 int ans=0,sum=0;//printf("%d**\n",maxx);
34 for(int i=maxx;i>0;--i)
35 {
36
37 ans+=w[i];
38 if(ans>0) //如果你有一种糖果数量是5,此时这个可以来拿出来4个,来填充
39 //礼物,也可以都拿出来,这要看有没有其他种类的糖果也有这个数量个糖果
40 sum+=i,ans--;
41 }
42 printf("%d\n",sum);
43 }
44 return 0;
45 }

E题题意:

给你一个长为n的字符串,你需要从中找出来k个不重复的子序列(注意是子序列而不是子串。子序列不需要连续,可以再原字符串中间删除字母)。如果可以找到输出你删除的总字母个数(你要是这个值尽可能地小),如果不能输出-1

题解:

如果直接暴力dfs,那就相当于2^100,肯定会超时,那我们注意到k的值比较小,所有我们就可以找到k个子序列就可以了。又因为题目有要求输出的值尽量小。所以我们就可以按删除字符从小到大来枚举

我这里采用set去重

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<map>
6 #include<math.h>
7 #include<set>
8 #include<queue>
9 using namespace std;
10 typedef long long ll;
11 const int maxn=2e5+5;
12 const int mod=26;
13 const int INF=0x3f3f3f3f;
14 const int block=300;
15
16 int main()
17 {
18 int n,k,ans=0;
19 queue<string>r;
20 set<string>str;
21 string s;
22 cin>>n>>k;
23 cin>>s;
24 r.push(s);
25 str.insert(s);
26 while(!r.empty() && str.size()<k)
27 {
28 string ss=r.front();
29 r.pop();
30 int len=ss.size();
31 //printf("%d %d**\n",ss.length(),ss.size());
32 for(int i=0;i<len;++i)
33 {
34 string fresh=ss;
35 fresh.erase(i,1);
36 //cout<<fresh<<"**"<<endl;
37 if(!str.count(fresh) && str.size()<k)
38 {
39 //cout<<1<<endl;
40 r.push(fresh);
41 str.insert(fresh);
42 ans+=n-fresh.size();
43 if(str.size()==k) break;
44 }
45 }
46 }
47 if(str.size()<k)
48 printf("-1\n");
49 else printf("%d\n",ans);
50 }

Codeforces Round #570 (Div. 3) B. Equalize Prices、C. Computer Game、D. Candy Box (easy version)、E. Subsequences (easy version)的更多相关文章

  1. Codeforces Round #570 (Div. 3) B. Equalize Prices

    原文链接https://codeforces.com/contest/1183/problem/B 题意:进行Q组测试,在每组中有长度为n的数组a[i],然后现在给你一个K,问你找到一个bi使得|ai ...

  2. Codeforces Round #590 (Div. 3) A. Equalize Prices Again

    链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...

  3. (原创)Codeforces Round #550 (Div. 3) D. Equalize Them All

    D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. Codeforces Round #570 Div. 3

    A:暴力从小到大枚举判断. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 10 ...

  5. Codeforces Round #570 (Div. 3) E. Subsequences (easy version) (搜索,STL)

    题意:有一长度为\(n\)的字符串,要求得到\(k\)不同的它的子序列(可以是空串),每个子序列有\(|n|-|t|\)的贡献,求合法情况下的最小贡献. 题解:直接撸个爆搜找出所有子序列然后放到set ...

  6. Codeforces Round #570 (Div. 3) G. Candy Box (hard version) (贪心,优先队列)

    题意:你有\(n\)个礼物,礼物有自己的种类,你想将它们按种类打包送人,但是打包的礼物数量必须不同(数量,与种类无关),同时,有些礼物你想自己留着,\(0\)表示你不想送人,问你在送出的礼物数量最大的 ...

  7. Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)

    题意:有一组数,可以选择某个数\(a_i\)相邻的一个数\(a_j\),然后可以让\(a_i\)加上或者减去\(|a_i-a_j|\),问最少操作多少次使得数组中所有数相同. 题解:不难发现,每次操作 ...

  8. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. 温故而知新--day2

    温故而知新--day2 类 类与对象 类是一个抽象的概念,是指对现实生活中一类具有共同特征的事物的抽象.其实列化后称为对象.类里面由类属性组成,类属性可以分为数据属性和函数属性(函数属性又称为类方法) ...

  2. --safe-user-create

    此参数如果启用,用户将不能用grant语句创建新用户,除非用户有mysql数据库中user表的insert权限, ./mysqld_safe  --safe-user-create & 用-- ...

  3. JavaScript入门-对象

    js对象 本篇主要介绍js里如何创建对象,以及for循环访问对象的成员... 什么是对象? 对象,并不是中文里有男女朋友意思,它是从英文里翻译来的,英文叫[Object],目标,物体,物品的意思. 在 ...

  4. 电子邮箱、邮件地址、网站地址正则表达式!几个有用的RE、regex、regexp!

    几个常用的正则表达式! r"\w[-\w\.]*@\w[-\w]*(\.\w[-\w]*)+" 这个是电子邮件地址的. r"<TAG\b[^>]*<(. ...

  5. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  6. 判断最长回文串——暴力、延展、Manacher

    1. 暴力 时间复杂度O(n^3). 2. 延展 以某一字符为中心,设置left, right两个变量同时向外扩,判断他们指向字符是否相同.注意分奇偶讨论.时间复杂度O(n^2). 3. Manach ...

  7. MySQL中UPDATE语句里SET后使用AND的执行过程和结果分析

    使用SQL中的UPDATE关键字更新多个字段值时,SET后面的更新字段应该使用逗号而不能用AND.虽然用AND不会报错,但会使更新结果错误,下面我将通过场景来分析当我们使用AND时SQL的执行过程和为 ...

  8. 2021年【线上】lammps分子动力学技术实战培训班

    材料模拟分子动力学课程 3月19号--22号 远程在线课 lammps分子动力学课程 3月12号--15号 远程在线课 第一性原理VASP实战课 3月25号-28号 远程在线课 量子化学Gaussia ...

  9. Spring AOP介绍与使用

    Spring AOP介绍与使用 AOP:Aspect Oriented Programming 面向切面编程 OOP:Object Oriented Programming 面向对象编程 ​ 面向切面 ...

  10. 三次握手 四次握手 原因分析 TCP 半连接队列 全连接队列

    小结 1. 三次握手的原因:保证双方收和发消息功能正常: [生活模型] "请问能听见吗""我能听见你的声音,你能听见我的声音吗" [原理]A先对B:你在么?我在 ...