好像一不小心出难了?
由于 没有想到大家都不会想拆贡献因此第二档可能放少了。
题解
预期最高分
实际最高分
预期中位分
实际中位分
A
送分题
其实不嫩发现,每次让一个 加一就会对答案贡献 ,我们就希望这个尽可能的小
不难发现在 增大时, 也是会增大的,所以我们每次直接贪心找最小值一定最优
由于 很小可以直接用优先队列做,时间复杂度
尝试
std
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
priority_queue<int, vector<int>, greater<int> > q;
int n, k, a[N];
char c1[100], c2[100];
int main() {
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i], q.push(a[i]);
while(k--) {
int x = q.top();
q.pop();
q.push(x + 1);
}
for(int i = 1; i <= n; i++) cout << q.top() << " ", q.pop();
return 0;
}
B
脑电波题
现场画图
首先看到 最大可以到 量级,那么我们考虑二进制拆分
这样就可以做到很多个二的次幂相加。但是这样的话你的长就肯定会超过 ,最长可以到
那么考虑加大进制的底数,比如六进制,配一下系数即可
std
#include <bits/stdc++.h>
using namespace std;
int mp[35][35] =
{
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0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 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
} ;
pair<int, int> ttm[20] =
{
{28, 0},
{2, 28},
{28, 4},
{6, 28},
{28, 8},
{10, 28},
{28, 12},
{14, 28},
{28, 16},
{18, 28},
{28, 20},
{22, 28},
{28, 24}
} ;
int main() {
int n;
cin >> n;
for(int i = 0; i < 13; i++) {
int tp = n % 6, xx = ttm[i].first, yy = ttm[i].second;
n /= 6;
if(i % 2 == 0) {
if(tp == 0) mp[xx][yy] = 0;
if(tp == 2) mp[xx][yy + 1] = 1;
if(tp == 3) mp[xx][yy + 1] = mp[xx - 1][yy + 1] = 1;
if(tp == 4) mp[xx][yy + 1] = mp[xx - 1][yy + 1] = mp[xx - 2][yy + 1] = 1;
if(tp == 5) mp[xx][yy + 1] = mp[xx - 1][yy + 1] = mp[xx][yy + 2] = 1;
} else {
if(tp == 0) mp[xx][yy] = 0;
if(tp == 2) mp[xx + 1][yy] = 1;
if(tp == 3) mp[xx + 1][yy] = mp[xx + 1][yy - 1] = 1;
if(tp == 4) mp[xx + 1][yy] = mp[xx + 1][yy - 1] = mp[xx + 1][yy - 2] = 1;
if(tp == 5) mp[xx + 1][yy] = mp[xx + 1][yy - 1] = mp[xx + 2][yy] = 1;
}
}
for(int i = 0; i < 30; i++) {
for(int j = 0; j < 30; j++) {
mp[i][j] = 1;
cout << mp[i][j];
if(j != 29) cout << " ";
}
if(i != 29) cout << "\n";
}
return 0;
}
C
数数、送分题
全排列,考虑拆贡献
对于两个位置 ,如果 那么你不管怎么样都不会有贡献
如果 那么在所有的填发里恰好有一半是有贡献的,这个也是容易理解的
如果 那么只有 填比 小的并且没出现过的数, 同理
都不等于可以直接单独提溜出来做三位偏序
那么直接 枚举,期望得分
然后你发现两个位置都是 这个可以换成求 ,本质上是一个逆序对
然后 的情况可以直接枚举 并且记录 的位置中有多少个小于 ,这个可以树状数组做到
直接 分治就有一个 的做法,期望得分
如果卡常卡到极限那么最后一档也可以冲过去
瓶颈在于求三位偏序,不难发现这个三位偏序很特别,因为他每个维度里的数都互不相同
我们将 分别做二维偏序
那么对于一个三位偏序的对 它会被恰好计算三次,而如果不是三位偏序的对则至多也恰好只会被计算一次
那么直接将三个二位偏序的结果加起来,减去对子数,再除二就得到了三位偏序的个数。
时间复杂度 ,期望得分
std
#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
void read(int &x) {
x = 0;
int f = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + (c - 48);
c = getchar();
}
x *= f;
}
inline void write(int x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const int N = 1e6 + 5, mod = 998244353;
int n, a[N], b[N], bit[N], tot, ans;
struct node {
int a, b;
bool operator< (const node &T) const {
return a < T.a;
}
} c[N];
int inv(int x) {
int as = 1, tm = mod - 2;
while(tm) {
if(tm & 1) as = (as * x) % mod;
x = (x * x) % mod, tm >>= 1;
}
return as;
}
int query(int x) {
int tot = 0;
for(int i = x; i; i -= i & -i) tot += bit[i];
return tot;
}
void add(int x, int k) {
for(int i = x; i <= n; i += i & -i) bit[i] += k;
}
void wrk(int m) {
for(int i = 1; i <= m; i++) ans += query(c[i].a), ans %= mod, add(c[i].a, 1);
for(int i = 1; i <= n; i++) bit[i] = 0;
for(int i = 1; i <= m; i++) ans += query(c[i].b), ans %= mod, add(c[i].b, 1);
for(int i = 1; i <= n; i++) bit[i] = 0;
sort(c + 1, c + m + 1);
for(int i = 1; i <= m; i++) ans += query(c[i].b), ans %= mod, add(c[i].b, 1);
for(int i = 1; i <= n; i++) bit[i] = 0;
ans = (ans - (m * (m - 1) / 2) % mod + mod) * inv(2) % mod;
}
namespace sub1 {
int fc1, fc2, fr[N], e[N];
void work() {
fc1 = 1;
for(int i = 1; i < tot; i++) fc1 = fc1 * i % mod;
fc2 = fc1 * tot % mod;
for(int i = 1; i <= n; i++) e[i] = b[i];
sort(e + 1, e + n + 1);
int x = 1;
while(e[x] == -1 && x <= n) x++;
for(int i = 1; i <= n; i++) {
if(e[x] == i) fr[i] = fr[i - 1], x++;
else fr[i] = fr[i - 1] + 1;
}
for(int i = 1; i < n; i++) {
for(int j = i + 1; j <= n; j++) {
if(a[i] > a[j]) continue;
if(b[i] == b[j]) {
ans = (ans + fc2 * inv(2) % mod) % mod;
} else if(b[i] == -1) {
ans = (ans + fc1 * fr[b[j]] % mod) % mod;
} else if(b[j] == -1) {
ans = (ans + fc1 * (tot - fr[b[i]]) % mod) % mod;
} else if(b[i] < b[j]) {
ans = (ans + fc2) % mod;
}
}
}
ans = ans * inv(fc2) % mod;
}
}
namespace sub2 {
void work() {
for(int i = 1; i <= n; i++) c[i] = {a[i], b[i]};
wrk(n);
}
}
namespace sub3 {
int cnt, e[N], fr[N];
void work() {
for(int i = 1; i <= n; i++) e[i] = b[i];
sort(e + 1, e + n + 1);
int x = 1;
while(e[x] == -1 && x <= n) x++;
for(int i = 1; i <= n; i++) {
if(e[x] == i) fr[i] = fr[i - 1], x++;
else fr[i] = fr[i - 1] + 1;
}
int inv2 = inv(2), invtot = inv(tot);
for(int i = 1; i <= n; i++) if(b[i] != -1) c[++cnt] = {a[i], b[i]};
wrk(cnt);
for(int i = 1; i <= n; i++) {
if(b[i] == -1) {
ans = (ans + inv2 * query(a[i]) % mod) % mod;
add(a[i], 1);
}
}
for(int i = 1; i <= n; i++) bit[i] = 0;
for(int i = 1; i <= n; i++) {
if(b[i] == -1) add(a[i], 1);
else ans = (ans + ((invtot * query(a[i])) % mod) * fr[b[i]] % mod) % mod;
}
for(int i = 1; i <= n; i++) bit[i] = 0;
for(int i = n; i >= 1; i--) {
if(b[i] == -1) add(a[i], 1);
else ans = (ans + ((invtot * (query(n) - query(a[i]))) % mod) * (tot - fr[b[i]]) % mod) % mod;
}
}
}
signed main() {
read(n);
for(int i = 1; i <= n; i++) read(a[i]);
for(int i = 1; i <= n; i++) read(b[i]), tot += (b[i] == -1);
if(n <= 5000 && tot) { //sub 1 2
sub1::work();
} else if (!tot) { //sub 3 5
sub2::work();
} else { //sub 4 6
sub3::work();
}
write(ans % mod);
return 0;
}
D
先判断 。如果一个括号序列 是 ”()“,那么答案第一行一定是 ,否则一定可以找到一个正则括号序列使得 不是它的子串。
接下来要证明一个推论,答案的第一行 。构造两个字符串, ”()()…()”, ”((((…(())…))))”, 长度都是 。对于每一个 ,如果 ”((” 或 ”))” 是 的字串,那么 一定不是 的字串。如果 ”((” 或 ”))” 都不是 的字串,那么 一定是 ”)()()(…”,没有连续的 ’(’ 或 ’)‘,那么 一定不是 的字串。
所以这道题就变成了判断是否存在一个长度为 的正则括号序列 使得 都不是它的字串。
由于是多个字符串在一个字符串中是否出现的问题,所以考虑AC自动机上dp。
每个 在AC自动机的结束节点是危险节点。如果一个节点的父亲或者fail是危险节点的话,那么这个节点也是危险节点。
定义状态 表示是否存在长度为 ,在AC自动机的第 个节点上,’(’ ’)’ 的值是 的字符串。
怎么转移呢?如果 且 节点不是危险节点,那么 ,如果 ,那么 。
最后判断是否存在一个 使得 且节点 不是危险节点。如果存在,那么第一行答案 ,并输出路径。如果不存在,那么答案第一行 ,输出字符串 ,并输出对应的分组。
总复杂度 。
最后说一下每个 的做法。
暴力枚举。
建立kmp自动机。定义状态 表示是否存在长度为 ,在kmp自动机的第 个节点上,’(’ ’)’ 的值是 的字符串。最后输出路径。
由于 数据比较弱,乱搞也能过这个部分分。
建立 个kmp自动机。定义状态 表示是否存在长度为 ,在第 个kmp自动机的第 个节点上,在第 个kmp自动机的第 个节点上,’(’ ’)’ 的值是 的字符串。如果最后存在,输出路径。如果不存在,用 的做法分别给 和 做一遍dp。
std
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
char c;
int son[2],fa,fail,type;
}t[2510];
struct node1{
bool f;
int x;
char c;
}dp[2510][60][60];//dp状态和题解中是反的
int n,k,tlen,ans = -1,idx[60],dfn[2510],dfnn,siz[2510],T[2510],f[60];
vector<int> v[2510];
int read(){
int k = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-') f = -1;
c = getchar();
}
while('0' <= c && c <= '9'){
k = k * 10 + c - '0';
c = getchar();
}
return k * f;
}
int id(char c){
return (c == '(') ? 0 : 1;
}
void bfs(){//构造AC自动机
queue<int> q;
for(int i = 0;i <= 1;i++){
if(t[0].son[i]){
q.push(t[0].son[i]);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
if(t[u].fa == 0){
t[u].fail = 0;
}else{
t[u].fail = t[t[t[u].fa].fail].son[id(t[u].c)];
}
v[t[u].fail].push_back(u);
t[u].type |= (t[t[u].fa].type | t[t[u].fail].type);
for(int i = 0;i <= 1;i++){
if(t[u].son[i]){
q.push(t[u].son[i]);
}else{
t[u].son[i] = t[t[u].fail].son[i];
}
}
}
}
void ansdfs(int x,int y,int z){
if(y == 0) return;
if(dp[x][y][z].c == '('){
ansdfs(dp[x][y][z].x,y - 1,z - 1);
}else{
ansdfs(dp[x][y][z].x,y - 1,z + 1);
}
putchar(dp[x][y][z].c);
}
void dfs(int u,int fa){
dfn[u] = ++dfnn;
siz[u] = 1;
for(int i = 0;i < v[u].size();i++){
if(v[u][i] != fa){
dfs(v[u][i],u);
siz[u] += siz[v[u][i]];
}
}
}
int lowbit(int x){
return x & -x;
}
void add(int x,int k){
for(;x <= tlen + 1;x += lowbit(x)){
T[x] += k;
}
}
int query(int x){
int sum = 0;
for(;x > 0;x -= lowbit(x)){
sum += T[x];
}
return sum;
}
signed main(){
n = read(),k = read();
for(int i = 1;i <= n;i++){//输入+构造AC自动机
string s;
cin >> s;
if(s == "()" || s == "(" || s == ")"){
cout << -1 << endl;
return 0;
}
int p = 0;
for(int j = 0;j < s.size();j++){
if(t[p].son[id(s[j])]){
p = t[p].son[id(s[j])];
}else{
tlen++;
t[p].son[id(s[j])] = tlen;
t[tlen].fa = p;
t[tlen].c = s[j];
p = tlen;
}
}
idx[i] = p;
t[p].type = 1;
}
bfs();
dp[0][0][0].f = 1;
for(int j = 0;j <= k;j++){//dp转移
for(int i = 0;i <= tlen;i++){
if(t[i].type) continue;
for(int l = 0;l <= k;l++){
if(!dp[i][j][l].f) continue;
if(j == k && l == 0) ans = i;
dp[t[i].son[0]][j + 1][l + 1] = {1,i,'('};
if(l >= 1) dp[t[i].son[1]][j + 1][l - 1] = {1,i,')'};
}
}
}
if(ans != -1){
cout << 1 << endl;
ansdfs(ans,k,0);//输出字符串
cout << endl;
cout << n << endl;
for(int i = 1;i <= n;i++) cout << i << " ";
cout << endl;
}else{
dfs(0,0);
string ans1 = "",ans2 = "";
for(int i = 1;i <= k;i++){//构造字符串
if(i <= k / 2){
ans1 += "(";
}else{
ans1 += ")";
}
if(i % 2 == 1){
ans2 += "(";
}else{
ans2 += ")";
}
}
int p = 0;
for(int i = 0;i < ans1.size();i++){//构造组内的字符串
p = t[p].son[id(ans1[i])];
add(dfn[p],1);
}
int cnt = 0;
for(int i = 1;i <= n;i++){
int x = query(dfn[idx[i]] + siz[idx[i]] - 1) - query(dfn[idx[i]] - 1);
if(!x){
cnt++;
f[i] = 1;
}
}
cout << 2 << endl;
cout << ans1 << endl;//输出方案
cout << cnt << endl;
for(int i = 1;i <= n;i++) if(f[i]) cout << i << " ";
cout << endl;
cout << ans2 << endl;
cout << n - cnt << endl;
for(int i = 1;i <= n;i++) if(!f[i]) cout << i << " ";
cout << endl;
}
return 0;
}