贪心。

只考虑负数的位置,先填间隔较小的,再填间隔较大的。如果填不满就不填,如果有多余就留给最后一个负数到终点这段路。

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

  1. codeforces 747D. Winter Is Coming(贪心)

    题目链接:http://codeforces.com/problemset/problem/747/D 题意:冬天有n天,冬天用的轮胎总共能用k天,一开始车子用的是夏天的轮胎. 给出n天的平均气温,温 ...

  2. CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

  3. 【23.26%】【codeforces 747D】Winter Is Coming

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. Codeforces 747D:Winter Is Coming(贪心)

    http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...

  5. Codeforces 839D Winter is here【数学:容斥原理】

    D. Winter is here time limit per test:3 seconds memory limit per test:256 megabytes input:standard i ...

  6. 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 ...

  7. Codeforces 839D Winter is here(容斥原理)

    [题目链接] http://codeforces.com/contest/839/problem/D [题目大意] 给出一些数,求取出一些数,当他们的GCD大于0时,将数量乘GCD累加到答案上, 求累 ...

  8. Codeforces 839D Winter is here

    链接:CF839D 题目大意 给定一个数组大小为\(n(1\leq n\leq 200000)\)的数组\(a\),满足\(1\leq a_i \leq 1000000\). 选择其中任意\(len\ ...

  9. CF-补题1

    CF-补题1 1.CodeForces 735C    题意:n个人淘汰赛,两个人可以比赛的条件是:两人打过的场数之差绝对值<2.求冠军最多可以打多少场.  总结:看了题解,转换一下思路.求n个 ...

随机推荐

  1. 与6lowpan最相关的RFC文档列表

    有关于6lowpan最原始的文档,请参考下面的链接与截图: http://datatracker.ietf.org/wg/6lowpan/

  2. CSS边框阴影效果

    ul { margin:5px 0 0 0; padding:0; list-style:none; width:300px; background:#f1f1f1; }li { border-lef ...

  3. Redis源代码分析(二十四)--- tool工具类(2)

    在上篇文章中初步的分析了一下,Redis工具类文件里的一些使用方法,包含2个随机算法和循环冗余校验算法,今天,继续学习Redis中的其它的一些辅助工具类的使用方法.包含里面的大小端转换算法,sha算法 ...

  4. 基于EF+WCF的通用三层架构及解析

    分享基于EF+WCF的通用三层架构及解析 本项目结合EF 4.3及WCF实现了经典三层架构,各层面向接口,WCF实现SOA,Repository封装调用,在此基础上实现了WCFContext,动态服务 ...

  5. SpringMVC源码

    SpringMVC源码分析系列 说到java的mvc框架,struts2和springmvc想必大家都知道,struts2的设计基本上完全脱离了Servlet容器,而springmvc是依托着Serv ...

  6. WinDbg 命令手册

    WinDbg 命令三部曲:(一)WinDbg 命令手册   本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 系列博文 <WinDbg 命令三部 ...

  7. Step one : 熟悉HTML

    //H1 1 <html> <head> <title>BeiJing</title> </head> <body> <h ...

  8. C#中一些易混淆概念总结

    C#中一些易混淆概念 这几天一直在复习C#基础知识,过程中也发现了自己以前理解不清楚和混淆的概念.现在给大家分享出来我的笔记: 一,.NET平台的重要组成部分都是有哪些 1)FCL (所谓的.NET框 ...

  9. web代理进行跨域访问

    通过web代理进行跨域访问,http请求返回超时的问题定位   [现象] 在ajax通过web代理跨域访问时,http第一次登陆时正常,但是第二次再下发其他命令的时候总是返回 java.net.Soc ...

  10. 通过VS2010命令提示窗口创建强命名文件时报错问题

    问题描述详见图 解决方案 00000005意思是Access Denied(即拒绝访问). 原因是:Program Files(x86)目录对一般用户和未提升权限的管理员是只读的. 所以只要赋予Use ...