Winter Is Coming(贪心)
来源:互联网 发布:织梦cms二次开发 编辑:程序博客网 时间:2024/06/08 23:13
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day.
Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days.
Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative.
Vasya can change summer tires to winter tires and vice versa at the beginning of any day.
Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.
The first line contains two positive integers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total.
The second line contains a sequence of n integers t1, t2, ..., tn ( - 20 ≤ ti ≤ 20) — the average air temperature in the i-th winter day.
Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.
4 3-5 20 -3 0
2
4 2-5 20 -3 0
4
10 62 -5 1 3 0 0 -4 -3 1 0
3
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#define MAX 200000+5using namespace std;int day[MAX];int num[MAX];int sec[MAX];int main(){ int n, k; while(cin >> n >> k){ int cnt=0; for(int i=0; i<n; i++){ cin >> day[i]; if(day[i]<0){ cnt++; num[cnt]=i; } } if(cnt>k){ cout << -1 << endl; continue; } if(cnt==0){ cout << 0 << endl; continue; } int ans=cnt*2; //cout << cnt << endl; for(int i=2; i<=cnt; i++){ sec[i-1]=num[i]-num[i-1]-1; //cout << num[i] << endl; } sort(sec+1, sec+cnt); k-=cnt; for(int i=1; i<cnt; i++){ if(k-sec[i]>=0){ k-=sec[i]; ans-=2; } } if(k>=n-num[cnt]-1) ans--; //cout << endl; cout << ans << endl; } return 0;}
- Winter Is Coming(贪心)
- codeforces Winter Is Coming 贪心
- codeforces 747 D. Winter Is Coming (贪心)
- Codeforces Round #387 (Div. 2) Winter Is Coming(贪心)
- 贪心 CodeForces - 747D Winter Is Coming
- winter is coming
- The Winter Is Coming
- Winter Is Coming CodeForces
- Codeforces Round #387 (Div. 2) D && coedeforces 744 D. Winter Is Coming(贪心)
- Codeforces Round #387 (Div. 2)D. Winter Is Coming(复杂贪心)
- Codeforces 747D Winter is Coming 贪心(从最坏解开始优化)
- 【23.26%】【codeforces 747D】Winter Is Coming
- codeforces 747D Winter Is Coming
- codeforce #387 D. Winter Is Coming
- Problem 747D - Winter Is Coming
- Winter Is Coming(CF #387 Div. 2)
- Codeforces Round #387 (Div. 2) D. Winter Is Coming
- Codeforces Round #387 (Div. 2) D. Winter Is Coming
- Android笔记—通过SharedPreferences将List<Map<String, String>>进行存储
- 51nod 1503 猪和回文(双线DP)
- ios view的frame和bounds之区别(位置和大小)
- 负载均衡-00-感想
- ORACL中的DB_NAME,SERVICE_NAME,INSTANCE_NAME,ORACLE_SID,GLOBAL_DBNAME介绍
- Winter Is Coming(贪心)
- webservice简介
- [笔记]《操作系统精髓与设计原理》---(6)处理器调度
- node.js 89行爬虫爬取智联招聘信息
- c 语言字符串和变量拼接
- Android studio 修改module名报错
- Resources加载资源
- Qt问题集锦
- 作业 三国志