CSP-J2020题解

A 优秀的拆分

显然每个数只有一种分法。从大到小枚举(2)的正整数次幂,能拆则拆。

若未拆完,则是无解情况。

#include <bits/stdc++.h>

using namespace std;

int ans[50], l;

int main()
{
	freopen("power.in", "r", stdin);
	freopen("power.out", "w", stdout);
	int n;
	scanf("%d", &n);
	for (int i = 24; i && n; i--)
		if (n >= (1 << i))
			n -= (1 << i), ans[++l] = (1 << i);
	if (n)
	{
		puts("-1"); return 0;
	}
	for (int i = 1; i <= l; i++)
		printf("%d ", ans[i]);
	return 0;
}

B 直播获奖

观察到(a_i)不超过(600),可以直接开桶记录每个分数有多少人。

枚举分数线。若当前人数大于等于获奖人数,该分数线即为答案。

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5, M = 600;

int n, w;
int a[M + 5];

int main()
{
	freopen("live.in", "r", stdin);
	freopen("live.out", "w", stdout);
	scanf("%d%d", &n, &w);
	for (int i = 1; i <= n; i++)
	{
		int x, s = 0, t = max(1, i * w / 100);
		scanf("%d", &x), a[x]++;
		for (int j = M; j >= 0; j--)
		{
			s += a[j];
			if (s >= t) { printf("%d ", j); break; } 
		}
	}
	return 0;
}

C 表达式

首先建出表达式树。显然变量为叶子节点,符号为非叶子节点。记(f_i)(i)节点改变是否有影响,转移按符号分类讨论即可。

#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 5;

int e[N][2], vl[N], id[N], a[N], st[N], f[N];
int n, top, cnt;
char s[N];

void dfs(int x)
{
    int y0 = e[x][0], y1 = e[x][1];
    if (f[x] == -1)
    {
        if (vl[y0] && vl[y1]) dfs(y0), dfs(y1);
        if (!vl[y0] && vl[y1]) dfs(y0);
        if (vl[y0] && !vl[y1]) dfs(y1);
    }
    else if (f[x] == -2)
    {
        if (!vl[y0] && !vl[y1]) dfs(y0), dfs(y1);
        if (vl[y0] && !vl[y1]) dfs(y0);
        if (!vl[y0] && vl[y1]) dfs(y1);
    }
    else if (f[x] == -3) dfs(y0);
    else f[x] = 1;
}

int main()
{
    int q, x, y, l;
    gets(s), l = strlen(s);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 0; i < l; i++)
        if (s[i] == 'x')
        {
            i++;
            for (x = 0; s[i] != ' ' && i < l; i++) x = x * 10 + s[i] - '0';
            vl[id[x] = st[++top] = ++cnt] = a[x];
        }
        else if (s[i] == '&')
            cnt++, vl[cnt] = vl[e[cnt][0] = st[top--]] & vl[e[cnt][1] = st[top--]], f[st[++top] = cnt] = -1;
        else if (s[i] == '|')
            cnt++, vl[cnt] = vl[e[cnt][0] = st[top--]] | vl[e[cnt][1] = st[top--]], f[st[++top] = cnt] = -2;
        else if (s[i] == '!')
            cnt++, vl[cnt] = !vl[e[cnt][0] = st[top--]], f[st[++top] = cnt] = -3;
    dfs(cnt);
    for (scanf("%d", &q); q--;)
        scanf("%d", &x), printf("%d
", vl[cnt] ^ f[id[x]]);
    return 0;
}

D 方格取数

(f_{i,j})为小能走到((i,j))这个点答案最大能多少。

注意到小能只能在横方向只能向右走,可以先枚举j。

转移时先将初值从左边转移。

再从上往下、从下往上更新(f)。(其中一次需要开临时数组)

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1005;

int a[N][N];
ll f[N][N], g[N][N];
int n, m;

int main()
{
	freopen("number.in", "r", stdin);
	freopen("number.out", "w", stdout);
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			scanf("%d", &a[i][j]);
	for (int i = 0; i <= n + 1; i++)
		for (int j = 0; j <= m + 1; j++)
			g[i][j] = f[i][j] = -1145141919810;
	f[1][1] = a[1][1];
	for (int j = 1; j <= m; j++)
	{
		for (int i = 1; i <= n; i++) g[i][j] = f[i][j] = max(f[i][j], f[i][j - 1] + a[i][j]);
		for (int i = 1; i <= n; i++) f[i][j] = max(f[i][j], f[i - 1][j] + a[i][j]);
		for (int i = n; i; i--) g[i][j] = max(g[i][j], g[i + 1][j] + a[i][j]);
		for (int i = 1; i <= n; i++) f[i][j] = max(f[i][j], g[i][j]);
	}
	printf("%lld
", f[n][m]);
	return 0;
}
原文地址:https://www.cnblogs.com/oply/p/13949012.html