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