Codeforces Round #387 (Div. 2) D. Winter Is Coming
2016-12-19 20:43
609 查看
D. Winter Is Coming
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 kdays
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.
Input
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.
Output
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.
Examples
input
output
input
output
input
output
Note
In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number
of tires' changes equals two.
In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and
after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
题目大意:
给出 n m n 代表有n 天,m代表冬季轮胎可用m天。
给出n天的温度,一开始轮胎是夏季轮胎,然后气温如果小于 0 必须用冬季轮胎,其他无要求。问最小更换轮胎次数。
思路:
先预处理,遇到小于 0 就更换轮胎 ,遇到大于 0 在更换回来 。然后记录冬季之间间隔的天数。用剩余的天数去补这个间隔,最后剩余的天数去补最后的一段路。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int aa[200005];
int dp[200005];
int main()
{
int n,m;
int num=0;
int pos;
int ans=0;
int tep=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&aa[i]);
if(aa[i]<0)
{
num++;
pos=i;
}
}
if(num==0)
{
cout<<0<<endl;
return 0;
}
if(num>m)
{
cout<<-1<<endl;
return 0;
}
m-=num;
int noww=0;
num=0;
tep=0;
int work=0;
for(int i=1;i<=pos;i++)
{
if(noww==0)
{
if(aa[i]<0)
{
noww=1;
ans++;
}
}
else
{
if(aa[i]>=0)
{
noww=0;
ans++;
}
}
if(work&&aa[i]>=0)
{
num++;
}
else if(aa[i]<0)
{
work=1;
if(num!=0)
{
dp[tep++]=num;
num=0;
}
}
}
ans++;
sort(dp,dp+tep);
for(int i=0;i<tep;i++)
{
if(m>=dp[i])
{
m-=dp[i];
ans-=2;
}
else
{
break;
}
}
if(m>=n-pos)
{
ans--;
}
cout<<ans<<endl;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 kdays
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.
Input
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.
Output
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.
Examples
input
4 3 -5 20 -3 0
output
2
input
4 2-5 20 -3 0
output
4
input
10 6 2 -5 1 3 0 0 -4 -3 1 0
output
3
Note
In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number
of tires' changes equals two.
In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and
after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
题目大意:
给出 n m n 代表有n 天,m代表冬季轮胎可用m天。
给出n天的温度,一开始轮胎是夏季轮胎,然后气温如果小于 0 必须用冬季轮胎,其他无要求。问最小更换轮胎次数。
思路:
先预处理,遇到小于 0 就更换轮胎 ,遇到大于 0 在更换回来 。然后记录冬季之间间隔的天数。用剩余的天数去补这个间隔,最后剩余的天数去补最后的一段路。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int aa[200005];
int dp[200005];
int main()
{
int n,m;
int num=0;
int pos;
int ans=0;
int tep=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&aa[i]);
if(aa[i]<0)
{
num++;
pos=i;
}
}
if(num==0)
{
cout<<0<<endl;
return 0;
}
if(num>m)
{
cout<<-1<<endl;
return 0;
}
m-=num;
int noww=0;
num=0;
tep=0;
int work=0;
for(int i=1;i<=pos;i++)
{
if(noww==0)
{
if(aa[i]<0)
{
noww=1;
ans++;
}
}
else
{
if(aa[i]>=0)
{
noww=0;
ans++;
}
}
if(work&&aa[i]>=0)
{
num++;
}
else if(aa[i]<0)
{
work=1;
if(num!=0)
{
dp[tep++]=num;
num=0;
}
}
}
ans++;
sort(dp,dp+tep);
for(int i=0;i<tep;i++)
{
if(m>=dp[i])
{
m-=dp[i];
ans-=2;
}
else
{
break;
}
}
if(m>=n-pos)
{
ans--;
}
cout<<ans<<endl;
}
相关文章推荐
- 大作业(一)
- 2016/12/19学习工作总结
- Fragment复用Demo
- Kafka组件研究<五>---- Kafka监控-KafkaOffsetMonitor
- Android:dagger2让你爱不释手-终结篇
- 动态规划
- 浅谈移动端的自适应问题——响应式、rem/em、利用Js动态实现移动端自适应
- 监听器
- 前端Js框架汇总
- Android:dagger2让你爱不释手-重点概念讲解、融合篇
- C语言 模拟实现strcpy 与 strlen
- android ---Intent详解
- Sublime Text插件之HTML-CSS-JS Prettify
- 初识Spring Boot框架
- 初识Spring Boot框架
- 初识Spring Boot框架
- 初识Spring Boot框架
- Codeforces Round #387 (Div. 2) D && coedeforces 744 D. Winter Is Coming(贪心)
- Android:dagger2让你爱不释手-基础依赖注入框架篇
- oracle rac 安装redhat6 EMC存储绑定udev