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 */