D. Winter is Coming greedy (good question)

Source: Internet
Author: User

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)

Contact Us

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.