2019.1.12(寒假热身)
问题 A: ><
时间限制: 1 Sec 内存限制: 128 MB
题目描述
Given is a string S of length N−1. Each character in S is < or >.
A sequence of N non-negative integers, a1,a2,⋯,aN, is said to be good when the following condition is satisfied for all i (1≤i≤N−1):
·If S i= ‘<‘: a i<a i+1
·If S i= ‘>’: a i>a i+1
Find the minimum possible sum of the elements of a good sequence of N non-negative integers.
A sequence of N non-negative integers, a1,a2,⋯,aN, is said to be good when the following condition is satisfied for all i (1≤i≤N−1):
·If S i= ‘<‘: a i<a i+1
·If S i= ‘>’: a i>a i+1
Find the minimum possible sum of the elements of a good sequence of N non-negative integers.
Constraints
·2≤N≤5×105
·S is a string of length N−1 consisting of < and >.
输入
Input is given from Standard Input in the following format:
S
S
输出
Find the minimum possible sum of the elements of a good sequence of N non-negative integers.
样例输入 Copy
<>>
样例输出 Copy
3
提示
a=(0,2,1,0) is a good sequence whose sum is 3. There is no good sequence whose sum is less than 3.
题解:A题,找到每一个波谷,然后dfs。感觉我写这种暴力的代码还是不太擅长。
问题 C: Neither AB nor BA
时间限制: 1 Sec 内存限制: 512 MB
题目描述
Find the number of strings s of length N consisting of ‘A’, ‘B’, and ‘C’ that satisfy the following condition:
·s can be converted to the empty string by repeating the following operation:
·s can be converted to the empty string by repeating the following operation:
·Choose two consecutive characters in s and erase them. However, choosing ‘AB’ or ‘BA’ is not allowed.
For example, ‘ABBC’ satisfies the condition for N=4, because we can convert it as follows: ‘ABBC’ → (erase ‘B’B) → ‘AC’ → (erase ‘AC’) → ‘(empty)’.
The answer can be enormous, so compute the count modulo 998244353.
Constraints
·2≤N≤107
·N is an even number.
输入
Input is given from Standard Input in the following format:
N
N
输出
Print the number of strings that satisfy the conditions, modulo 998244353.
样例输入 Copy
2
样例输出 Copy
7
提示
Except ‘AB’ and ‘BA’, all possible strings satisfy the conditions.
C题算是一种思维吧,总的组合肯定是3^n,只要是A或者B的个数大于n/2,无解。所以只需要枚举n/2+1—n个A或者B.
然后剩下的B,C,还有n-i个.乘以2^n-i.
再说一下关键的,题目的意思是不能选择AB or BA,不是不能选AB和BA.
删除两个字符,奇偶性不变。如果AB中任何一个大于n/2,那么必然会遇到删不完的情况。
//A题代码
#include<bits/stdc++.h>
#define rep(i, a, b) for(int i=(a);i<=(b);i++)
#define rop(i, a, b) for(int i=(a);i<(b);i++)
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
string s;
int a[maxn];
int len;
void dfs(int x) {
if (x < 0 || x > len) return;
if (x > 0 && x <= len && s[x] == '>') {
if (a[x - 1] < a[x] + 1) {
a[x - 1] = a[x] + 1;
dfs(x - 1);
}
}
if (x >= 0 && x < len && s[x + 1] == '<') {
if (a[x + 1] < a[x] + 1) {
a[x + 1] = a[x] + 1;
dfs(x + 1);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
s += '1';
string s1;
cin >> s1;
s = s + s1;
len = s.length() - 1;
if (s[1] == '<') a[0] = 1;
if (s[len] == '>') a[len] = 1;
rop(i, 1, len) if (s[i] == '>' && s[i + 1] == '<') a[i] = 1;
rep(i, 0, len) if (a[i] == 1) dfs(i);
ll ans = 0;
rep(i, 0, len) {
ans += a[i] - 1;
}
cout << ans << endl;
return 0;
}
//C -code
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define rep(i, a, b) for(int i=(a);i<=(b);i++)
#define rop(i, a, b) for(int i=(a);i<(b);i++)
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int maxn = 1e7+10;
ll f[maxn];
ll p[maxn];
ll qpow(ll a,ll b) {
ll ans = 1;
while (b) {
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b/=2;
}
return ans%mod;
}
void F() {
f[0]=1;
p[0]=1;
rep(i,1,maxn-5) {
f[i]=f[i-1]*i%mod;
p[i]=p[i-1]*2%mod;
}
}
ll C(ll x,ll y){
ll ans=f[x]%mod*qpow(f[y]*f[x-y]%mod,mod-2)%mod;
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
F();
int n;
cin>>n;
ll ans = qpow(3,n);
rep(i,n/2+1,n) {
ans=(ans-2*C(n,i)%mod*p[n-i]%mod+mod)%mod;
}
cout<<ans<<endl;
return 0;
}