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.如 ...
随机推荐
- IO管理与磁盘调度
- 使用脚本删除ios工程中未使用图片
使用脚本删除ios工程中未使用图片 最近在读唐巧大神的<iOS开发进阶>,学到了一个大招:使用脚本删除ios中未使用的图片(纸书上有点小问题,参考github上的issue:使用脚本删除i ...
- Arcgis Engine(ae)接口详解(2):featureClass查询
//属性查询~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //IQueryFilter代表查询条件,QueryFilterClass代表只限于属性查询(就是没有空间查询) ...
- Unity3D集成腾讯语音GVoice SDK
友情提示:最近发现腾讯GVoice有另一个官网,叫做腾讯游戏服务,经过对比发现这个网站才是最新的,下面我介绍的那个估计是已经废弃的,但不知道为啥老的网站没有直接链接到新网址而是仍然保留了.不过新官网的 ...
- 树的深度优先遍历和广度优先遍历的原理和java实现代码
import java.util.ArrayDeque; public class BinaryTree { static class TreeNode{ int value; TreeNode le ...
- react native 中的ListView
ListView 的运用: 1.首先在react native中引入这个组件: 2.初始化的ListView 的相关属性: constructor(props) { super(props); con ...
- Awesome Adb——一份超全超详细的 ADB 用法大全【转】
本文转载自:https://juejin.im/entry/57c00fe4c4c971006179838a ADB,即 Android Debug Bridge,它是 Android 开发/测试人员 ...
- 域名ip自动跳转 跳转指定页面的js
域名ip自动跳转 跳转指定页面的js 为了应对百度审核,需要客户的网站在个别地区跳转到另一个页面,就搞到了这段代码,屡试不爽,超实用.下面把地址换成你要访问的网站url地址或者文件url地址即可.超实 ...
- 类、对象(java基础知识六)
1.Java约定俗成 java约定俗成 1,类名接口名 一个单词首字母大写,多个单词每个单词首字母都大写 2,方法名和变量名 一个单词全部小写,多个单词从第二个单词首字母大写 建议:如果能用英语尽量用 ...
- 如何反编译silverlight
@years(945060991) 15:10:28问一下 如何反编译silverlight观,一世沧桑如画♥(752816388) 15:10:46解压就行@years(945060991) ...