Codeforces 437B
Test instructions: Find the minimum number between 1 and limit so that their sum of lowbit equals sum.
First, all the Lowbit () are sorted and then swept over.
#include <iostream>#include<algorithm>using namespacestd;structnode{intN,ln;} a[100005];intLowbit (intx) { returnx& (-x);}BOOLCMP (node Aa,node bb) {returnAa.ln>Bb.ln;}intcan[100005];intMain () {intsum,limit,ok=0, s=0; Long Longtot=0; CIN>> sum >>limit; for(intI=0; i<limit;i++) {A[I].N=i+1; A[i].ln=lowbit (i+1); } sort (A,a+limit,cmp); for(intI=0; i<limit;i++) { if(Tot+a[i].ln <sum) {Can[i]=1; Tot+=A[i].ln; S++; Continue; } if(Tot+a[i].ln = =sum) {Can[i]=1; Tot+=A[i].ln; S++; Break; } if(tot>sum) {Can[i]=0; Continue; } } if(tot==sum) ok=1; if(OK) {cout<< s <<Endl; for(intI=0; i<limit;i++) { if(Can[i]) cout<< A[I].N <<" "; } cout<<Endl; } Else{cout<<"-1"<<Endl; } return 0;}
Codeforces 437C
Test instructions: N-sections, m-connecting lines, which take up a line that takes energy, and is the smaller of the energy value of the connected part of the two ends. The minimum cost required to remove all the cables.
Without the brain to read the side Plus, the line must be completely removed, read an accumulation of the last output can be.
#include <iostream>#include<algorithm>using namespacestd;inta[1005];intMain () {intn,m; Long Longans=0; CIN>> N >>m; for(intI=1; i<=n;i++) {cin>>A[i]; } for(intI=1; i<=m;i++) { intx, y; CIN>> x >>y; Ans+=min (a[x],a[y]); } cout<< ans <<Endl; return 0;}
Codeforces 437B & 437C