COJ 1156 Switching Bulbs

Source: Internet
Author: User

A simulation problem

For all 0 or 1, we can all imagine a state of 0.

V[i] indicates the original value

But for the original value of 1 to add its value as the initial

Then after conversion a[i] =-v[i], if the original is 0, then the direct assignment

We always want to add as large a value as possible, so the order of a from large to small, always add to the negative when you need to consider this negative number to take, in fact, we can take a negative, because when added

If the first negative number is negative, then we can reverse the negative number and add it back.

But the first negative number can not be taken to see its absolute value is not greater than the previous one, if large, it is better to change the previous number once again color after the shed does not need to add a negative

Finally, we determine the number of actually used, if the operand is greater than this number, indicating that the range is just constantly changing the minimum number of colors

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 Const intN =100005;6 intCol[n], v[n], a[n];7 8 BOOLcmpintAintb)9 {Ten     returnA>b; One } A  - intMain () - { the    //freopen ("a.in", "R", stdin); -     intN, M; -      while(SCANF ("%d%d", &n, &m) = =2) -     { +          for(inti =0; I<n; i++) -scanf"%d", col+i); +          for(inti =0; I<n; i++) Ascanf"%d", v+i); at  -         intval =0; -          for(intI=0; I<n; i++) -             if(Col[i]) { -Val + =V[i]; -A[i] =-V[i]; in             } -             Else{ toA[i] =V[i]; +             } -Sort (A, A +N, CMP); the          for(inti =0; I<n; i++){ *             if(A[i] <0){ $                 if(i = =0){Panax Notoginsengn =1; -                      Break; the                 } +                 if(-a[i] > a[i-1]) n =i; A                 Elsen = i+1; the                  Break; +             } -         } $         if(m<=N) { $              for(inti =0; I<m; i++) -Val + =A[i]; -         } the         Else{ -              for(inti =0; I<n; i++){WuyiVal + =A[i]; the             } -             if((m-n) &1) Val-= a[n-1]; Wu         } -printf"%d\n", Val); About     } $     return 0; -}

COJ 1156 Switching Bulbs

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.