1. Minimum digit sum
[Title Description] define S(n), which represents the sum of each digit of N in decimal system.
Now, given an X, please find the minimum positive integer n, which satisfies x ≤ S(n)
Enter description
The number of data groups in the first row is T, and for each group of data, one number x per row.
1≤x≤105,1≤T≤10
Output description
For each set of data, one integer per row represents the smallest n.
Example 1
input
2
7
9
output
7
9
Example 2
input
2
13
18
output
49
99
[problem solving ideas]
Construction problem. The maximum number of hexadecimal is 9, so there are several 9 for the divisor of 9, and the remainder is the highest bit, so the number is the smallest.
#include <bits/stdc++.h> using namespace std; int main(){ int t,x; cin>>t; while(t--){ cin>>x; int num=x/9; int yu=x%9; if(yu!=0) printf("%c",yu+'0'); for(int i=0;i<num;i++) printf("9",); printf("\n"); } return 0; }
2. Eat grapes
[Title Description] there are three kinds of grapes, each with a, B and C grapes. There are three people. The first person only eats the 1st and 2nd grapes, the second person only eats the 2nd and 3rd grapes, and the third person only eats the 1st and 3rd grapes.
Properly arrange three people to eat all the grapes, and the one who eats the most among the three people eats as little as possible.
Enter description
The number T in the first line represents the number of data groups.
Next T lines, three numbers a,b,c per line
1≤a,b,c≤1018,1≤T≤10
Output description
For each group of data, output a line and a number to represent the amount eaten by the person who eats the most among the three people.
Example 1
input
2
1 2 3
1 2 6
output
2
3
Example 2
input
1
12 13 11
output
12
[problem solving ideas]
It can be regarded as three people standing at the apex of the triangle (assuming that the triangle can be formed). Let the two short sides of a triangle be a and B and the long side be c. Then, if the sum of the two short sides is greater than or equal to half of the long side, the total can be divided equally; Otherwise, the result is half of the long side.
#include <bits/stdc++.h> using namespace std; #define ll long long ll up(ll a, ll b) { if (a % b) return a / b + 1; return a / b; } int main() { int t; scanf("%d", &t); while (t--) { ll a, b, c; cin >> a >> b >> c; ll mx = max(max(a, b), c); ll ans = max(up(mx, 2), up(a + b + c, 3)); cout << ans << endl; } return 0; }
. ring cutting
[Title Description] Xiaoyi has n numbers arranged in a ring. Can you divide them into two consecutive parts (that is, they must be continuous on the ring) so that the sum of the two parts is equal?
Enter description
Number of data groups T in the first row, for each group of data
The number n in the first line represents the number of numbers
The next line is n numbers, giving the numbers on the ring in order.
2≤n≤100000,1≤Ai≤109
Output description
For each group of data, one row outputs YES/NO
Example 1
input
1
6
1 2 3 4 5 6
output
NO
Example 2
input
1
4
4 4 5 3
output
YES
[problem solving ideas]
Maintain the prefix and array, and then do a hash lookup through the set container to see if the cutting can be completed.
#include <bits/stdc++.h> using namespace std; int n; #define ll long long ll sum[100005]; set<ll> st; void solve() { scanf("%d", &n); memset(sum, 0, sizeof(sum)); st.clear(); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); sum[i] = sum[i - 1] + x; } if (sum[n] & 1) { puts("NO"); return; } for (int i = 1; i <= n; i++) { ll s = sum[i] - sum[n] / 2; if (st.find(s) != st.end()) { puts("YES"); return; } st.insert(sum[i]); } puts("NO"); return; } int main() { int t; scanf("%d", &t); while (t--) solve(); return 0; }
4. Jump column
[Title Description] Xiaoyi has n columns, and the height of the ith column is hi. At first, Xiao Yi stood on the first post. Xiaoyi can jump from the ith column to the j column if and only if hj ≤ hi and 1 ≤ J − i ≤ K. Where k is a specified number.
In addition, Xiaoyi has a chance to release super powers. This super ability allows Xiaoyi to jump from column i to any column j satisfying 1 ≤ j − i ≤ k, regardless of the limit of column height.
Now Xiaoyi wants to know whether Xiaoyi can reach the nth column.
Enter description
Number of data groups in the first row T
For each group of data, the first row of numbers n,k, and the next row of N numbers represent hi.
1≤n≤1000,1≤hi≤109,1≤T≤10,1≤k≤n
Output description
For each group of data, output YES or NO
Example 1
input
1
5 3
6 2 4 3 8
output
YES
Example 2
input
1
5 2
1 8 2 3 4
output
NO
[problem solving ideas]
Consider dp. Let dp[i][0] and dp[i][1] indicate whether the column can be reached without / using superpowers. Enumerate the front k columns each time and update dp[i][0] and dp[i][1].
#include <bits/stdc++.h> using namespace std; int h[1005]; int f[1005][2]; int n, k; void solve() { scanf("%d%d", &n, &k); memset(f, 0, sizeof(f)); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); f[1][0] = f[1][1] = 1; for (int i = 2; i <= n; i++) { for (int j = i - 1; j >= 1 && j >= i - k; j--) { if (f[j][0]) { f[i][1] = 1; if (h[j] >= h[i]) f[i][0] = 1; } if (f[j][1]) { if (h[j] >= h[i]) f[i][1] = 1; } } } if (f[n][1]) puts("YES"); else puts("NO"); return; } int main() { int t; scanf("%d", &t); while (t--) solve(); return 0; }
5. Product
[Title Description] Xiaoyi gives you a positive integer sequence Ai of length n. you can add or subtract a number by one at the cost of 1 each time. You want to use the least cost to make the product of all numbers equal to B and find the minimum cost (after the operation, each number must also be a positive integer).
Enter description
The number n, B in the first line represents the product of sequence length and target.
The next line of n positive integers represents the initial sequence.
1≤n≤103,1≤B≤105,1≤Ai≤105.
Output description
One number per line indicates the answer
Example 1
input
5 12
1 3 9 2 6
output
10
explain
It takes 2 to change 3 into 1 and 8 to change 9 into 1. The total cost is 10.
Example 2
input
3 15
3 8 7
output
9
explain
It takes 3 to change 8 into 5 and 6 to change 7 into 1. The total cost is 9.
[problem solving ideas]
Direct order f[i][j] represents the minimum cost of making the product of the first I numbers j. the direct transfer complexity is O(nA(B)^2), where A(B) represents the number of divisors of B. generally, it is not very large and can be passed through the data.
#include <bits/stdc++.h> using namespace std; int f[1005][105]; int n, b; int a[1005]; vector<int> v[100005]; int num[100005]; int main() { scanf("%d%d", &n, &b); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= b; i++) { for (int j = i; j <= b; j += i) v[j].push_back(i); } for (int i = 0; i < v[b].size(); i++) num[v[b][i]] = i; f[0][0] = 0; for (int i = 1; i < v[b].size(); i++) f[0][i] = 1e9; for (int i = 1; i <= n; i++) { for (int j = 0; j < v[b].size(); j++) { f[i][j] = 1e9; for (int k = 0; k < v[v[b][j]].size(); k++) { f[i][j] = min(f[i][j], f[i - 1][num[v[v[b][j]][k]]] + abs(a[i] - v[b][j] / v[v[b][j]][k])); } } } printf("%d\n", f[n][v[b].size() - 1]); return 0; }