CSP-2022-J 复赛题解

\(\large \texttt{T1 P8813 [CSP-J 2022] 乘方}\)

原题链接

#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
int a, b, c;
int pow(int a, int b)
{
    int ans = 1;
    for (int i = 1;i <= b;i ++)
    {
        ans *= a;
        if (ans > 1e9) return -1;
    }
    return ans;
}
signed main()
{
    scanf("%lld %lld",&a,&b);
    cout << pow(a, b);
}

\(\large \texttt{T2 P8814 [CSP-J 2022] 解密}\)

原题链接

#include<iostream>
#include<cmath>
#define int long long
using namespace std;
int k, n, d, e;
int check(int y) {int x = sqrt(y); return x * x == y; }
void solve(int n, int d, int e)
{
	int a = n + 2 - e * d;
	int b = e * d;
	int c = a + b - 2;
	if (a * a - 4 * c >= 0 && check(a * a - 4 * c))
	{
		int t, p, q;
		t = a + sqrt(a * a - 4 * c);
		if (t % 2) 
		{
			cout << "NO\n";
			return;
		}
		q = t / 2, p = a - q;
		if (p > q) 
		{
			cout << "NO\n";
			return;
		}
		cout << p << " " << q << endl;
		return;
	}
	cout << "NO\n";
}
signed main(void)
{
	cin >> k;
	while (k --)
	{
		cin >> n >> d >> e;
		solve(n, d, e);
	}
}

\(\large \texttt{T3 P8815 [CSP-J 2022] 逻辑表达式}\)

原题链接

#include<bits/stdc++.h>
using namespace std;
struct node {
	int value, cnt_or, cnt_and;
};
stack<node> str;
string expr;
int getpriority(char a)
{
	if (a == '(') return 0;
	if (a == '|') return 1;
	return 2;
}
string turn_into(string expr)
{
	string ret;
	stack<char> st;
	for (auto c:expr)
	{
		if (c == '(') st.push(c);
		else if (c == ')')
		{
			while (st.top() != '(')
			{
				ret.push_back(st.top());
				st.pop();
			}
			st.pop();
		}
		else if (c == '&' || c == '|')
		{
			while (!st.empty() && getpriority(c) <= getpriority(st.top()))
			{
				ret.push_back(st.top());
				st.pop();
			}
			st.push(c);
		}
		else ret.push_back(c);
	}
	while (!st.empty())
	{
		ret.push_back(st.top());
		st.pop();
	}
	return ret;
}
int main()
{
	cin >> expr;
	string skl = turn_into(expr);
	for (auto c:skl)
	{
		if (c == '|')
		{
			node r = str.top();str.pop();
			node l = str.top();str.pop();
			str.push({l.value | r.value, l.cnt_or + (l.value == 1 ? 1 : r.cnt_or), l.cnt_and + (l.value == 1 ? 0 : r.cnt_and)});
		}
		else if (c == '&')
		{
			node r = str.top();str.pop();
			node l = str.top();str.pop();
			str.push({l.value & r.value, l.cnt_or + (l.value == 0 ? 0 : r.cnt_or),l. cnt_and + (l.value == 0 ? 1 : r.cnt_and)});
		}
		else str.push({c - '0', 0, 0});
	}
	node ans = str.top();
	cout << ans.value << endl;
	cout << ans.cnt_and << " " << ans.cnt_or << endl;
	return 0;
}

\(\large \texttt{T4 P8816 [CSP-J 2022] 上升点列}\)

原题链接

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct _pnt {
	int x, y;
}a[1010];
int n, k, ans = 0;
int f[550][550];
int dis(int i, int j) { return abs(a[i].x - a[j].x) + abs(a[i].y - a[j].y) - 1; }
bool cmp(_pnt p1, _pnt p2) {
	if (p1.x == p2.x) return p1.y < p2.y;
	else return p1.x < p2.x;
}
int main()
{
	scanf("%d %d", &n, &k);
	for (int i = 1;i <= n;i ++) scanf("%d %d", &a[i].x, &a[i].y);
	sort(a + 1, a + n + 1, cmp);
	for (int i = 1;i <= n;i ++)
	{
		int x = a[i].x;
		int y = a[i].y;
		for (int z = 0;z <= k;z ++)
		{
			f[i][z] = 1;
			for (int j = 1;j <= n;j ++)
			{
				if (i != j && a[j].x <= x && a[j].y <= y)
				{
					int len = dis(i, j);
					if (z >= len)
					{
						f[i][z] = max(f[i][z], f[j][z - len] + len + 1);
					}
				}
			}
			ans = max(ans, f[i][z] + k - z);
		}
	}
	printf("%d\n", ans);
	return 0;
}
posted @ 2022-11-24 20:00  血色湖  阅读(322)  评论(0编辑  收藏  举报