A. Detective Book
代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 10; int N; int a[maxn]; int maxx = 0; int main() { scanf("%d", &N); for(int i = 0; i < N; i ++) scanf("%d", &a[i]); int ans = 0; for(int i = 0; i < N; i ++) { maxx = max(a[i], maxx); if(maxx == i + 1) ans ++; } printf("%d ", ans); return 0; }
B. Good String
代码:
#include <bits/stdc++.h> using namespace std; int T, N; string s; int main() { scanf("%d", &T); while(T --) { scanf("%d", &N); cin >> s; int st, en; if(s[0] == '>') printf("0 "); else if(s[N - 1] == '<') printf("0 "); else { for(int i = 0; i < N; i ++) { if(s[i] == '>') { st = i; break; } } for(int i = N - 1; i >= 0; i --) { if(s[i] == '<') { en = i; break; } } printf("%d ", min(st, N - 1 - en)); } } return 0; }
C. Playlist
优先队列 不是很会 看了题解才知道 555
代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int N, K; int sum[maxn]; int maxx = 0; struct Node{ long long t; long long b; }node[maxn]; bool cmp(const Node &n1, const Node &n2) { return n1.b > n2.b; } int main() { scanf("%d%d", &N, &K); for(int i = 1; i <= N; i ++) cin >> node[i].t >> node[i].b; long long sum = 0, ans = 0;; priority_queue<long long, vector<long long>, greater<long long> > q; sort(node + 1, node + 1 + N, cmp); for(int i = 1; i <= N; i ++) { q.push(node[i].t); sum += node[i].t; if(q.size() > K) { sum -= q.top(); q.pop(); } ans = max(ans, sum * node[i].b); } cout << ans << endl; return 0; }
D. Minimum Triangulation
代码:
#include <bits/stdc++.h> using namespace std; int N; int dp[505]; int main() { dp[3] = 6; for(int i = 4; i <= 501; i ++) dp[i] = dp[i - 1] + (i * (i - 1)); scanf("%d", &N); printf("%d ", dp[N]); return 0; }