题目链接:http://codeforces.com/contest/835/problem/B
题意:给定一个数k和一个数字串n。问你最少改几个数字才能满足所有数字的和不小于k。
思路:考虑贪心,每次肯定把最小的数字改成'9' 会使得所有数字的和最大。
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<queue> #include<vector> #include<time.h> #include<cmath> #include<set> #include<map> using namespace std; typedef long long int LL; const int MAXN = 100000 + 24; const int INF = 1e9; const int mod = 1e9 + 7; char n[MAXN]; int main(){ #ifdef kirito freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int k; while (~scanf("%d", &k)){ scanf("%s", n); int sum = 0,len=strlen(n),res=0; for (int i = 0; i < len; i++){ sum += (n[i] - '0'); } sort(n, n + len); for (int i = 0; i < len&&sum<k; i++,res++){ sum = (sum - (n[i] - '0') + 9); } printf("%d ", res); } return 0; }