# Codeforces 747d:winter is Coming (greedy)

Source: Internet
Author: User

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)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.