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个 ...
随机推荐
- Asp.Net MVC页面静态化功能实现一:利用IHttpModule和ResultFilter
由于公司现在所采用的是一套CMS内容管理系统的框架,所以最近项目中有一个需求提到要求实现页面静态化的功能.在网上查询了一些资料和文献,最后采用的是小尾鱼的池塘提供的 利用ResultFilter实现a ...
- 【转】【Android工具】被忽略的UI检视利器:Hierarchy Viewer
原文:http://blog.csdn.net/ddna/article/details/5527072 Hierarchy Viewer是随AndroidSDK发布的工具,位置在tools文件夹下, ...
- iOS基础 - 数据存取
一.iOS应用数据存储的常用方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档 SQLite3 Core Data 二.应用沙盒 每个iOS应 ...
- Spring注解:@Resource、@PreConstruct、@PreDestroy、@Component
要使用Spring的注解,必须在XML文件中配置有属性,告诉人家你要使用注解,Spring容器才会去加载类上的注解: <?xml version="1.0" encoding ...
- 【IOS开发】UItextfield输入电话号码,自动调整格式
UItextfield中实现输入电话号码,自动按位置在加“—”效果.效果图如下. 核心代码: -(BOOL)textField:(UITextField *)textField shouldChang ...
- [转]ARM/Thumb2PortingHowto
src: https://wiki.edubuntu.org/ARM/Thumb2PortingHowto#ARM_Assembler_Overview When you see some assem ...
- IOS使用 Visual Format Language 定义水平和垂直约束
定义限制条件来改变一个 UI 组件在其父视图的水平和垂直方向布局的方法. 可以使用方程式里 H:方向符号代表水平方向的边距,使用 V:方向符号代表垂直方向的边 距. 转载请注明,本文转自:http:/ ...
- 求字符串长度 strlen(数组指针两种方式)
问题: 求字符串中所含有字符的个数(包括空格),即求字符串长度: #include <stdio.h> #include <assert.h> int _strlen(cons ...
- Mahout之(三)相似性度量
User CF 和 Item CF 都依赖于相似度的计算,因为只有通过衡量用户之间或物品之间的相似度,才能找到用户的“邻居”,才能完成推荐.上文简单的介绍了相似性的计算,但不完全,下面就对常用的相似度 ...
- 转--htaccess语法教程 apache服务器伪静态规则教程
转自:http://blog.csdn.net/qingli518/article/details/9118361 htaccess语法教程apache服务器伪静态规则教程 注:如果你是为了找Disc ...