Http://codeforces.com/problemset/problem/747/D
Test instructions: There are n days, k times use winter tires opportunity, unlimited use of summer tires opportunity, if t<=0 must use winter wheel, other casual. Ask for the fewest number of changes to the tyre.
Idea: The number of days before the winter tol, if the number of days >k, then it is impossible to spend. Otherwise, in the worst case, change winter tires every winter, and then spend the winter to change the summer tires, so the answer is 2*tol. Then consider trying to make every winter continuous, so that you can reduce the number of changes, so that the interval between the winter, and then from small to large sort, every time to reduce the interval, ans can-2. Then consider the special case, if the last time to change winter tires, can be used to end, then ans-1.
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <string>6#include <cmath>7#include <queue>8#include <vector>9#include <map>Ten#include <Set> One using namespacestd; A #defineINF 0x3f3f3f3f - #defineN 200010 -typedefLong LongLL; thevector<int>Vec; - intT[n]; - intMain () { - intN, K; +CIN >> N >>K; - intTol =0, ans =0; + intPre =-1, last =-1; A for(inti =1; I <= N; i++) { atscanf"%d", &t[i]); - if(T[i] <0) { -last = i;//One last winter -Ans + =2; -tol++; - if(Pre! =-1) inVec.push_back (I-pre-1);//Handling Winter Intervals -Pre =i; to } + } - theK-=Tol; * if(K <0) { $Puts"-1");Panax Notoginseng}Else { - sort (Vec.begin (), Vec.end ()); the for(inti =0; I < vec.size (); i++) { + if(K-vec[i] >=0) { AK-=Vec[i]; theAns-=2; +}Else Break; - } $ if(N-last <= k) ans--; $printf"%d\n", ans); - } - return 0; the}
Codeforces 747d:winter is Coming (greedy)