Codeforces Round #510 (Div. 2) A&B By cellur925
第一次CF祭==
由于太菜了只做了前两题==
因为在第一题上耗费时间太多了,我还是太菜了==。
A. Benches
1 second
256 megabytes
There are nn benches in the Berland Central park. It is known that aiai people are currently sitting on the ii-th bench. Another mm people are coming to the park and each of them is going to have a seat on some bench out of nn available.
Let kk be the maximum number of people sitting on one bench after additional mm people came to the park. Calculate the minimum possible kk and the maximum possible kk.
Nobody leaves the taken seat during the whole process.
The first line contains a single integer nn (1≤n≤100)(1≤n≤100) — the number of benches in the park.
The second line contains a single integer mm (1≤m≤10000)(1≤m≤10000) — the number of people additionally coming to the park.
Each of the next nn lines contains a single integer aiai (1≤ai≤100)(1≤ai≤100) — the initial number of people on the ii-th bench.
Print the minimum possible kk and the maximum possible kk, where kk is the maximum number of people sitting on one bench after additional mm people came to the park.
一句话题意:对一个确定的数列增加元素,求增加元素后序列最大值k的最大值和最小值
观察样例发现,k的最大值一定是把所有元素加到数列中最大值的结果。
那么最小值呢?几乎40分钟都在思考这个,从二分想到平均数云云,都不对,后来突然想到,我们从宏观来审视这个问题,因为增加的元素是全部都要进数列的,所以我们求出最后数列的全部元素总和,除以数列项数即可。(看起来很小学奥数的算法结果想了很久。。。)
于是愉快的交,几分钟后就被hack了,非常不爽...原因写在了注释里。还是考虑情况不全面。
#include<cstdio>
#include<algorithm> using namespace std; int n,m,maxans,sum,minans;
int a[]; int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
maxans=max(maxans,a[i]);
sum+=a[i];
}
sum+=m;
minans=max(maxans,(sum+n-)/n);
//结果肯定不能比maxans小
// 面对不能整除的情况我们+1,然后为了让结果具有普适性,所以+n/n,但是又怕在整除的时候加多,所以-1
printf("%d %d",minans,maxans+m);
return ;
}
B. Vitamins
2 seconds
256 megabytes
Berland shop sells nn kinds of juices. Each juice has its price cici. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin "A", vitamin "B" and vitamin "C". Each juice can contain one, two or all three types of vitamins in it.
Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.
The first line contains a single integer nn (1≤n≤1000)(1≤n≤1000) — the number of juices.
Each of the next nn lines contains an integer cici (1≤ci≤100000)(1≤ci≤100000) and a string sisi — the price of the ii-th juice and the vitamins it contains. String sisi contains from 11 to 33 characters, and the only possible characters are "A", "B" and "C". It is guaranteed that each letter appears no more than once in each string sisi. The order of letters in strings sisi is arbitrary.
Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.
本题显然是背包类的dp了,但是我开始竟然写了模拟(???),后来发现7种情况可能不全有。。我太菜了。
看到两种好的做法:Chemist的,码量较大,但是很容易理解。
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; int n;
int price[],val[];
char Chemist[];
int dp[][][][]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&price[i]);
scanf("%s",Chemist+);
for(int j=;j<=strlen(Chemist+);j++)
{
if(Chemist[j]=='A') val[i]+=;
if(Chemist[j]=='B') val[i]+=;
if(Chemist[j]=='C') val[i]+=;
}
}
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
dp[i][][][]=;//两个初值操作,必须有。
for(int i=;i<=n;i++)
{
for(int j=;j<=;j++)
for(int l=;l<=;l++)//继承之上,必须有。
for(int r=;r<=;r++)
dp[i][j][l][r]=dp[i-][j][l][r];
if(val[i]==)
{
for(int j=;j<=;j++)
for(int l=;l<=;l++)
for(int r=;r<=;r++)
dp[i][][][]=min(dp[i-][j][l][r]+price[i],dp[i][][][]);
}
else if(val[i]==)
{//VITAMIN A可能有或没有 分开考虑
for(int j=;j<=;j++)
for(int l=;l<=;l++)//从上一个转移来,就是带上当前的了,也就是决策
dp[i][][][]=min(dp[i-][][j][l]+price[i],dp[i][][][]);
for(int j=;j<=;j++)
for(int l=;l<=;l++)
dp[i][][][]=min(dp[i-][][j][l]+price[i],dp[i][][][]);
}
else if(val[i]==)
{
for(int j=;j<=;j++)
for(int l=;l<=;l++)
dp[i][][][]=min(dp[i-][j][][l]+price[i],dp[i][][][]);
for(int j=;j<=;j++)
for(int l=;l<=;l++)
dp[i][][][]=min(dp[i-][j][][l]+price[i],dp[i][][][]);
}
else if(val[i]==)
{
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
}
else if(val[i]==)
{
for(int j=;j<=;j++)
for(int l=;l<=;l++)
dp[i][][][]=min(dp[i-][j][l][]+price[i],dp[i][][][]);
for(int j=;j<=;j++)
for(int l=;l<=;l++)
dp[i][][][]=min(dp[i-][j][l][]+price[i],dp[i][][][]);
}
else if(val[i]==)
{
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
}
else if(val[i]==)
{
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
dp[i][][][]=min(dp[i][][][],dp[i-][][][]+price[i]);
}
}
int ans=1e9;
for(int i=;i<=n;i++)
//从每一个地方寻找!! 因为决策可能不在最后
ans=min(ans,dp[i][][][]);
if(ans==1e9) printf("-1");
else printf("%d",ans);
return ;
}
L_A的,精简代码,巧妙运用位运算,神仙做法。
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int i,j,n,f[],c,t,len;
char vt[]; int main()
{
scanf("%d",&n);
memset(f,0x3f,sizeof(f));
f[]=;
for(i=;i<=n;++i)
{
scanf("%d%s",&c,vt+);
len=strlen(vt+);
t=;
for(j=;j<=len;++j)
switch(vt[j])
{
case 'A':
t|=;
break;
case 'B':
t|=;
break;
case 'C':
t|=;
break;
}
for(j=;j<=;++j)
f[t|j]=min(f[t|j],f[j]+c);
}
if(f[]==0x3f3f3f3f) printf("-1");
else printf("%d",f[]);
return ;
}
二位神仙都用了1,2,4这类二进制表示的方法,我开始也想用的,只不过用了1,2,3就会gg了,因为相加后可能会有重复,用2^n的数字表示,肥肠好。
Codeforces Round #510 (Div. 2) A&B By cellur925的更多相关文章
- Codeforces Round #510 (Div. 2)
Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- Codeforces Round #510 (Div. 2) B. Vitamins
B. Vitamins 题目链接:https://codeforces.com/contest/1042/problem/B 题意: 给出几种药,没种可能包含一种或多种(最多三种)维生素,现在问要吃到 ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #510 (Div. 2)(C)
传送门:Problem C https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 给你n个数,定义有两种操作 ① 1 i j : (i != ...
- Codeforces Round #510 (Div. 2)(B)
传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 如果可以通过喝果汁将维生素A,B,C全部摄取,求最小花费,如 ...
- Codeforces Round #510 (Div. 2)(A)
传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 公园里有n个沙滩,a[i]表示第i个沙滩初始人数,现有m个人 ...
- codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)
题意:给出一个数组,求其中和小于t的区间数. 先计算前缀和数组sum[i].对当前的sum[i],查询树状数组中有几个比(sum[i]-t)大的数,那么用sum[i]减它就是一个合法区间.再将当前的s ...
- codeforces 1042c// Array Product// Codeforces Round #510(Div. 2)
题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如 ...
随机推荐
- Distributed Management Task Force----分布式管理任务组
http://baike.baidu.com/link?url=Y9HGLs8Qj6pXbbgY6xPdfiGDsQO8Eu1e80B4giQtQ_hAfGNF59byxnLoERYri4Duw7Gw ...
- php利用cookie防止重复提交解决办法
原理:如果数据通过了上边的两次验证,说明数据是合法有效的数据,这时候我们把提交的数据串接为一个字符串,并用MD5加密后得到一个MD5的值. 接着我们把这个值通过Cookie放进客户端,当用户下一次提交 ...
- 2016/06/13 phpexcel 未完待续
①准备工作: 1,php版本不能太低 2,去官网下载PHPExcel插件 http://phpexcel.codeplex.com/ 3,解压后提取classes文件夹到工作目录,并重命名为PH ...
- All the best open source and Software as a Service (SaaS) tools in one place 工具 工欲善其事必先利其器
Open Source & SaaS Tools | StackShare https://stackshare.io/categories AfterShip/SaaS: List of S ...
- mongo-java-driver
http://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.5.0 <!-- https://mvnrepository. ...
- easyui tree的简单使用
Tree 数据转换 所有节点都包含以下属性: id:节点id,这个很重要到加载远程服务器数据 which is important to load remote data text: 显示的节点文本 ...
- 教你如何配置Ubuntu用于高效、高质量的发送邮件
本文首发在: http://mengxi.me/how-to-setup-ubuntu-sendmail-to-deliver-email-fast-and-reliable/ 在网站上线后,经常会遇 ...
- 深度学习入门-4.1 AND.py 源码分析
源代码 ------------------------------------------------------------------------------------------------ ...
- MYSQL进阶学习笔记七:MySQL触发器的创建,应用及管理!(视频序号:进阶_16,17)
知识点八:MySQL触发器的应用(16,17) 触发器的定义: 什么是触发器: 触发器是一种特殊的存储过程,它在插入,删除或修改特定表中的数据是触发执行,他比数据库本身标准的功能有更精细和更复杂的数据 ...
- 书写优雅的shell脚本(三) - shell中exec解析
参考:<linux命令.编辑器与shell编程> <unix环境高级编程> exec和source都属于bash内部命令(builtins commands),在bash下输入 ...