E.
题意:给定长度为n的序列a[1~n],每次操作可以选择一个子序列,然后把所有奇数位置+1,偶数位置-1,或者-1+1,求使a[1~n]=0的最小操作次数.
如果a[i]>0,那么我们可以利用前面a[i]<0的值抵消,该位置能产生供后面的数抵消的值是a[i],因为可以同时抵消多个数.a[i]<0同理.贪心处理即可.
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
// #define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 2e5 + 10;
int n, a[N];
void solve() {
cin >> n;
int A = 0, B = 0, ans = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] > 0) {
int t = min(B, a[i]);
B -= t;
A += a[i];
a[i] -= t;
ans += a[i];
}else if (a[i] < 0) {
a[i] = -a[i];
int t = min(A, a[i]);
A -= t;
B += a[i];
a[i] -= t;
ans += a[i];
}
}
cout << ans << "\n";
}
signed main() {
IOS;
int T;
cin >> T;
while (T--) solve();
}
F.
题意:n个方块,建造一个周长最短的图形,输出方案和方案数.
直接枚举长和宽,求出mx值,然后按照矩形贪心地堆就可以输出方案.注意到周长最短是矩形,矩形四周折进来周长不变,折进来的形状是一个梯形,我们可以用完全背包求出f[n]堆n个方块的梯形方案数,然后对矩形四个角都进行一次背包即可.
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
// #define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 1010;
int n, MOD, f[5][N];
void init() {
f[1][0] = 1;
for (int i = 1; i < N; ++i) {
for (int j = i; j < N; ++j) {
(f[1][j] += f[1][j - i]) %= MOD;
}
}
// f[0][0] = 1;
for (int i = 2; i <= 4; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; j + k < N; ++k) {
(f[i][j + k] = f[i][j + k] + f[i - 1][j] * f[1][k] % MOD) %= MOD;
}
}
}
}
void solve(int op) {
int ans = INF, w = 0, h = 0;
vector<PII> choose;
cin >> n;
for (int i = 1; i <= n; ++i) {
int j = (n + i - 1) / i;
if (i > j) break;
if (i + j < ans) {
w = i;
h = j;
ans = i + j;
choose.clear();
choose.emplace_back(i, j);
}else if (i + j == ans) {
choose.emplace_back(i, j);
}
}
if (op == 1) {
cout << h << " " << w << "\n";
int tot = 0;
for (int i = 1; i <= h; ++i) {
for (int j = 1; j <= w; ++j) {
if (++tot <= n) cout << "#";
else cout << ".";
}
cout << "\n";
}
}else {
int res = 0;
for (auto [w, h] : choose) {
int del = w * h - n;
if (del < 0) break;
if (w == h) (res += f[4][del]) %= MOD;
else (res += f[4][del] * 2) %= MOD;
}
cout << ans * 2 << " " << res << "\n";
}
}
signed main() {
IOS;
int T, op;
cin >> T >> op;
if (op == 2) cin >> MOD, init();
while (T--) solve(op);
}