Codeforces 437B & 437C

Source: Internet
Author: User

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

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.