CodeForces - 747D
2017-05-17 21:47
204 查看
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.
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.
Example
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.
先分成所有的负数块
然后开始按照间隔联通
最后特判一下结尾
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.
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.
Example
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.
先分成所有的负数块
然后开始按照间隔联通
最后特判一下结尾
#include<iostream> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #include<stack> using namespace std; int n, k; int tu[1000001]; struct p { int z, y, cd; bool operator < (const p&a)const { return cd > a.cd; } }; int main() { cin >> n >> k; int _ = 0; for (int a = 1; a <= n; a++) { scanf("%d", &tu[a]); if (tu[a] < 0)_++; } if (_ > k) { cout << -1; return 0; } if (_ == 0) { cout << 0; return 0; } priority_queue<p>q; int jc = 1; int qq = 0; for (int a = 1; a <= n; a++) { if (tu[a] >= 0)continue; if (jc) { qq = a; jc = 0; continue; } q.push({ qq,a,a - qq - 1 }); qq = a; } int kuai = _; k -= _; for (;;) { if (q.empty())break; if (q.top().cd > k)break; kuai--; k -= q.top().cd; q.pop(); } kuai *= 2; if (k >= n - qq)kuai--; cout << kuai; }
相关文章推荐
- CodeForces 372B 脑洞大开的DP递推
- Codeforces 623 B. Array GCD
- Codeforces 702C Cellular Network(二分)
- 【Codeforces Round 397】Codeforces 765A-D
- CodeForces 834 C. The Meaningless Game
- CodeForces 827 E.Rusty String(FFT)
- Codeforces 1B. Spreadsheets
- CodeForces 158A Next Round
- Codeforces 148 E Porcelain dp
- Codeforces 430C Xor-Tree DFS,奇偶性
- CodeForces - 8C:Looking for Order
- 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演
- codeforces 100548F (西安现场赛F题):容斥原理
- 【CodeForces 624D】Array GCD
- CodeForces 500 D. New Year Santa Network(树形DP)
- Codeforces 598B Queries on a String(rotate(begin,mid,end))
- CodeForces - 768B Code For 1(找规律)
- codeforces-913C Party Lemonade
- CodeForces 492D Vanya and Computer Game
- Codeforces 624B Making a String 【水题】