目录
F - Sonya and Problem Wihtout a Legend
目前只会ABCEF。。。等我会了就补上QAQ
A - Memory and Crow
题目链接https://codeforces.com/problemset/problem/712/A
#include <bits/stdc++.h>
using namespace std;
const int mac = 1e5 + 10;
#define ll long long
ll a[mac];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
int mark = 0;
ll sum = 0;
for (int i = n - 1; i >= 1; i--) {
sum += (mark == 0)?a[i + 1]:-a[i + 1];
a[i] += (mark == 0) ? sum : -sum;
mark ^= 1;
}
for (int i = 1; i <= n; i++)
printf("%lld ", a[i]);
printf("\n");
return 0;
}
B - Memory and Trident
题目链接https://codeforces.com/problemset/problem/712/B
#include <bits/stdc++.h>
using namespace std;
const int mac = 1e5 + 10;
#define ll long long
int x[mac], y[mac];
char s[mac];
int main()
{
int n;
while (scanf("%s", s + 1) != EOF) {
int len = strlen(s + 1);
if (len % 2) printf("-1\n");
else {
int r = 0, l = 0, u = 0, d = 0;
for (int i = 1; i <= len; i++) {
if (s[i] == 'R') r++;
else if (s[i] == 'L') l++;
else if (s[i] == 'U') u++;
else d++;
}
int ans = 0;
if (r != l) {
if (r < l) swap(r, l);
if ((r-l) % 2) ans += (r - l) / 2 + 1;
else ans += (r - l) / 2;
}
if (u != d) {
if (u < d) swap(u, d);
ans += (u - d) / 2;
}
printf("%d\n", ans);
}
}
return 0;
}
C - Memory and De-Evolution
题目链接https://codeforces.com/problemset/problem/712/C
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
scanf("%d%d", &a, &b);
if (a < b) swap(a, b);
int ans = 0, b1, b2, b3;
b1 = b2 = b3 = b;
while (1) {
if (b1 != a) {
if (b2 + b3 > a) b1 = a, ans++;
else b1 = b2 + b3 - 1, ans++;
}
if (b2 != a) {
if (b1 + b3 > a) b2 = a, ans++;
else b2 = b1 + b3 - 1, ans++;
}
if (b3 != a) {
if (b2 + b1 > a) b3 = a, ans++;
else b3 = b2 + b1 - 1, ans++;
}
if (b1 == a && b2 == a && b3 == a) break;
}
printf("%d\n", ans);
return 0;
}
D - Memory and Scores
题目链接https://codeforces.com/problemset/problem/712/D
E - Sonya and Queries
题目链接https://codeforces.com/problemset/problem/713/A
#include <bits/stdc++.h>
using namespace std;
const int mac = 1e5 + 10;
char s[50], ss[10];
int vis[1 << 20];
int main()
{
int n;
scanf("%d", &n);
while (n--) {
scanf("%s%s", ss, s);
if (ss[0] == '+') {
int len = strlen(s);
int sum = 0;
for (int i = 0; i < len; i++) {
if (s[i] & 1) sum = sum << 1 | 1;
else sum = sum << 1;
}
vis[sum]++;
}
else if (ss[0] == '-') {
int len = strlen(s);
int sum = 0;
for (int i = 0; i < len; i++) {
if (s[i] & 1) sum = sum << 1 | 1;
else sum = sum << 1;
}
vis[sum]--;
}
else {
int len = strlen(s);
int sum = 0;
for (int i = 0; i < len; i++) {
if (s[i] & 1) sum = sum << 1 | 1;
else sum = sum << 1;
}
printf("%d\n", vis[sum]);
}
}
return 0;
}
F - Sonya and Problem Wihtout a Legend
题目链接https://codeforces.com/problemset/problem/713/C
方法一:O(nlog(n))
#include <bits/stdc++.h>
using namespace std;
const int mac = 2e5 + 10;
#define IOS ios::sync_with_stdio(false)
#define ll long long
const ll inf = 1e18 + 10;
priority_queue<int>q;
ll f[mac];
int a[mac];
int main()
{
IOS;
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
ll ans = 0;
for (int i = 1; i <= n; i++) {
int x = a[i];
x -= i;
q.push(x);
if (q.size() && q.top() > x) {
ans += q.top() - x;
q.pop();
q.push(x);
}
}
cout << ans << endl;
return 0;
}
方法二:O(n^2)
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false)
#define ll long long
const int mac = 2e5 + 10;
const ll inf = 1e16 + 10;
int a[mac], b[mac];
ll dp[3100][3100];
int main()
{
IOS;
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i] - i, a[i] -= i;
sort(b + 1, b + 1 + n);
for (int i = 1; i <= n; i++) {
ll p = inf;
for (int j = 1; j <= n; j++) {
p = min(p, dp[i - 1][j]);
dp[i][j] = p + abs(a[i] - b[j]);
}
}
ll ans = inf;
for (int i = 1; i <= n; i++) ans = min(dp[n][i], ans);
cout << ans << endl;
return 0;
}