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.如 ...
随机推荐
- Posix信号量操作函数
Posix信号量: 分类: Posix有名信号量:使用Posix IPC名字标识,可用于线程或进程间同步Posix基于内存的信号量:存放在共享内存区中,可用于进程或线程间的同步 sem_open(). ...
- 常用shell命令的写法
这并不是教人怎么进行shell编程的文章,只是韦哥在工作中用到的一些简单脚本的写法.因为有些命令即使用过几次了,再次使用时仍然写不对,需要man来看下或者需要google,你也可以理解为对命令的理解不 ...
- hibernate工作原理及作用
转载自 http://www.cnblogs.com/dashi/p/3597969.html#commentform JAVA Hibernate工作原理及为什么要用 hibernate 简介:hi ...
- SpringMVC实战(注解)
1.前言 前面几篇介绍了SpringMVC中的控制器以及视图之间的映射方式,这篇来解说一下SpringMVC中的注解,通过注解能够非常方便的訪问到控制器中的某个方法. 2.配置文件配置 2.1 注解驱 ...
- poj 3017 Cut the Sequence(单调队列优化DP)
Cut the Sequence \(solution:\) 这道题出的真的很好,奈何数据水啊! 这道题当时看得一脸懵逼,说二分也不像二分,说贪心也不像贪心,说搜索吧这题数据范围怎么这么大?而且这题看 ...
- Java语言基础二
1.常量的概述和使用 A:什么是常量 B:Java中常量的分类 常量分类为六种:a.”字符串” b.’字符’ c.整数 d.小数 e.boolern(布尔类型) 返回值为 FALSE和TRUE ...
- html5--3.2 input元素(1)
html5--3.2 input元素(1) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 i ...
- TRIZ发明问题解决理论——本质是分析问题中的矛盾,利用资源(时间空间物质能量功能信息等)来解决矛盾从而解决问题——抽象出来:问题是什么,为什么?
TRIZ意译为发明问题的解决理论.TRIZ理论成功地揭示了创造发明的 内在规律和原理,着力于澄清和强调系统中存在的矛盾,其目标是完全解决矛盾,获得最终的理想解.它不是采取折衷或者妥协的做法,而且它是基 ...
- trying to draw too large(106,975,232 bytes) bitmap.
Loading Large Bitmaps Efficiently This lesson teaches you to Read Bitmap Dimensions and Type Load a ...
- 【CAIOJ1177】 子串是否出现
[题目链接] 点击打开链接 [算法] KMP [代码] #include<bits/stdc++.h> using namespace std; #define MAXA 1000010 ...