AtCoder ABC175

2023-12-13 10:25:56

C - Walking Takahashi
正数和负数是一样的,因此取绝对值
如果不能移到负半轴,那么终点就是答案
如果能移到负半轴,那么取余数r求r与d-r里的最小值

E - Picking Goods
在单纯的dp路径问题上增加一个维度,表示当前行拿到k个数的情况下最多能得到的分数

// atcoder.cpp : 
//
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <vector>
#include <regex>
#include <queue>
#include <climits>
using namespace std;

typedef pair<int, int> pii;
typedef long long LL;
typedef vector<int> vi;

int R, C, K;
LL dp[3001][3001][4];
LL b[3001][3001];
LL a[3001][3001];


int main()
{
	//freopen("in.txt", "r", stdin);
	scanf("%d%d%d", &R, &C, &K);
	for (int i = 0; i < K; ++i) {
		int r, c, v;
		scanf("%d%d%d", &r, &c, &v);
		a[r][c] = v;
	}
	for (int i = 0; i <= R; ++i) {
		for (int j = 0; j <= C; ++j) {
			for (int k = 0; k < 4; ++k)
				dp[i][j][k] = -(1LL << 60);
			b[i][j] = -(1LL << 60);
		}
	}

	for (int i = 1; i <= R; ++i) {
		for (int j = 1; j <= C; ++j) {
			if (i == 1 && j == 1) {
				dp[i][j][0] = 0;
				if (a[i][j] > 0) dp[i][j][1] = a[i][j];
				for (int k = 0; k < 4; ++k)
					b[i][j] = max(b[i][j], dp[i][j][k]);
				continue;
			}
			for (int k = 0; k < 4; ++k) {
				dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
			}
			dp[i][j][0] = max(dp[i][j][0], b[i - 1][j]);
			if (a[i][j] > 0) {
				for (int k = 1; k < 4; ++k) {
					dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
				}
				dp[i][j][1] = max(dp[i][j][1], b[i - 1][j] + a[i][j]);
			}
			for (int k = 0; k < 4; ++k) {
				b[i][j] = max(b[i][j], dp[i][j][k]);
			}
		}
	}
	LL ans = b[R][C];
	printf("%lld\n", ans);
	return 0;
}

F - Making Palindrome
写了一天
主要问题还是建图想的不够清楚
回文串可以这么处理
abcab cba cba
每次维护字符串本身,以及字符串是在左边拼接还是在右边拼接作为一个状态节点
比如abcab可以看成(abcab,0)
匹配cba后变成(ab, 0)
再匹配cba后变成(c,1)
每次拿这些节点去匹配字符串,相当于一条边
起点向每个字符串连一条权值为w[i]的边,从回文前缀和回文后缀向终点连一条权值为0的边
跑spfa

/*
 * @Author: C.D.
 * @Date: 2023-12-12 08:14:36
 * @LastEditors: C.D.
 * @LastEditTime: 2023-12-12 11:00:11
 * @FilePath: \vs_coder\atcoder.cpp
 *
 * Copyright (c) 2023 by C.D./tongwoo.cn, All Rights Reserved.
 */

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef long long LL;

int n;
const int maxn = 2000020;
string vs[55];
bool vis[maxn];
LL dist[maxn];
int w[55];
int idx = 0;
int src, dst;
map<pair<string, int>, int> vi;         // vertex index
map<int, pair<string, int>> si;         // for debug
vector<pair<int, int>> edges[maxn];


void check(string sa, string sb, int cost, int i_node) {
    int i = 0, j = sb.size() - 1;
    while (j >= 0 && i < sa.size()) {
        if (sa[i] != sb[j]) return;
        i++, j--;
    }
    if (i < sa.size()) {
        string left = sa.substr(i);
        int i_left = vi[{left, 0}];
        edges[i_node].push_back({ i_left, cost });
    }
    else {
        string left = sb.substr(0, j + 1);
        int i_left = vi[{left, 1}];
        edges[i_node].push_back({ i_left, cost });
    }
}

bool check_pal(string sa) {
    for (int i = 0, j = sa.size() - 1; i <= j; ++i, --j) {
        if (sa[i] != sa[j]) return false;
    }
    return true;
}


void spfa() {
    for (int i = 0; i < idx; ++i)
        dist[i] = 1LL << 62;
    queue<int> qu;
    vis[src] = 1;
    qu.push(src);
    dist[src] = 0;
    while (!qu.empty()) {
        int q = qu.front();
        qu.pop();
        vis[q] = 0;
        for (auto it : edges[q]) {
            int v = it.first;
            int c = it.second;
            if (dist[v] > dist[q] + c) {
                //cout << si[q].first << " " << q << " " << si[v].first << " " << v << " " << c << " " << dist[q] + c << endl;
                dist[v] = dist[q] + c;
                if (!vis[v]) {
                    qu.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
}


int main() {
    //freopen("in.txt", "r", stdin);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        char s[30];
        scanf("%s%d", s, &w[i]);
        vs[i] = s;
    }
    for (int i = 0; i < n; ++i) {
        string temp = "";
        string s = vs[i];
        for (int j = s.size() - 1; j >= 0; --j) {
            temp = s[j] + temp;
            si[idx] = { temp, 0 };
            vi[{temp, 0}] = idx++;
        }
        temp = "";
        for (int j = 0; j < s.size(); ++j) {
            temp += s[j];
            si[idx] = { temp , 1 };
            vi[{temp, 1}] = idx++;
        }
    }
    si[idx] = { "", 0 };
    vi[{"", 0}] = idx++;
    si[idx] = { "", 1 };
    vi[{"", 1}] = idx++;
    si[idx] = { "#", 0 };
    src = idx++;
    si[idx] = { "@", 1 };
    dst = idx++;
    for (auto it : vi) {
        int type = it.first.second;
        string s = it.first.first;
        if (s == "") continue;
        if (type == 0) {
            for (int j = 0; j < n; ++j)
                check(s, vs[j], w[j], it.second);
        }
        if (type == 1) {
            for (int j = 0; j < n; ++j)
                check(vs[j], s, w[j], it.second);
        }
    }
    for (int i = 0; i < n; ++i) {
        edges[src].push_back({ vi[{vs[i], 0}], w[i] });
        edges[src].push_back({ vi[{vs[i], 1}], w[i] });
    }
    for (int i = 0; i < n; ++i) {
        string s = vs[i];
        string temp = "";
        for (int j = s.size() - 1; j >= 0; --j) {
            temp = s[j] + temp;
            if (check_pal(temp)) {
                edges[vi[{temp, 0}]].push_back({ vi[{"", 0}], 0 });
            }
        }
        temp = "";
        for (int j = 0; j < s.size(); ++j) {
            temp += s[j];
            if (check_pal(temp)) {
                edges[vi[{temp, 1}]].push_back({ vi[{"", 0}], 0 });
            }
        }
    }

    edges[vi[{"", 0}]].push_back({ dst, 0 });
    edges[vi[{"", 1}]].push_back({ dst, 0 });

    spfa();
    LL ans = dist[dst];
    if (ans >= 1LL << 62) ans = -1;
    printf("%lld\n", ans);
    return 0;
}

文章来源:https://blog.csdn.net/acrux1985/article/details/134918166
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。