Day 26 - 模拟赛
热门景点(hot)
题目描述
输入格式
输出格式
\(\text{input1}\)
10 5
1 7 4 0 9 4 8 8 2 4
6 6
2 8
2 2
3 6
7 8
\(\text{output1}\)
Y
N
Y
N
Y
数据范围
#include<iostream>
#include<cstdio>
#include<ctime>
using namespace std;
#define MAXN 5000005
int read() {
int x = 0; bool f = 0;
char ch = getchar();
while(ch < 48 || ch > 57) {
if(ch == 45) f = 1;
ch = getchar();
}
while(ch >= 48 && ch <= 57) {
x = (x << 3) + (x << 1) + (ch - 48);
ch = getchar();
}
return f ? -x : x;
}
int n, q, a[MAXN], pl[MAXN], pr[MAXN], cnt = 0, l, r;
struct node { int lm, rm; } b[MAXN];
int main() {
freopen("hot.in", "r", stdin);
freopen("hot.out", "w", stdout);
n = read(); q = read();
for(int i = 1; i <= n; i ++) a[i] = read();
pl[1] = 1;
for(int i = 2; i <= n; i ++)
if(a[i] <= a[i - 1]) pl[i] = pl[i - 1];
else pl[i] = i;
pr[n] = n;
for(int i = n - 1; i >= 1; i --)
if(a[i] <= a[i + 1]) pr[i] = pr[i + 1];
else pr[i] = i;
// for(int i = 1; i <= n; i ++) cout << pl[i] << " ";
// cout << "\n";
// for(int i = 1; i <= n; i ++) cout << pr[i] << " ";
// cout << "\n";
while(q --) {
l = read(); r = read();
if(pr[l] >= pl[r]) printf("Y\n");
else printf("N\n");
}
// for(int i = 1; i <= n; i ++)
// if(a[i] >= a[i - 1]) pl[i] = pl[i - 1] + 1;
// else pl[i] = 1;
// for(int i = n; i >= 1; i --)
// if(a[i] >= a[i + 1]) pr[i] = pr[i + 1] + 1;
// else pr[i] = 1;
//// for(int i = 1; i <= n; i ++) cout << pl[i] << " ";
//// cout << "\n";
//// for(int i = 1; i <= n; i ++) cout << pr[i] << " ";
//// cout << "\n";
// int tmp = 1; while(pl[tmp] > pl[tmp - 1]) tmp ++;
//// cout << tmp - 1 << "\n";
// pl[n + 1] = 1;
// for(int i = tmp - 1; i <= n; ) {
// b[++ cnt].lm = i - pl[i] + 1, b[cnt].rm = i + pr[i] - 1;
// i += pr[i]; while(pl[i + 1] != 1) i ++;
// }
//// for(int i = 1; i <= cnt; i ++) cout << b[i].lm << " " << b[i].rm << "\n";
// int l = 0, r = 0;
// while(q --) {
// l = read(); r = read();
// if(r - l + 1 == 1 || r - l + 1 == 2) { printf("Y\n"); continue; }
// int ll = 1, lr = cnt, ansl = -1;
// while(ll <= lr) {
// int mid = (ll + lr) >> 1;
// if(b[mid].lm <= l) ansl = mid, ll = mid + 1;
// else lr = mid - 1;
// }
// int rl = 1, rr = cnt, ansr = -1;
// while(rl <= rr) {
// int mid = (rl + rr) >> 1;
// if(b[mid].rm >= r) ansr = mid, rr = mid - 1;
// else rl = mid + 1;
// }
//// cout << ansl << " " << ansr << "\n";
// if(ansr + 1 == ansl && b[ansr].lm <= l && b[ansr].rm >= r) printf("Y\n");
// else if(ansl == ansr && ansl != -1) printf("Y\n");
// else printf("N\n");
// }
return 0;
}
管道(tunnel)
题目描述
输入格式
输出格式
\(\text{input1}\)
1 7
15 -5 100 -40 10 10 10
\(\text{output1}\)
115
\(\text{input2}\)
3 3
-10 4 -10
3 1 -10
6 2 8
\(\text{output2}\)
28
样例解释
数据范围
#include<iostream>
using namespace std;
#define MAXN 1005
long long read() {
long long x = 0; bool f = 0;
char ch = getchar();
while(ch < 48 || ch > 57) {
if(ch == 45) f = 1;
ch = getchar();
}
while(ch >= 48 && ch <= 57) {
x = (x << 3) + (x << 1) + (ch - 48);
ch = getchar();
}
return f ? -x : x;
}
long long n, m, a[MAXN][MAXN], b[MAXN][MAXN], ans = 0;
void update() {
long long res = 0;
for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++)
if(b[i][j] == 1) {
if(b[i][j + 1] == 2 || b[i][j + 1] == 4) res += max(0ll, a[i][j] + a[i][j + 1]);
if(b[i + 1][j] == 3 || b[i + 1][j] == 4) res += max(0ll, a[i][j] + a[i + 1][j]);
}
else if(b[i][j] == 2) {
if(b[i][j - 1] == 1 || b[i][j - 1] == 3) res += max(0ll, a[i][j] + a[i][j - 1]);
if(b[i + 1][j] == 3 || b[i + 1][j] == 4) res += max(0ll, a[i][j] + a[i + 1][j]);
}
else if(b[i][j] == 3) {
if(b[i][j + 1] == 2 || b[i][j + 1] == 4) res += max(0ll, a[i][j] + a[i][j + 1]);
if(b[i - 1][j] == 1 || b[i - 1][j] == 2) res += max(0ll, a[i][j] + a[i - 1][j]);
}
else if(b[i][j] == 4) {
if(b[i][j - 1] == 1 || b[i][j - 1] == 3) res += max(0ll, a[i][j] + a[i][j - 1]);
if(b[i - 1][j] == 1 || b[i - 1][j] == 2) res += max(0ll, a[i][j] + a[i - 1][j]);
}
// if(res / 2 > ans) {
// for(int i = 1; i <= n; i ++) {
// for(int j = 1; j <= m; j ++) cout << b[i][j] << " ";
// cout << "\n";
// }
// cout << res / 2 << "\n";
// }
ans = max(ans, res / 2); return;
}
void dfs(long long x, long long y) {
// cout << x << " " << y << "\n";
for(int i = 1; i <= 4; i ++) {
b[x][y] = i;
if(x == n && y == m) update();
else if(y == m) dfs(x + 1, 1);
else dfs(x, y + 1);
}
return;
}
int main() {
freopen("tunnel.in", "r", stdin);
freopen("tunnel.out", "w", stdout);
n = read(); m = read();
for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++) a[i][j] = read();
dfs(1, 1);
cout << ans << "\n";
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
#define MAXN 1005
long long read() {
long long x = 0; bool f = 0;
char ch = getchar();
while(ch < 48 || ch > 57) {
if(ch == 45) f = 1;
ch = getchar();
}
while(ch >= 48 && ch <= 57) {
x = (x << 3) + (x << 1) + (ch - 48);
ch = getchar();
}
return f ? -x : x;
}
long long n, m, a[MAXN][MAXN], f[MAXN][MAXN][2], g[MAXN][MAXN][2], ans = 0;
int main() {
freopen("tunnel.in", "r", stdin);
freopen("tunnel.out", "w", stdout);
n = read(); m = read();
for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++) a[i][j] = read();
for(int i = 2; i <= n; i ++) for(int j = 1; j <= m; j ++)
f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1] + max(0ll, a[i - 1][j] + a[i][j])),
f[i][j][1] = max(f[i - 1][j][0], f[i - 1][j][1]);
for(int i = 1; i <= n; i ++) for(int j = 2; j <= m; j ++)
g[i][j][0] = max(g[i][j - 1][0], g[i][j - 1][1] + max(0ll, a[i][j - 1] + a[i][j])),
g[i][j][1] = max(g[i][j - 1][0], g[i][j - 1][1]);
for(int i = 1; i <= m; i ++) ans += f[n][i][0];
for(int i = 1; i <= n; i ++) ans += g[i][m][0];
cout << ans << "\n";
return 0;
}
序列上的树(tree)
题目描述
输入格式
输出格式
\(\text{input1}\)
5
1 2 1 2 1
\(\text{output1}\)
8
\(\text{input2}\)
9
1 2 1 3 4 3 5 3 1
\(\text{output2}\)
15
样例解释
数据范围
#include<iostream>
using namespace std;
#define MAXN 200005
long long read() {
long long x = 0; bool f = 0;
char ch = getchar();
while(ch < 48 || ch > 57) {
if(ch == 45) f = 1;
ch = getchar();
}
while(ch >= 48 && ch <= 57) {
x = (x << 3) + (x << 1) + (ch - 48);
ch = getchar();
}
return f ? -x : x;
}
long long n, a[MAXN], ans = 0, cnt = 0, b[MAXN], p[MAXN];
bool c[MAXN];
int main() {
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
n = read();
for(int i = 1; i <= n; i ++) a[i] = read();
for(int i = 1; i <= n; i ++) {
b[++ cnt] = a[i];
if(b[cnt] == b[cnt - 2] && !c[b[cnt - 1]])
c[b[cnt - 1]] = true, cnt -= 2;
}
if(cnt == 1) {
for(int i = 1; i <= n; i ++) p[a[i]] ++;
for(int i = 1; i <= n; i ++) ans += (p[i] - 1) * p[i] / 2;
cout << ans + n << "\n";
return 0;
}
for(int l = 1; l < n - 1; l ++) for(int r = l + 2; r <= n; r ++) {
if(a[l] != a[r]) continue; cnt = 0;
for(int i = l; i <= r; i ++) c[a[i]] = false;
for(int i = l; i <= r; i ++) {
b[++ cnt] = a[i];
if(b[cnt] == b[cnt - 2] && !c[b[cnt - 1]])
c[b[cnt - 1]] = true, cnt -= 2;
}
if(cnt == 1) ans ++;
}
cout << ans + n << "\n";
return 0;
}
生存(survive)
题目描述
输入格式
输出格式
\(\text{input1}\)
3 2
1 2 3
1 2
2 3
1 2
2 1 2
\(\text{output1}\)
5
样例解释
数据范围
#include<iostream>
#include<vector>
using namespace std;
#define MAXN 1000005
long long read() {
long long x = 0; bool f = 0;
char ch = getchar();
while(ch < 48 || ch > 57) {
if(ch == 45) f = 1;
ch = getchar();
}
while(ch >= 48 && ch <= 57) {
x = (x << 3) + (x << 1) + (ch - 48);
ch = getchar();
}
return f ? -x : x;
}
long long n, m, a[MAXN], ans = 0;
int main() {
freopen("survive.in", "r", stdin);
freopen("survive.out", "w", stdout);
n = read(); m = read();
for(int i = 1; i <= n; i ++) a[i] = read();
for(int i = 1; i <= n; i ++) ans += a[i];
cout << ans << "\n";
return 0;
}
\[100 + 35 + 50 + 0 = 185
\]
本文来自博客园,作者:So_noSlack,转载请注明原文链接:https://www.cnblogs.com/So-noSlack/p/18338616