第一次CF祭==

由于太菜了只做了前两题==

因为在第一题上耗费时间太多了,我还是太菜了==。

A. Benches

time limit per test

1 second

memory limit per test

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.

Input

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.

Output

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

time limit per test

2 seconds

memory limit per test

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.

Input

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.

Output

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的更多相关文章

  1. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...

  2. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  3. Codeforces Round #510 (Div. 2) B. Vitamins

    B. Vitamins 题目链接:https://codeforces.com/contest/1042/problem/B 题意: 给出几种药,没种可能包含一种或多种(最多三种)维生素,现在问要吃到 ...

  4. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  5. Codeforces Round #510 (Div. 2)(C)

    传送门:Problem C https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 给你n个数,定义有两种操作 ① 1 i j : (i != ...

  6. Codeforces Round #510 (Div. 2)(B)

    传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 如果可以通过喝果汁将维生素A,B,C全部摄取,求最小花费,如 ...

  7. Codeforces Round #510 (Div. 2)(A)

    传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 公园里有n个沙滩,a[i]表示第i个沙滩初始人数,现有m个人 ...

  8. codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)

    题意:给出一个数组,求其中和小于t的区间数. 先计算前缀和数组sum[i].对当前的sum[i],查询树状数组中有几个比(sum[i]-t)大的数,那么用sum[i]减它就是一个合法区间.再将当前的s ...

  9. codeforces 1042c// Array Product// Codeforces Round #510(Div. 2)

    题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如 ...

随机推荐

  1. Python爬取韩寒所有新浪博客

    接上一篇,我们依据第一页的链接爬取了第一页的博客,我们不难发现,每一页的链接就仅仅有一处不同(页码序号),我们仅仅要在上一篇的代码外面加一个循环,这样就能够爬取全部博客分页的博文.也就是全部博文了. ...

  2. (WPF)Storyboard

    Storyboard是一个为其所包括的动画提供目标信息的容器. 除非动画放在Storyboard中,负责不能在XMAL中被实例化. BeginStoryboard通过将Storyboard加入到触发器 ...

  3. winfrom桌面程序调用python解释器

    Winfrom桌面程序调用python解释器执行py脚本后台执行完成具体的功能,为什么要这样处理呢?因为我现在的大部分过项目都是后台的脚本处理,界面基本的输入完成之后,将参数按照规则传入到脚本的入口, ...

  4. C++ 四种强制类型转变与区别之处

    使用标准C++的类型转换符:static_cast.dynamic_cast.reinterpret_cast和const_cast.1.static_cast    用法:static_cast&l ...

  5. inherited在消息中的作用(编译器根据inherited所在的函数,直接转换成对祖先类同名动态函数的调用,或者转换成对DefaultHandler的调用)

    好奇一下.看来Object Pascal确实与Windows深入结合了. unit Unit1; interface uses Windows, Messages, SysUtils, Variant ...

  6. vscode部分文件夹无法打开

    vscode部分文件夹无法打开,无法正常显示 解决方案:关闭该IDE.找到C:\Users\XX\AppData\Roaming\Code,将Code文件夹删除.重新打开vsCode,即可恢复.但是以 ...

  7. @SpringBootApplication 注解

    @SpringBootApplication 启动类注解(一般在root下):该注解被@Configuration.@EnableAutoConfiguration.@ComponentScan 这三 ...

  8. 取clientdataset detal中的 更新数据, 将detal 转 数据库脚本sql

    转自永南博客,更改update 脚本只取变化字段,更改排除blob与数组字段,这两个类型会报错 function vartosql(value: Variant): wideString; var  ...

  9. HDU3746 Cyclic Nacklace —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    M ...

  10. java多线程实现简单队列

    1.创建Queue.java public class Queue { private LinkedList<Object> list = new LinkedList<Object ...