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第17题--4Sum
Problem:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- javascript保留两位
原文:javascript保留两位 //保留两位小数 //功能:将浮点数四舍五入,取小数点后2位 function toDecimal(x) { var f = parseFloat(x); if ( ...
- 如何有效的遍历django的QuerySet
最近做了一个小的需求,在django模型中通过前台页面的表单的提交(post),后台对post的参数进行解析,通过models模型查询MySQL,将数据结构进行加工,返回到前台页面进行展示.由于对dj ...
- oledb快速导入Excel案例
DataTable dtImportExcel = null; string pathFile = Server.MapPath("~/ErrorCatory.xlsx"); // ...
- beanutils中jdbc
public class JDBCTest { // public static void main(String[] args) throws Exception {// Cla ...
- SQLSERVER数据库学习总结七(视图,索引)
--视图的主意点:不能和表的名称相同,如果某一列为函数,表达式,常量或者与来自多张表的列名相同,必须为列定义名称,不能在试图上创建索引 if exists(select 1 from sys.syso ...
- JS引擎
在javax.script包下提供有关脚本引擎的类和接口,为我们定义了一系列的规范,达到可以在java应用程序中执行脚本语言编写的程序. javax.script包的主要功能有: 1.脚本执行:执行脚 ...
- dedecms模板中使用php代码
{dede:php} echo “test”: {/dede:php} 使用这段代码之前要在后台的系统--系统基本参数--其它选项 里找到 模板引擎禁用标签: php 将其删除
- [转]iOS Tutorial – Dumping the Application Memory Part 2
Source:https://blog.netspi.com/ios-tutorial-dumping-the-application-memory-part-2/ In my previous bl ...
- ie8下下拉菜单文字为空
<html> <head> <title></title> <script type="text/javascript"> ...