贪心。

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

#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. leetcode第13题--Roman to Integer

    Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  2. POJ 1579-Function Run Fun(内存搜索)

    Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16503   Accepted: 8514 ...

  3. Mysql高级之主从复制

    原文:Mysql高级之主从复制 主从复制不就是多台服务器嘛!,一个改变另一个也改变啦,内容其实都一样! 原理: 对数据库进行操作会生成一个文件,binlog(二进制文件),从服务器配置relaylog ...

  4. 站点维护使用app_offline.htm页面提供友好的更新提示

    进行站点维护时为了以一个友好的方式提示给用户,比如什么“本网站正在更新”等等的信息可以建立一个叫app_offline.htm 的静态HTM页面文件,其中修改成你要临时显示的内容,将其放在你的应用的根 ...

  5. ASP.NET MVC项目里创建一个aspx视图

    先从控制器里添加视图 视图引擎选"ASPX(C#)",使用布局或模板页不要选. 在Views\EAV目录里,生成的aspx是个单独的页面,没有代码文件,所以代码也要写在这个文件里. ...

  6. 设置checkbox为只读(readOnly)

    方式一: checkbox没有readOnly属性,如果使用disabled=“disabled”属性的话,会让checkbox变成灰色的,用户很反感这种样式可以这样让它保持只读:设置它的onclic ...

  7. SZU:A66 Plastic Digits

    Description There is a company that makes plastic digits which are primarily put on the front door o ...

  8. JavaScript原生数组函数

    有趣的JavaScript原生数组函数 在JavaScript中,可以通过两种方式创建数组,构造函数和数组直接量, 其中后者为首选方法.数组对象继承自Object.prototype,对数组执行typ ...

  9. python 获取当前时间

    我有的时候写程序要用到当前时间,我就想用python去取当前的时间,虽然不是很难,但是老是忘记,用一次丢一次,为了能够更好的记住,我今天特意写下python 当前时间这篇文章,如果你觉的对你有用的话, ...

  10. C/C++基础知识总结——类与对象

    1. 面向对象程序设计的特点 1.1 抽象 1.2 封装 1.3 继承 1.4 多态 (1) 分为:强制多态.重载多态.类型参数化多态.包含多态 (2) 强制多态:类型转换 重载多态: 类型参数化多态 ...