A String Game
meaning of the title
Give a length of n n String of n s s s. Output will s s s ago x x Results at the end of x character migration.
Solution
Simulate according to the meaning of the question. ( x x The x order of magnitude is large, right n n n after taking the mold, reduce the actual effective operation times.
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, j, k) for (ll(i) = (j); (i) <= (k); (++i)) void solve() { ll n, x; string s; cin >> n >> x >> s; x %= n; rep(i, x, n - 1) cout << s[i]; rep(i, 0, x - 1) cout << s[i]; cout << endl; } signed main() { solve(); return 0; }
B Sequence Game
meaning of the title
Given length is n n Array of n a a a. Each number in the array has k k k possible values, find the array a a The longest ascending subsequence of a
Solution
Ensure the given in the question k k If k possible values are monotonous and the values are less than 1000, consider using a dp array record d p [ i ] dp[i] dp[i] is expressed in i i i is the length of the longest ascending subsequence at the end of the number.
#include <bits/stdc++.h> using namespace std; #define rep(i, j, k) for (ll(i) = (j); (i) <= (k); (++i)) #define mem(x, y) memset(x, y, sizeof(x)) typedef long long ll; const int N = 1e6 + 7; int n, k, ans, tmp[N], dp[N]; signed main() { cin >> k >> n; rep(i, 1, n) { int mx = 0, now = 0, x; mem(tmp, 0); rep(j, 1, k) { cin >> x; while (now < x) mx = max(mx, dp[now]), now++; tmp[x] = mx + 1; } rep(j, 0, 1000) dp[j] = max(tmp[j], dp[j]), ans = max(ans, dp[j]); } cout << ans; return 0; }
C Simple Game
meaning of the title
One with n n In a directed graph with n nodes, ask the minimum number of nodes that each node can reach, and output the results after sorting and de duplication.
Solution
At the beginning, I didn't think about it carefully. I directly built a map to run dfs, and then updated the access nodes. It was found that if I simply judged that the ring would not go where I went, the nodes would not be updated in time, resulting in that other nodes on the ring could not be updated in time. The code is 95 points. In fact, if the data is stronger, I can't get such a high score
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, j, k) for (ll(i) = (j); (i) <= (k); (++i)) const int N = 1e5 + 7; vector<ll> to[N]; ll ans[N]; bool vis[N]; ll dfs(int p) { if (vis[p]) return ans[p]; vis[p] = 1; for (auto i : to[p]) ans[p] = min(ans[p], dfs(i)); return ans[p]; } signed main() { ll n, m, u, v; cin >> n >> m; rep(i, 1, n) ans[i] = i; rep(i, 1, m) cin >> u >> v, to[u].emplace_back(v); rep(i, 1, n) dfs(i); vector<ll> a; rep(i, 1, n) a.emplace_back(ans[i]); sort(a.begin(), a.end()), a.erase(unique(a.begin(), a.end()), a.end()); for (auto i : a) cout << i << ' '; return 0; }
The positive solution should build an inverse graph, and then directly run dfs. The first access from small to large is the minimum value that the current node can reach.
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, j, k) for (ll(i) = (j); (i) <= (k); (++i)) const int N = 1e5 + 7; vector<ll> to[N]; int ans[N]; bool vis[N]; void dfs(int fa, int p) { ans[p] = min(ans[p], fa); if (vis[p]) return; vis[p] = 1; for (auto i : to[p]) dfs(ans[p], i); } signed main() { int n, m, u, v; cin >> n >> m; rep(i, 1, n) ans[i] = i; rep(i, 1, m) cin >> u >> v, to[v].emplace_back(u); rep(i, 1, n) dfs(i, i); vector<ll> a; rep(i, 1, n) a.emplace_back(ans[i]); sort(a.begin(), a.end()), a.erase(unique(a.begin(), a.end()), a.end()); for (auto i : a) cout << i << ' '; return 0; }
D Sweet Game
meaning of the title
Altogether n n n candy, the sweetness of each candy will change over time.
Eat a candy every other unit time when the eating number is i i When eating candy of i, it is necessary to ensure that the number ratio is higher before eating this candy i i i large candies are eaten up; or if the number is higher than i i i if the large candy is not finished, the number must be compared next i i i big candy is finished, that is, you can't eat it at this time i i i small candy.
Solution
Construct a candy sequence and consider the sequence as the sugar eating order.
According to the requirements of the topic, ensure that the number is i i i's candy is eaten before or after eating this candy i i i when all the big sweets are eaten, the order of eating sweets should be considered from the back to the front.
When the number is n n When eating n , sugar, you can consider eating No n n n) before or after eating sugar numbered n − 1 n-1 n − 1 sugar, n − 2 n-2 n − 2 is the same, and so on. Therefore, it is assumed that the number has been determined to be greater than x + 1 x+1 The sugar eating order after the sugar of x+1 , is next considered as No x x When to eat the sugar of x , there are two choices at this time. Eat this sugar at the beginning, and the contribution to the answer is a [ x ] + s u m [ x + 1 ] a[x]+sum[x+1] a[x]+sum[x+1], where s u m [ x + 1 ] sum[x+1] sum[x+1] indicates that the number in a unit time is greater than x x The sum of changes in sugar sweetness of x ^ or the number is greater than x + 1 x+1 After eating all the sugar of x+1 , eat this sugar. The contribution to the answer is a [ x ] + d [ x ] ∗ ( n − x ) a[x]+d[x]*(n-x) a[x]+d[x] * (n − x), where d [ x ] d[x] d[x] indicates that the number per unit time is x x The change value of sugar sweetness of x +, n − x n-x n − x indicates how long the sugar was eaten.
#include <bits/stdc++.h> using namespace std; #define int ll #define rep(i, j, k) for (ll(i) = (j); (i) <= (k); (++i)) #define rrep(i, j, k) for (ll i = (ll)(j); i >= (ll)(k); i--) typedef long long ll; const int N = 1e5 + 7; ll a[N], d[N], sum[N], n, l, r; signed main() { cin >> n; rep(i, 1, n) cin >> a[i]; rep(i, 1, n) cin >> d[i]; rrep(i, n, 1) sum[i] = d[i] + sum[i + 1]; ll ans = a[n]; rrep(i, n - 1, 1) ans += a[i] + max(sum[i + 1], d[i] * (n - i)); cout << ans << '\n'; return 0; }