24年丽泽普及联考day13 TJ

· · 个人记录

24年丽泽普及联考day13

T1

跳石头

Description

有 n 块石头排成一排,从左到右编号为 1\sim n。开始时,有 n 只青蛙站在这些石头上,其中编号为 i 的青蛙站在第 i 块石头上。然后,它们同时起跳,编号为 i 的青蛙跳到了第 p_i 个石头上,p 为 1\sim n 的全排列。现在通过某种方式得到了以下信息:对所有 1\le i <n,有 a_i 只青蛙跳过了(i,i+1)的上方。请问你能否还原出合法的 p 序列?

Input

第一行一个正整数 n。

第二行 n-1 个整数 a_1 ,a_2,…,a_{n-1}。

Output

如果无解,输出 No。 否则在第一行输出 Yes,并在第二行输出 n 个整数,表示 p_1,p_2,…,p_n,用空格隔开。如果有多解请输出任意一组。

我们默认 a_0=a_n=0。首先可以发现 a 一定是偶数,且相邻两个 a 的差不超过 2。同时只要满足了这些条件,就一定能构造出一组解:

  • 如果 a_i=a_{i-1},则令 p_i=i;
  • 如果 a_i=a_{i-1}+2,则把 i 加进候选集合;
  • 如果 a_i=a_{i-1}-2,则取候选集合内的任意一点 j,令 p_i=j,p_j=i 即可。
    #include<bits/stdc++.h>
    using namespace std;
    #define F(a, b, c) for(int a = b;a <= c;a ++)
    #define fine cout << "No\n"
    const int N = 2e5;
    int a[N + 1];
    int n, m;
    int p[N + 1], l, s[N + 1];
    int main(){
    cin >> n;
    F(i, 1, n - 1)cin >> a[i];
    F(i, 1, n - 1)if (a[i] & 1 || a[i] - a[i - 1] > 2){fine;return 0;}
    F(i, 1, n){
    if (a[i] == a[i - 1])p[i] = i;
    else if (a[i] == a[i - 1] + 2)s[++ l] = i;
    else p[i] = s[l], p[s[l]] = i, l --; 
    }
    cout << "Yes\n";
    F(i, 1, n)cout << p[i] << " ";
    }

    T2

    树

Description

给定一棵 n 个点的树,节点编号为 1\sim n。初始时,每个点有一个 0 或 1 的权值。你可以通过某种操作改变这些节点的权值。具体的,在一步操作中,你可以选定一个节点 x,并把所有满足 “x 到 v 的路径上所有点的权值与 x 相同” 的点 v 的权值取反。请问你最少需要多少次操作才能使所有节点的权值相同?

Input

第一行一个正整数 n。

第二行 n 个数 0 或 1,表示节点的初始权值。

接下来 n-1 行,每行两个正整数 x,y,表示 x 与 y 之间有一条边。

Output

一行一个整数,表示答案。

一次操作就是把一个同色连通块内的点取反。把同色连通块缩成一个点,那么

ans = 树的直径长度 \div 2

这肯定是答案的下界,因为一次操作最多令直径长度 - 2,同时不断地操作直径中心点可以取到这个下界。

#include<bits/stdc++.h>
using namespace std;
#define F(a, b, c) for(int a = b;a <= c;a ++)
const int N = 5e5;
int n;
struct Edge{
    int to, next;
}edge[N << 2];
int head[N + 1], ppp;
int x, y, maxn;
void add(int x, int y){
    edge[++ ppp] = {y, head[x]};
    head[x] = ppp;
}
int vis[N + 1], w[N + 1], l[N + 1];
int st;
void dfs1(int u, int pre){
    for(int i = head[u];i;i = edge[i].next){
        int v = edge[i].to;
        if (v == pre || vis[v])continue;
        if (w[st] == w[v])dfs1(v, u);
        else if (pre) add(st, v), add(v, st);
    }
    if (pre)vis[u] = 1;
}
void work(int u){
    st = u;
    dfs1(u, 0);
}
void dfs(int u, int pre){
    l[u] = l[pre] + 1;
    for(int i = head[u];i;i = edge[i].next){
        int v = edge[i].to;
        if (v != pre && !vis[v])dfs(v, u);
    }
}
int main(){
    cin >> n;
    F(i, 1, n)cin >> w[i];
    F(i, 1, n - 1)cin >> x >> y, add(x, y), add(y, x);
    F(i, 1, n)if (!vis[i])work(i);
    dfs(1, 0);x = 0;
    F(i, 1, n)(maxn < l[i] ? (maxn = l[i], x = i) : 0), l[i] = 0;
    dfs(x, 0);
    F(i, 1, n)(maxn < l[i] ? (maxn = l[i], x = i) : 0);
    cout << maxn / 2;
}

T3

背包

把物品按重量排好序,如果没有背包的限制,问题就是求一个最长上升子序列。同时发现把背包按容量排好序,最优解肯定是选择最靠后的若干个背包。考虑对物品从后往前 dp,设 F_i 表示当前最靠前的物品价值为 i 最多能取多少个物品,同时需要满足能装进最大的若干个背包里。用树状数组优化 dp 即可。

#include<bits/stdc++.h>
using namespace std;
#define F(a, b, c) for(int a = b;a <= c;a ++)
const int N = 1e5, inf = 0x3fffffff;
int n, m, cnt, p, mid, l, r, T;
int t[N + 1], s[N + 1];
struct node{
int w, v;
bool operator < (const node &x) const{
return w == x.w ? v > x.v : w > x.w;
}
}a[N + 1];
bool cmp(int x, int y){return x > y;}
void work(){
cnt = p = 0;
cin >> n;
F(i, 1, n)cin >> a[i].w >> a[i].v;
cin >> m;
F(i, 1, m)cin >> t[i];
sort(a + 1, a + n + 1);
sort(t + 1, t + m + 1, cmp);
s[0] = inf;
F(i, 1, n){
while(p < m && t[p + 1] >= a[i].w) p ++;
if(a[i].v <= s[cnt]){
if(cnt < p)s[++ cnt] = a[i].v;
}
else{
l = 0, r = cnt;
while(l < r){
mid = (l + r) / 2;
if(s[mid] >= a[i].v)l = mid + 1;
else r = mid;
}
s[r] = a[i].v;
}
}
cout << cnt << "\n";
}
int main(){
cin >> T;
while(T --) work();
}

T4

蚂蚁 这个问题和树没啥关系。考虑每只蚂蚁所在节点的深度 d,把问题看成为每只蚂蚁钦定一个\ge d 的到达时刻,并且没有两只蚂蚁的到达时刻相等。

记深度为 i 的蚂蚁个数为 cnt_i,考虑一种显然的贪心:从小到大枚举时刻 i,先往栈里加入 cnt_i只蚂蚁,如果栈非空就从栈中弹出一只蚂蚁,令这只蚂蚁的到达时刻为 i。接下来有很多种做法:可以考虑用线段树维护 p_i表示考虑完时刻 i 之后栈中还剩多少只蚂蚁,只需要线段树二分查找某个位置后第一次出现连续两个 0 的位置和某个位置后第一个 0 即可;也可以考虑最后一段栈非空的时刻,答案显然是 \max (i+suf_i-1),suf 是 cnt 的后缀和,直接用线段树维护即可...

T5

划分

Description

给定一棵 n 个点的树,节点编号为 1 \sim n。要求你将其划分为三个非空的连通块 A、B、C,使得 |A| = |B|,且 |A| 尽可能的大(|A|表示连通块 A 中的点数)。请你求出在最优的划分方式下 |A| 的值。

Input

第一行一个正整数 n 。

接下来 n-1 行,每行两个数 u,v 表示点 u 和点 v 之间有一条边。

Output

一行一个整数,表示答案。

把一个连通块缩成一个点,这棵三个点的树肯定有一个叶子。这个叶子代表的连通块肯定是原树的一个子树,枚举这个子树的根节点 x。另外两个连通块肯定其中一个根为 1,考虑另外一个连通块的根 y 的子树大小是多少。

  1. 如果 size_x=|A|:
  • 若 y 不是 x 的祖先,则 size_y=size_x 或 size_y = n-size_x\times 2
  • 若 y 是 x 的祖先,则 size_y=size_x\times 2 或 size_y = n - size_x
  1. 如果 size_x=|C|:
  • 若 y 不是 x 的祖先,则 size_y=(n-size_x)\div2
  • 若 y 是 x 的祖先,则 size_y=(n+size_x)\div2

分类讨论一番之后发现只需要对每个点 x 求出:在 x 子树外且不是 x 的祖先的点中是否存在 size 为某个值的点,只需要 dfs 一遍即可。

#include<bits/stdc++.h> 
using namespace std;
#define F(a, b, c) for(int a = b;a <= c;a ++)
int n, ans;
const int N = 1e6;
int x, y;
vector<int>ed[N + 1];
int siz[N + 1];
int all[N + 1], son[N + 1], fa[N + 1];
void dfs1(int x, int fat){
    siz[x] = 1;
    for(int i = 0;i < ed[x].size();i ++){
        if(ed[x][i] != fat){
            dfs1(ed[x][i], x);siz[x] += siz[ed[x][i]];
        }
    }
    all[siz[x]]++;
}
void dfs2(int x, int fat){
    if (siz[x] * 2 < n && fa[2 * siz[x]] != 0) ans = max(ans, siz[x]);
    else if (fa[n - siz[x]] != 0 && 2 * siz[x] < n) ans = max(ans, siz[x]);
    if ((n + siz[x]) % 2 == 0 && fa[(n + siz[x]) / 2] != 0)ans = max(ans, (n - siz[x]) / 2);
    int t1 = son[siz[x]];
    int t2 = -1e9 + 888;
    if (2 * siz[x] < n) t2 = son[n - 2 * siz[x]];
    int t3 = -1e9 + 666;
    if ((n - siz[x]) % 2 == 0) t3 = son[(n - siz[x]) / 2];
    son[siz[x]] ++; fa[siz[x]] ++;
    for(int i = 0;i < ed[x].size();i ++){
        if (ed[x][i] == fat) continue;
        dfs2(ed[x][i], x);
    }
    t1 = (all[siz[x]] - son[siz[x]] + t1);
    if (t2 != -1e9 + 888) t2 = (all[n - siz[x] * 2] - son[n - siz[x] * 2] + t2);
    if (t3 != -1e9 + 666) t3 = (all[(n - siz[x]) / 2] - son[(n - siz[x]) / 2] + t3);
    if ((t1 > 0 || t2 > 0) && 2 * siz[x] < n) ans = max(ans, siz[x]);
    if (t3 > 0) ans = max(ans, (n - siz[x]) / 2);
    fa[siz[x]] --;
}
int main(){
    cin >> n;
    F(i, 1, n - 1){
        cin >> x >> y;
        ed[x].push_back(y);
        ed[y].push_back(x);
    }
    dfs1(1, 0);dfs2(1, 0);
    cout << ans;
}