Codeforces 845B Luba and the Ticket (greedy)

Source: Internet
Author: User
Topic links

CF 845B Topic to the effect

Enter a six-digit number, and ask at least how many digits can make the first three bits and equal the sum of the last three bits. Analysis

Simple greed.
For the initial six digits, we calculate the first three bits and the last three bits and then make each operation as much as possible to fill the first three bits and the gap with the last three bits. And the smaller group, each of which can compensate for a gap of up to 9 minus its digits (making it larger to 9), and the larger group in which each of the most can make up the gap for its number (to change it to 0), then the number of 6 digits can compensate for the gap, from large to small as far as possible to fill the gap, so that the gap is less or equal to 0. Code

#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath  > #include <algorithm> #include <queue> #define LS (rt<<1) #define RS (rt<<1|1) using namespace
Std
Const Double Pi=4*atan (1.0);
const int maxn=100010;

const int MAXM=2*MAXN;
    BOOL CMP (int a,int b) {return (a>b);} int main () {int a[10],num1,num2,s,ans;
    String str;
    cin>>str;
    num1=str[0]+str[1]+str[2]-3* ' 0 ';
    num2=str[3]+str[4]+str[5]-3* ' 0 ';
    ans=0;
    if (num1==num2) cout<<0<<endl;
        else if (num1<num2) {a[0]=9-(str[0]-' 0 ');
        a[1]=9-(str[1]-' 0 ');
        a[2]=9-(str[2]-' 0 ');
        a[3]=str[3]-' 0 ';
        a[4]=str[4]-' 0 ';
        a[5]=str[5]-' 0 ';
        Sort (a,a+6,cmp);
        S=NUM2-NUM1;
        while (s>0) {s-= (a[ans++]);
    } cout<<ans<<endl;
} else if (num1>num2) {a[0]=str[0]-' 0 ';        a[1]=str[1]-' 0 ';
        a[2]=str[2]-' 0 ';
        a[3]=9-(str[3]-' 0 ');
        a[4]=9-(str[4]-' 0 ');
        a[5]=9-(str[5]-' 0 ');
        Sort (a,a+6,cmp);
        s=num1-num2;
        while (s>0) {s-= (a[ans++]);
    } cout<<ans<<endl;
} return 0;
 }/* 651894 */

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.