CodeForces 747D Winter Is Coming
贪心。
只考虑负数的位置,先填间隔较小的,再填间隔较大的。如果填不满就不填,如果有多余就留给最后一个负数到终点这段路。
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
using namespace std; int n,k,a[],sz;
struct X
{
int L,R,len;
} s[];
vector<int>v;
int f[]; bool cmp(X a, X b)
{
return a.len<b.len;
} int main()
{
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++) scanf("%d",&a[i]);
int sum=; for(int i=; i<=n; i++) if(a[i]<) f[i]=,sum++;
if(sum>k) printf("-1\n");
else
{
if(sum==) printf("0\n");
else
{
for(int i=; i<=n; i++) if(a[i]<) v.push_back(i); for(int i=; i<v.size()-; i++) s[sz].L=v[i], s[sz].R=v[i+], s[sz].len = s[sz].R-s[sz].L-, sz++; sort(s,s+sz,cmp);
sum=k-sum;
for(int i=; i<sz; i++)
{
if(sum>=s[i].len)
{
for(int j=s[i].L;j<=s[i].R;j++) f[j]=;
sum=sum-s[i].len;
}
} if(sum>=n+-v[v.size()-]-)
{
for(int i=v[v.size()-];i<=n;i++) f[i]=;
} // for(int i=1;i<=n;i++) printf("%d ",f[i]); int ans=; int f1=,f2=n;
for(int i=;i<=n;i++)
{
if(f[i]==) continue;
else {f1=i;break;}
} for(int i=n;i>=;i--)
{
if(f[i]==) continue;
else {f2=i;break;}
} ans=ans+; if(f2!=n) ans=ans+; for(int i=f1+;i<=f2;i++)
{
if(f[i]!=f[i-]) ans++;
} printf("%d\n",ans);
} }
return ;
}
CodeForces 747D Winter Is Coming的更多相关文章
- codeforces 747D. Winter Is Coming(贪心)
题目链接:http://codeforces.com/problemset/problem/747/D 题意:冬天有n天,冬天用的轮胎总共能用k天,一开始车子用的是夏天的轮胎. 给出n天的平均气温,温 ...
- CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)
赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...
- 【23.26%】【codeforces 747D】Winter Is Coming
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 747D:Winter Is Coming(贪心)
http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...
- Codeforces 839D Winter is here【数学:容斥原理】
D. Winter is here time limit per test:3 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces 839D Winter is here - 暴力 - 容斥原理
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n s ...
- Codeforces 839D Winter is here(容斥原理)
[题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累 ...
- Codeforces 839D Winter is here
链接:CF839D 题目大意 给定一个数组大小为\(n(1\leq n\leq 200000)\)的数组\(a\),满足\(1\leq a_i \leq 1000000\). 选择其中任意\(len\ ...
- CF-补题1
CF-补题1 1.CodeForces 735C 题意:n个人淘汰赛,两个人可以比赛的条件是:两人打过的场数之差绝对值<2.求冠军最多可以打多少场. 总结:看了题解,转换一下思路.求n个 ...
随机推荐
- leetcode第九题--Palindrome Number
Problem: Determine whether an integer is a palindrome. Do this without extra space. click to show sp ...
- 《Visual Studio Magazine》2013年读者选择奖—软件类
<Visual Studio Magazine>会在每年的下半年向读者发出投票邀请,读者将在28个大类,超过500个开发工具的名单中选出他们认为最好的产品,以票数评出各分类的金.银.铜奖. ...
- LeetCode之Reverse Words in a String
1.(原文)问题描述 Given an input string, reverse the string word by word. For example, Given s = "the ...
- 【分布式存储系统sheepdog
】
Sheepdog,是由NTT的3名日本研究员开发的开源项目,主要用来为虚拟机提供块设备. 其架构例如以下: 以下,我们将从架构.模块等几个方面来介绍下: 一.架构图 如上图: 採用无中心节点的全对称架 ...
- 编写SQL的辅助工具
原文:编写SQL的辅助工具 今天在同事的帮助下,下载了一个工具:ApexSQL edit,可能是我孤陋寡闻,不知道还有这样的好工具,它可以在我键入SQL时,帮助我提示表的名称和列名称.还可以帮助我格式 ...
- C#快速随机按行读取大型文本文件
原文:C#快速随机按行读取大型文本文件 下面是我实现的一个数据文件随机读取类,可以随机读取大型文本文件的某一行.在我机器上对一个130MB的文本文件,读取第200000的速度从传统做法的400ms提高 ...
- bat批量目光声明
写bat同一批次,盯着函数应使用.这个程序对可读性 在批处理,凝视节还有一种更常用的方法: goto start = 能够是多行文本,能够是命令 = 能够包括重定向符号和其它特殊字 ...
- TeamCity vs Jenkins: Which is the Better Continuous Integration (CI) Server for .NET Software Development?
原文:http://www.excella.com/insights/teamcity-vs-jenkins-better-continuous-integration-server So, you’ ...
- Linux环境进程间通信(五): 共享内存(下)
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- 系统架构、网络通信、IM、视频会议技术
专注于系统架构.网络通信.IM.视频会议技术. 主要作品: ESFramework 强悍的通信框架.P2P框架.群集平台. OMCS 简单易用的 网络语音视频 框架. MFile 语音视频录制组件. ...