Codeforces #380 Subordinates(贪心 构造)
从前往后扫,找到一出现次数为0的数,从后面找一个出现不为0的数转化而来。设置两指针l, r来处理。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 1000008, INF = 0x3F3F3F3F;
int a[N], cnt[N], rem[N];
int main(){
int n, s;
cin >> n >> s;
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
int ans = 0;
if(a[s]){
ans++;
a[s] = 0;
}
for(int i = 1; i <= n; i++){
cnt[a[i]]++;
}
cnt[n] += cnt[0] - 1;
int l = 1, r = n;
while(l < r){
while(cnt[l] && l < r){
l++;
}
while(cnt[r] == 0 && l < r){
r--;
}
if(l < r){
cnt[l]++;
cnt[r]--;
ans++;
}
}
cout<<ans<<endl; return 0;
}
Codeforces #380 Subordinates(贪心 构造)的更多相关文章
- 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...
- Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
- CodeForces - 158B.Taxi (贪心)
CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...
- 贪心/构造/DP 杂题选做
本博客将会收录一些贪心/构造的我认为较有价值的题目,这样可以有效的避免日后碰到 P7115 或者 P7915 这样的题就束手无策进而垫底的情况/dk 某些题目虽然跟贪心关系不大,但是在 CF 上有个 ...
- 贪心/构造/DP 杂题选做Ⅱ
由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
- Codeforces Round #380 (Div. 2)/729E Subordinates 贪心
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a ...
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) E. Subordinates 贪心
E. Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)
A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
随机推荐
- js loaclstorage和sessionstorage
这里需要注意的是这两种储存方式只能以字符串的形式来存取 html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于 ...
- Html中自定义鼠标的形状
Html中自定义鼠标的形状 <html> <head> <title>自定义的鼠标形状</title> <meta http-equiv=&quo ...
- bootstrap学习笔记--bootstrap排版类的使用
标题 Bootstrap 中定义了所有的 HTML 标题(h1 到 h6)的样式,这个和一般的html没啥区别.请看下面的实例: <h1>测试1 h1</h1> <h2& ...
- vim删除以#,空格开头的行
1,删除以#号开头的行: :g/^#/d :%s/^#.*\n 2,删除以空格开头的行: :g/^\s/d “\s代表空格” :%s/^\s.*\n 3,删除以空格#开头 ...
- js笔记
1.克隆对象 克隆数组: var country=['中国','美国']; var copyCountry=country.slice(0); 克隆对象: var people={sex:'man', ...
- MFC下打开选择文件夹并获取文件夹的绝对路径
http://blog.csdn.net/w18758879921/article/details/51613382 http://www.cnblogs.com/greatverve/archive ...
- [译]C#编码约定
原文:https://msdn.microsoft.com/en-us/library/ff926074.aspx 编码约定的目的是: 创建统一格式的代码,让读者的注意力更集中在内容上面,而不是结构 ...
- leetcode--Different Ways to Add Parentheses
题目链接:https://leetcode.com/submissions/detail/86532557/ 算法类型:分治法 题目分析:计算表达式的所有结果可能性 代码实现: class Solut ...
- 实战动态PDF在线预览及带签名的PDF文件转换
开篇语: 最近工作需要做一个借款合同,公司以前的合同都是通过app端下载,然后通过本地打开pdf文件,而喜欢创新的我,心想着为什么不能在线H5预览,正是这个想法,说干就干,实践过程总是艰难的,折腾了3 ...
- file xxx from install of xxx conflicts with file from xxx
执行安装 rpm -ivh lib64stdc++6-4.6.1-2-mdv2011.0.x86_64.rpm 时提示以下错误: warning: lib64stdc++6-4.6.1-2-mdv20 ...