湖南集训 Day2

51 篇文章 0 订阅
45 篇文章 0 订阅

湖南集训Day2

T1

这里写图片描述
这里写图片描述
这里写图片描述

//N*K模拟都会吧 然后转成N+K做法,然后发现奇数位的223233改变的时候
//都是循环的,,然后你再和N*K的做法对拍个十万八千组
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 1000005
using namespace std;
char s[MAXN];
int main(int argc,char *argv[]) {
    freopen("trans.in","r",stdin);
    freopen("trans.out","w",stdout);
    int n,k,flag,pos;
    while(scanf("%d %d",&n,&k) != EOF) {
        scanf("%s",s + 1);
        flag = 0,pos = 0;
        for(int i=1; i<=n && k; ++i) {
            if(s[i] == '2' && s[i + 1] == '2' && s[i + 2] == '3' && (i & 1)) {
                flag = 223,pos = i; break;
            }
            if(s[i] == '2' && s[i + 1] == '3' && s[i + 2] == '3' && (i & 1)) {
                pos = i,flag =  233; break;
            }
            if(s[i] == '2' && s[i + 1] == '3') {
                if(i & 1) s[i + 1] = '2';
                else s[i] = '3' ,--i;
                --k;
            }
        }
        if(flag == 223) {
            if(k & 1) s[pos + 1] = '3';
            else ;
        }
        if(flag == 233) {
            if(k & 1) s[pos + 1] = '2';
            else ;
        }
        printf("%s\n",s + 1);
    }
    fclose(stdin); fclose(stdout);
    return 0;
}

T2

这里写图片描述
这里写图片描述

分析:
把豆豆和砖块丢在一起dp
f[i][j][k]:到第i行,蛇的长度还剩下j,从k位置出第i行的最大得分
g[j][l][r]:蛇的长度剩下j,从当前行l到r区间内仍为死亡的最大得分
初始化:对于每个i,g[j][k][k]=f[i-1][j-a[i][k]][k]+max(-a[i][k],0) 为初始状态
方程g[j][l][r] = max(g[j-a[i][l]][l+1][r] + max(-a[i][j],0),g[j-a[i][r]][l][r-1]+max(-a[i][k],0));
最后f[i][j][k] = max{g[j][l][r](1≤l≤k ≤r ≤5)}

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>

using namespace std;
int a[205][5],f[205][10005][5];
int g[10005][5][5];
bool flag[205][4];

int main(int argc,char *argv[]) {
    freopen("snakevsblock.in","r",stdin);
    freopen("snakevsblock.out","w",stdout);
    int n,m,x,y,Maxi,Ans = 0;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
        for(int j=0; j<5; ++j)
            scanf("%d",&a[i][j]);
    scanf("%d",&m);
    for(int i=1; i<=m; ++i)
        scanf("%d%d",&x,&y),flag[x][y-1] = true;
    memset(f,-0x7f7f7f,sizeof f );
    f[0][4][2] = 0;
    Maxi = n * 50;
    for(int i=1; i<=n; i++) {
        memset(g,-0x7f7f7f,sizeof g);
        for(int j=0; j<=Maxi; j++)
            for (int k = 0; k < 5; k++)
                if (j - a[i][k] >= 0 && j - a[i][k] <= Maxi)
                    f[i][j][k] = g[j][k][k] = f[i - 1][j - a[i][k]][k] + max(-a[i][k], 0);
        for (int l = 1; l <= 4; l++)
            for (int j = 0, k = j + l; k < 5; j++, k++)
                for (int v = 0, val; v <= Maxi; v++) {
                    if (!flag[i][j] && (val = v - a[i][j]) >= 0 && val <= Maxi) g[v][j][k] = g[val][j + 1][k] + max(-a[i][j], 0);
                    if (!flag[i][k - 1] && (val = v - a[i][k]) >= 0 && val <= Maxi) g[v][j][k] = max(g[v][j][k], g[val][j][k - 1] + max(-a[i][k], 0));
                    for (int to = j; to <= k; to++) f[i][v][to] = max(f[i][v][to], g[v][j][k]);
                }
    }
    for (int l = 0; l <= n; l++)
        for (int i = 0; i <= Maxi; i++)
            for (int j = 0; j < 5; j++)
                Ans = max(Ans, f[l][i][j]);
    printf("%d\n", Ans);
    fclose(stdin); fclose(stdout);
    return 0;
}

T3

这里写图片描述
这里写图片描述
这里写图片描述

//五十分暴力
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define MAXN 100005
#define pb push_back
using namespace std;

struct Edge{ int to,next; }e[MAXN << 1];
int head[MAXN],tot,n,m,dep[MAXN],fa[MAXN][20],Ans;
vector<int> vec[300000];

inline void read(int &x){ 
    x=0; register char c = getchar();
    while(c > '9' || c < '0') c = getchar();
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
}

inline void Add_Edge(int u,int v) {
    e[++tot].to = v,e[tot].next = head[u],head[u] = tot;
    e[++tot].to = u,e[tot].next = head[v],head[v] = tot;
}

void DFS(int u,int fat,int deepth) {
    fa[u][0] = fat,dep[u] = deepth;
    for(int i=head[u]; i; i=e[i].next) {
        int v = e[i].to;
        if(v == fat) continue;
        DFS(v,u,deepth + 1);
    }
}

void DP() {
    for(int j=1; j<=18; ++j)
        for(int i=1; i<=n; ++i)
            fa[i][j] = fa[fa[i][j-1]][j-1];
}

void LCA(int u,int v) {
    if(dep[u] < dep[v]) swap(u,v);
    while(dep[u] > dep[v]){ u = fa[u][0],vec[Ans].pb(u); }
    while(u != v){
        u = fa[u][0], v = fa[v][0];
        vec[Ans].pb(u);
        if(u != v) vec[Ans].pb(v);
    }
}

void Megre() {
    vector<int> temp;
    for(int i=1; i<Ans; ++i) {
        for(int j=0; j<vec[Ans].size(); ++j)
            for(int k=0; k<vec[i].size(); ++k)
                if(vec[i][k] == vec[Ans][j]) temp.pb(vec[Ans][j]);
        if(!temp.empty()){//有交集
            vec[i].clear(),vec[Ans].clear(),Ans--;
            for(int k=0; k<temp.size(); ++k)
                vec[i].pb(temp[k]);
            break;
        }
    }
}

int main(int agrc,char *argv[]) {
    freopen("ping.in","r",stdin);
    freopen("ping.out","w",stdout);
    read(n),read(m); register int i; int u,v,k;
    for(i=1; i<=m; ++i)
        read(u),read(v),Add_Edge(u,v);
    DFS(1,0,-1);DP();
    read(k);
    for(i=1; i<=k; ++i) {
        read(u),read(v);
        ++Ans;
        vec[Ans].pb(u);
        if(u != v) vec[Ans].pb(v);
        LCA(u,v);
        Megre();
    }
    printf("%d\n",Ans);
    for(int i=1; i<=Ans; ++i)
        printf("%d ",vec[i][0]);
    fclose(stdin); fclose(stdout);
    return 0;
}


这里写图片描述

//这是std哟
#include <cstdio>
#include<algorithm>
using namespace std;
int n, m, P, u, v, to[200005], nxt[200005], p[100005], deep[100005], q[500005][2];
int son[100005], fa[100005], size[100005], top[100005], dfsx[100005], cnt, ans;
int lca[500005], Q[500005], lr[100005][2];
int sta[100005];
bool flag[400005];
void dfs(int x) {
    size[x] = 1;
    for (int i = p[x]; i != -1; i = nxt[i])
        if (to[i] != fa[x]) {
            fa[to[i]] = x;
            deep[to[i]] = deep[x] + 1;
            dfs(to[i]);
            if (son[x] == -1 || size[to[i]] > size[son[x]]) son[x] = to[i];
            size[x] += size[to[i]];
        }
}
void dfs1(int x) {
    dfsx[x] = ++cnt;
    if (son[x] != -1) top[son[x]] = top[x], dfs1(son[x]);
    for (int i = p[x]; i != -1; i = nxt[i])
        if (to[i] != fa[x] && to[i] != son[x])
            top[to[i]] = to[i], dfs1(to[i]);
}
int findlca(int x, int y) {
    while (1) {
        if (top[x] == top[y]) return deep[x] > deep[y]? y : x;
        if (deep[top[x]] > deep[top[y]]) x = fa[top[x]];
        else y = fa[top[y]];
    }
}
bool query(int x, int l, int r, int ll, int rr) {
    if (l == ll && r == rr) return flag[x];
    int mid = (l + r) >> 1, L = x << 1, R = L | 1;
    if (rr <= mid) return query(L, l, mid, ll, rr);
    else if (ll > mid) return query(R, mid + 1, r, ll, rr);
    else return query(L, l, mid, ll, mid) | query(R, mid + 1, r, mid + 1, rr);
}
void modify(int x, int l, int r, int to) {
    flag[x] = true;
    if (l == r) return;
    int mid = (l + r) >> 1, L = x << 1, R = L | 1;
    if (to <= mid) modify(L, l, mid, to);
    else modify(R, mid + 1, r, to);
}
bool Query(int x, int y) {
    while (1) {
        if (top[x] == top[y]) {
            if (deep[x] < deep[y]) return query(1, 1, cnt, dfsx[x], dfsx[y]);
            else return query(1, 1, cnt, dfsx[y], dfsx[x]);
        }
        if (deep[top[x]] > deep[top[y]])
            if (query(1, 1, cnt, dfsx[top[x]], dfsx[x])) return true;
            else x = fa[top[x]];
        else {
            if (query(1, 1, cnt, dfsx[top[y]], dfsx[y])) return true;
            else y = fa[top[y]];
        }
    }
}
void work(int x) {
    for (int i = p[x]; i != -1; i = nxt[i])
        if (to[i] != fa[x])
            work(to[i]);
    for (int i = lr[x][0]; i <= lr[x][1]; i++)
        if (!Query(q[Q[i]][0], q[Q[i]][1])) {
            modify(1, 1, cnt, dfsx[x]);
            sta[++ans] = x;
            return;
        }
}
bool cmp(int x, int y) {
    return lca[x] < lca[y];
}
int main() {
    freopen("ping.in","r",stdin);
    freopen("ping.out","w",stdout);
    scanf("%d%d", &n, &m);
    for (int i = 0; i <= n; i++) p[i] = son[i] = -1, top[i] = size[i] = fa[i] = deep[i] = 0;
    for (int i = 1; i <= n * 4; i++) flag[i] = false;
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &u, &v);
        u--, v--;
        to[i * 2 - 1] = v;
        nxt[i * 2 - 1] = p[u];
        p[u] = i * 2 - 1;
        to[i * 2] = u;
        nxt[i * 2] = p[v];
        p[v] = i * 2;
    }
    deep[0] = 1;
    dfs(0);
    cnt = 0;
    dfs1(0);
    scanf("%d", &P);
    for (int i = 1; i <= P; i++) {
        scanf("%d%d", &u, &v);
        u--, v--;
        q[i][0] = u, q[i][1] = v;
        lca[i] = findlca(u, v);
        Q[i] = i;
    }
    sort(Q + 1, Q + P + 1, cmp);
    for (int i = 0; i <= n; i++) lr[i][0] = P + 1, lr[i][1] = 0;
    for (int i = 1; i <= P; i++) {
        lr[lca[Q[i]]][0] = min(lr[lca[Q[i]]][0], i);
        lr[lca[Q[i]]][1] = max(lr[lca[Q[i]]][1], i);
    }
    ans = 0;
    work(0);
    printf("%d\n", ans);
    for (int i = 1; i <= ans; i++) printf("%d ", sta[i] + 1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值