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.如 ...
随机推荐
- Intel HEX文件解析
近期有一个需求就是为Arduino开发板做一个基于蓝牙的无线烧录程序.眼下的Arduino程序都是通过USB线连接到电脑的主机上,实际的传输过程是基于USB协议的,这个过程还是比較麻烦的.由于每次的编 ...
- C#高阶与初心:(二)P/Invoke平台调用
最近某个项目要采集交易终端的信息用于监管,主要厂商给出了API,C++版的...开启hard模式!!! C#调用C++的DLL基本就两种方法:加一个VC++项目包一层,或者使用P/Invoke(平台调 ...
- AOP和OOP的区别
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP与OOP是面向不同领域的两种设计思想. ...
- solr单机多实例部署文件锁冲突解决的方法
给出一个有问题的单机多tomcat实例引用同一个solr实例部署图. 这样的部署必定造成一个问题.启动第二个tomcat实例时,一定会报索引目录文件锁已经被占用. 最初的解决的方法是.有多少个tomc ...
- Mac 上VitrualBox安装CentOS6.5 调整root分区的大小
安装centOS的时候由于选择了动态调整磁盘大小.所以分配磁盘空间的时候就没多想,直接用的default的8G,以为不够了自己会调整,没想到是个大坑 发现提示空间不足的时候root仅仅有0k了.... ...
- 浅谈 React、Flux 与 Redux
React React 是一个 View 层的框架,用来渲染视图,它主要做几件事情: 组件化利用 props 形成单向的数据流根据 state 的变化来更新 view利用虚拟 DOM 来提升渲染性能 ...
- (24) java web的struts2框架的使用-action参数自动封装与类型转换
structs可以对参数进行自动封装,做法也很简单. 一,action参数自动封装: 1,可以直接在action类中,声明public的属性,接受参数. 2,属性也是是private,如果是priva ...
- 如何查看一个Application是32位的还是64位的?
使用process explorer查看,找到对应的进程. 注册表的路径是Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\ 使用powershell查 ...
- jLim - 紧凑的 JavaScript 框架
1. [代码][JavaScript]代码 /*! * jLim Core * * jLim is a small JavaScript base code. It can be used t ...
- hash与map的区别联系应用(转)
一,hashtable原理: 哈希表又名散列表,其主要目的是用于解决数据的快速定位问题.考虑如下一个场景. 一列键值对数据,存储在一个table中,如何通过数据的关键字快速查找相应值呢?不要告诉我一个 ...