上蓝场,纪念一下
A. Most Unstable Array
题意:构造一个长为n,和为m的数组,使得相邻差的绝对值之和sum最大
分析:
当n=1时,sum只能为0
当n=2时,放0和m,sum=m
否则,m放任意非首尾位置,sum等于2m
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, nr, rt << 1
#define rrt nl, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 9223372036854775807;
const int inf = 0x3f3f3f3f;
int main(void) {
int T;
read(T);
while (T--) {
int n, m;
read(n), read(m);
if (n == 1)
printf("0\n");
else if (n == 2)
printf("%d\n", m);
else
printf("%d\n", m * 2);
}
return 0;
}
B. Two Arrays And Swaps
题意:给定两个等长数组,最多可以交换k次,使得an数组和最大
分析:两个数组分别排列,依次比较bn中最大的和an中最小的,以此类推
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, nr, rt << 1
#define rrt nl, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 9223372036854775807;
const int inf = 0x3f3f3f3f;
const int maxn = 50;
int an[maxn], bn[maxn];
int main(void) {
int T;
read(T);
while (T--) {
int n, k;
read(n), read(k);
for (int i = 1; i <= n; i++)
read(an[i]);
for (int i = 1; i <= n; i++)
read(bn[i]);
sort(an + 1, an + n + 1);
sort(bn + 1, bn + n + 1, greater<int>());
int sum = 0;
for (int i = 1; i <= k; i++)
sum += max(an[i], bn[i]);
for (int i = k + 1; i <= n; i++)
sum += an[i];
printf("%d\n", sum);
}
return 0;
}
C. Board Moves
题意:有一个n*n的矩阵,要将所有数字全部移到一个格子,最少要几步
分析:从内到外,依次遍历即可
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, nr, rt << 1
#define rrt nl, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 9223372036854775807;
const int inf = 0x3f3f3f3f;
int main(void) {
int T;
read(T);
while (T--) {
int n;
read(n);
ll ans = 0;
for (int i = 1; i <= n / 2; i++) {
ans += (ll)i * 8 * i;
}
printf("%lld\n", ans);
}
return 0;
}
D. Constructing the Array
题意:一个数组开始都为0,每次选择最长的连续0段(靠左),将其中间位赋值(偶数则中间靠左)
分析:用优先队列,存位置及长度,每次取队首赋值
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, nr, rt << 1
#define rrt nl, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 9223372036854775807;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
struct node {
int p, l;
bool operator<(const node& a) const {
if (l == a.l)
return p > a.p;
return l < a.l;
}
};
int an[maxn];
int main(void) {
int T;
read(T);
while (T--) {
int n;
read(n);
int k = 0;
priority_queue<node> q;
q.push({1, n});
while (q.size()) {
int l = q.top().p;
int len = q.top().l;
q.pop();
int r = l + len - 1;
int mid = (l + r) / 2;
an[mid] = ++k;
if ((len - 1) / 2)
q.push({l, (len - 1) / 2});
if (len / 2)
q.push({mid + 1, len / 2});
}
for (int i = 1; i <= n; i++)
printf("%d ", an[i]);
cout << endl;
}
return 0;
}