csacademy Round #36(模拟+最坏情况)
传送门
题意
给出n种袜子,每种袜子个数a[i],两只相同种类袜子配成一对,询问至少拿出多少只袜子能确保配出k对袜子
分析
In order to find out the minimum number of socks needed, we should analyse the worst possible scenario.
This happens when we first take one sock of each color. Next, whatever color we choose, we're going to make a pair. But if the next sock is also of the same color, we won't be able to make a new pair. Any other color, and we'd be able to make a second pair. So in our worst case analysis, we can always assume that we take two consecutive socks of the same color, while possible.
If we run out of colors with two available socks, but we still can't make \(K\) pairs, we will start choosing the socks left, until we're able to make \(K\) pairs.
trick
1.注意一开始统计的时候会爆int
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
//#pragma comment(linker, "/STACK:102400000,102400000")
//inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}}
int n;
ll k;
ll a[100100];
ll avaliable_pairs,pairs,total;
int main()
{
cin>>n>>k;
F(i,1,n)
{
cin>>a[i];
avaliable_pairs+=a[i]/2;
a[i]--;
}
if(avaliable_pairs<k) puts("-1");
else
{
total+=n;
F(i,1,n)
{
if(a[i]>=2)
{
total+=a[i]/2*2;
pairs+=a[i]/2;
}
}
if(pairs>=k) cout<<k*2+n-1<<endl;
else cout<<total+k-pairs<<endl;
}
return 0;
}
csacademy Round #36(模拟+最坏情况)的更多相关文章
- 【Round #36 (Div. 2 only) C】Socks Pairs
[题目链接]:https://csacademy.com/contest/round-36/task/socks-pairs/ [题意] 给你n种颜色的袜子,每种颜色颜色的袜子有ai只; 假设你在取袜 ...
- POJ3783Balls[DP 最坏情况最优解]
Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 907 Accepted: 598 Description T ...
- 一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS.
一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS. 选择算法也就是求一个无序数组中第K大( ...
- HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)
HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...
- CodeForces.158A Next Round (水模拟)
CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...
- k个鸡蛋从N楼层摔,如果确定刚好摔碎的那个楼层,最坏情况下最少要试验x次?
题目 k个鸡蛋从N楼层摔,如果确定刚好摔碎的那个楼层,最坏情况下最少要试验x次? 换个说法: k个鸡蛋试验x次最多可以检测N层楼.计算出N? 逆向思维和数学公式解. 分析 定义N(k,x) 如果第k个 ...
- 7.12 NOI模拟赛 探险队 期望 博弈 dp 最坏情况下最优策略 可并堆
LINK:探险队 非常难的题目 考试的时候爆零了 完全没有想到到到底怎么做 (当时去刚一道数论题了. 首先考虑清楚一件事情 就是当前是知道整张地图的样子 但是不清楚到底哪条边断了. 所以我们要做的其实 ...
- 【codeforces】【比赛题解】#915 Educational CF Round 36
虽然最近打了很多场CF,也涨了很多分,但是好久没写CF的题解了. 前几次刚刚紫名的CF,太伤感情了,一下子就掉下来了,不懂你们Div.1. 珂学的那场我只做了第一题……悲伤. 这次的Education ...
- chales抓包,模拟异常情况
抓包能做什么? 1 .可以抓取客户端和server的请求和返回,可以借助判断是客户端问题是server问题 2.可以模拟各种异常情况用来测试异常情况 3.无接口文档情况下测试接口 怎么修改你抓到的请求 ...
随机推荐
- HDU 1257 最少拦截系统(dp)
Problem Description 某国为了防御敌国的导弹突击,发展出一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以到达随意的高度,可是以后每一发炮弹都不能超过前一发的 ...
- hud 1465、2049、2045 (递推)[含简单C(n,m) n!的写法]
C - 不容易系列之一 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- u-boot简单学习笔记(一)
一:Bootloader启动结构: 由于 Boot Loader 的实现依赖于 CPU 的体系结构,因此大多数 Boot Loader 都分为 stage1 和 stage2 两大部分.依赖 ...
- appium安装报错但运行成功
npm install -g appium ERR! fetch failed https://registry.npmjs.org/appium-uiauto/-/appium-uiauto-1. ...
- python day- 6 is 和 ==的区别 encode 和 decode
1.is 和 == 的区别. == 是由来判断左右两边的内容是否相等. is 是用来判断内存地址是否相同. 引进 id ( )函数 小数据池: 对于字符串 ,数字 ,bool 值进行 id()计 ...
- HDU 6096 String 排序 + 线段树 + 扫描线
String Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Problem De ...
- base64 hash sha
/*! * Crypto-JS v1.1.0 * http://code.google.com/p/crypto-js/ * Copyright (c) 2009, Jeff Mott. All ri ...
- 使用Mock.js进行独立于后端的前端开发
Mockjs能做什么? 基于 数据模板 生成模拟数据. 基于 HTML模板 生成模拟数据. 拦截并模拟 ajax 请求. 能解决的问题 开发时,前后端进度不同步,后端还没完成数据输出,前端只好写静态模 ...
- 注意css 小细节 颜色能缩写尽量缩写
如 background: #333333; 改为 background: #333;
- isMemberOf与isKindOf的区别
这两个方法常常会困惑我们,它们有什么区别呢? 定义 首先,我们来看看它们的定义. isKindOfClass: 官方解释:Returns a Boolean value that indicates ...