Http://codeforces.com/contest/747/problem/D
The idea is to find all two negative numbers in the clip line, priority to cover the minimum length. So that there is no need to change shoes, is the best.
But here is a pit point, is the last paragraph, if the length of the last paragraph and the length of the middle section is equal, then the middle of the first paragraph should be covered, because those in the middle, if not covered, then change shoes 2 times, and the last paragraph, change shoes only once.
22 11
1-1 1 2 3-1 1 2 3 4 5 6 7-1 1 2 3 4-1 1 2 3
The answer is 4.
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<assert.h>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = 2e5 + -;intA[MAXN];structNode {intVal, id; Node (intVval,intIID): Val (vval), ID (IID) {}BOOL operator< (Const structNode & RHS)Const { if(val! = rhs.val)returnVal <Rhs.val; Else returnID <rhs.id; }};vector<int>Pos;vector<int>dis;Set<structNode>SS;BOOLVIS[MAXN];voidWork () {intN, K; CIN>> N >>K; for(inti =1; I <= N; ++i) {cin>>A[i]; if(A[i] <0) Pos.push_back (i); } if(K <pos.size ()) {cout<<-1<<Endl; return; } if(pos.size () = =0) {cout<<0<<Endl; return; } for(inti =1; I < pos.size (); ++i) {dis.push_back (Pos[i]-Pos[i-1] -1); Ss.insert (Node (pos[i)-Pos[i-1] -1I1)); } intLef = k-pos.size (); for(Set<structNode>:: Iterator it = Ss.begin (); It! = Ss.end (); ++it) {//cout << it->val << Endl; if(Lef >= it->val) {//cout << "ff" << Endl;LEF-= it->Val; Vis[it->id] =true; } Else Break; } intAns =1; for(inti =0; I < dis.size (); ++i) {if(Vis[i])Continue; Ans+=2; } if(Lef < n-Pos.back ()) {ans+=1; } cout<< ans <<Endl;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);//freopen ("Data.txt", "w", stdout);#endifWork (); return 0;}
View Code
D. Winter is Coming greedy (good question)