A. Alex and a Rhombus
分析:随便推一下就可以了。
#include "bits/stdc++.h"
using namespace std;
int main() {
int n;
cin >> n;
int x = 0;
for (int i = 1; i < n; ++i) {
x += i;
}
printf("%d\n", (2 * n - 1) * (2 * n - 1) - 4 * x);
}
B. Nick and Array
分析:可以发现这个变换是可逆的,首先将所有整数转化为负数,如果有偶数个直接输出就可以了,如果有奇数个,可以得到把负数变成正数相当于答案乘(1-x)/x,那么就将最小的(绝对值最大)数变成正数就可以了。
#include "bits/stdc++.h"
using namespace std;
struct node {
int id, w;
} a[100004];
bool cmp1(node a, node b) {
return a.w < b.w;
}
bool cmp2(node a, node b) {
return a.id < b.id;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i].w);
a[i].id = i;
if (a[i].w >= 0) {
a[i].w = -a[i].w - 1;
}
}
if (n == 1) {
printf("%d", -a[0].w - 1);
return 0;
}
if (n & 1) {
sort(a, a + n, cmp1);
a[0].w = -a[0].w - 1;
sort(a, a + n, cmp2);
}
for (int i = 0; i < n; ++i) {
printf("%d ", a[i].w);
}
}
C. Valeriy and Deque
分析:可以发现在取出最大的数之后,剩下是一个循环,那么直接模拟到取出最大的数,然后再维护一个循环就可以了。
#include "bits/stdc++.h"
using namespace std;
vector<pair<int, int>> ans;
int a[100004];
deque<int> q;
int main() {
int n, m;
cin >> n >> m;
int maxi = -1;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
q.push_back(a[i]);
maxi = max(maxi, a[i]);
}
bool ok = 1;
while (ok) {
if (q.front() == maxi)break;
int t1 = q.front();
q.pop_front();
int t2 = q.front();
q.pop_front();
ans.push_back({t1, t2});
if (t1 < t2)swap(t1, t2);
q.push_front(t1);
q.push_back(t2);
}
vector<int> v;
while (q.size()) {
v.push_back(q.front());
q.pop_front();
}
while (m--) {
long long x;
scanf("%lld", &x);
if (x <= ans.size()) {
printf("%d %d\n", ans[x - 1].first, ans[x - 1].second);
} else {
x = (x - ans.size()) % (n - 1);
if (x == 0)x = v.size() - 1;
printf("%d %d\n", maxi, v[x]);
}
}
}
D. Tolik and His Uncle
分析:先放最左上的,再放最右下的,再放左上的,再放右下的。如果是奇数行,那么中间的一行特判一下就可以了。
#include "bits/stdc++.h"
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> v;
for (int i = 1; i <= n / 2; ++i) {
for (int j = 1; j <= m; ++j) {
v.push_back({i, j});
v.push_back({n - i + 1, m - j + 1});
}
}
if (n & 1) {
int x = n / 2 + 1;
for (int i = 1; i <= m / 2; ++i) {
v.push_back({x, i});
v.push_back({x, m - i + 1});
}
if (m & 1)v.push_back({x, m / 2 + 1});
}
for (int i = 0; i < v.size(); ++i) {
printf("%d %d\n", v[i].first, v[i].second);
}
}
E. Serge and Dining Room
分析:对于菜品,从1到a[i]全部加1,对于每个人,从1到b[i]全部减1(这样可以保证每个人只对最大的小于等于自己的菜品产生影响),然后找最右边的大于0的点就是答案,写颗线段树维护一下就可以了。
#include "bits/stdc++.h"
using namespace std;
struct node {
int l, r, maxi, laz;
} t[4000004];
void b(int rt, int l, int r) {
t[rt].l = l, t[rt].r = r, t[rt].maxi = 0, t[rt].laz = 0;
if (l < r) {
int mid = (l + r) >> 1;
b(rt << 1, l, mid);
b(rt << 1 | 1, mid + 1, r);
}
}
void pd(int rt) {
if (t[rt].laz == 0)return;
t[rt << 1].laz += t[rt].laz;
t[rt << 1 | 1].laz += t[rt].laz;
t[rt << 1].maxi += t[rt].laz;
t[rt << 1 | 1].maxi += t[rt].laz;
t[rt].laz = 0;
}
void upd(int rt, int l, int r, int w) {
if (t[rt].l >= l && t[rt].r <= r) {
t[rt].maxi += w;
t[rt].laz += w;
} else {
pd(rt);
int mid = (t[rt].l + t[rt].r) >> 1;
if (l <= mid)upd(rt << 1, l, r, w);
if (r > mid)upd(rt << 1 | 1, l, r, w);
t[rt].maxi = max(t[rt << 1].maxi, t[rt << 1 | 1].maxi);
}
}
int que(int rt, int l, int r) {
//if(t[rt].maxi<=0)return -1;
if (t[rt].maxi > 0 && t[rt].l == t[rt].r)return t[rt].l;
else {
pd(rt);
int mid = (l + r) >> 1;
if (t[rt << 1 | 1].maxi > 0)return que(rt << 1 | 1, mid + 1, r);
else if (t[rt << 1].maxi > 0)return que(rt << 1, l, mid);
else return -1;
}
}
int a[300004], c[300004];
int main() {
int n, m;
cin >> n >> m;
int x;
b(1, 1, 1000000);
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
a[i] = x;
upd(1, 1, x, 1);
}
for (int i = 1; i <= m; ++i) {
scanf("%d", &x);
c[i] = x;
upd(1, 1, x, -1);
}
int q;
cin >> q;
while (q--) {
int op, pos;
scanf("%d%d%d", &op, &pos, &x);
if (op == 1) {
upd(1, 1, a[pos], -1);
upd(1, 1, x, 1);
a[pos] = x;
} else {
upd(1, 1, c[pos], 1);
upd(1, 1, x, -1);
c[pos] = x;
}
printf("%d\n", que(1, 1, 1000000));
}
}