Codeforces Round #635 (Div. 1)
A. Linova and Kingdom
题意:
给定一颗以\(1\)为根的树,现在要选定\(k\)个结点为黑点,一个黑点的贡献为从他出发到根节点经过的白点数量。
问黑点贡献总和最大为多少。
思路:
- 最直接的想法黑点肯定是位于深度越深的点越好,并且有这样一个性质:假设我们选择了一个点,那么该点的所有后代也将会被选择。
- 一个黑点产生的贡献为其深度减去到根路径中黑点的数量。
- 直接按照上述思路贪心不好思考,考虑转化一下贡献的计算方法:我们减去黑点的数量时在其祖先结点再减。也就是说每个黑点会减去其子树\(size\)。
- 那么对于所有点按照\(deep-size\)进行排序,从大到小贪心选即可。
因为一个点选择了所有子树也会被选,并且显然子树的\(deep-size\)更大,所以容易证明这种贪心是正确的。
Code
/*
* Author: heyuhhh
* Created Time: 2020/4/15 22:36:55
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5;
int n, k;
vector <int> G[N];
int d[N], sz[N];
void dfs(int u, int fa) {
d[u] = d[fa] + 1;
sz[u] = 1;
for(auto v : G[u]) if(v != fa) {
dfs(v, u);
sz[u] += sz[v];
}
}
void run() {
cin >> n >> k;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
vector <int> v;
for(int i = 1; i <= n; i++) {
v.push_back(d[i] - sz[i]);
}
sort(all(v));
reverse(all(v));
ll ans = 0;
for(int i = 0; i < k; i++) ans += v[i];
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}
B. Xenia and Colorful Gems
枚举一维,然后另外两维直接贪心二分即可。
代码写得有点乱。。
Code
/*
* Author: heyuhhh
* Created Time: 2020/4/15 22:47:16
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
//#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
int na, nb, nc;
ll a[N], b[N], c[N];
ll calc(int x, int y, int z) {
if(x && y && z && x <= na && y <= nb && z <= nc)
return (a[x] - b[y]) * (a[x] - b[y]) + (a[x] - c[z]) * (a[x] - c[z]) + (b[y] - c[z]) * (b[y] - c[z]);
return 8e18;
}
ll calc_a(int y, int z) {
int t = lower_bound(a + 1, a + na + 1, b[y]) - a;
ll res = 9e18;
res = min(res, calc(t, y, z));
res = min(res, calc(t - 1, y, z));
t = lower_bound(a + 1, a + na + 1, c[z]) - a;
res = min(res, calc(t, y, z));
res = min(res, calc(t - 1, y, z));
return res;
}
ll calc_c(int x, int y) {
int t = lower_bound(c + 1, c + nc + 1, b[y]) - c;
ll res = 9e18;
res = min(res, calc(x, y, t));
dbg(x, y, t, res);
res = min(res, calc(x, y, t - 1));
t = lower_bound(c + 1, c + nc + 1, a[x]) - c;
res = min(res, calc(x, y, t));
res = min(res, calc(x, y, t - 1));
return res;
}
ll calc_b(int x, int z) {
int t = lower_bound(b + 1, b + nb + 1, c[z]) - b;
ll res = 9e18;
res = min(res, calc(x, t, z));
res = min(res, calc(x, t - 1, z));
t = lower_bound(b + 1, b + nb + 1, a[x]) - b;
res = min(res, calc(x, t, z));
res = min(res, calc(x, t - 1, z));
return res;
}
void run() {
cin >> na >> nb >> nc;
for(int i = 1; i <= na; i++) cin >> a[i];
for(int i = 1; i <= nb; i++) cin >> b[i];
for(int i = 1; i <= nc; i++) cin >> c[i];
sort(a + 1, a + na + 1);
sort(b + 1, b + nb + 1);
sort(c + 1, c + nc + 1);
ll ans = 9e18;
for(int i = 1, t; i <= na; i++) {
t = lower_bound(b + 1, b + nb + 1, a[i]) - b;
if(t <= nb) ans = min(ans, calc_c(i, t));
if(t > 1) ans = min(ans, calc_c(i, t - 1));
t = lower_bound(c + 1, c + nc + 1, a[i]) - c;
if(t <= nc) ans = min(ans, calc_b(i, t));
if(t > 1) ans = min(ans, calc_b(i, t - 1));
}
for(int i = 1, t; i <= nb; i++) {
t = lower_bound(a + 1, a + na + 1, b[i]) - a;
ans = min(ans, calc_c(t, i));
ans = min(ans, calc_c(t - 1, i));
t = lower_bound(c + 1, c + nc + 1, b[i]) - c;
ans = min(ans, calc_a(i, t));
ans = min(ans, calc_a(i, t - 1));
}
for(int i = 1, t; i <= nc; i++) {
t = lower_bound(b + 1, b + nb + 1, c[i]) - b;
ans = min(ans, calc_a(t, i));
ans = min(ans, calc_c(t - 1, i));
t = lower_bound(a + 1, a + na + 1, c[i]) - a;
ans = min(ans, calc_b(t, i));
ans = min(ans, calc_b(t - 1, i));
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T;
while(T--) run();
return 0;
}
C. Kaavi and Magic Spell
题意:
给出串\(S,T\),现在有一个空串\(A\),然后每次执行以下操作:
- 选择\(S\)的第一个字符并删除,然后将其插入\(A\)的前面或者后面。
现在问有多少种方式使得串\(T\)为串\(A\)的前缀。
思路:
将\(T\)串用\(*\)补齐到与\(S\)长度相同,问题转化为了对于\(T\)的一个前缀,\(S\)有多少种匹配方法。
注意到有个性质:匹配任意一段\(T\)时,我们只会用到\(S\)的一个前缀。
那么我们直接跑个区间\(dp\),表示匹配\(T[l,r]\)的方案数,转移时直接根据\(s[r-l+1]\)\(O(1)\)转移即可。
最后答案即为\(\sum_{i\geq m}{dp_{1,i}}\)。
细节见代码:
Code
/*
* Author: heyuhhh
* Created Time: 2020/4/16 9:06:01
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 3000 + 5, MOD = 998244353;
int n, m;
char s[N], t[N];
int dp[N][N];
int solve(int l, int r) {
if(l > r) return 1;
if(dp[l][r] != -1) return dp[l][r];
int k = r - l + 1;
int res = 0;
if(l > m || s[k] == t[l]) res += solve(l + 1, r);
if(r > m || s[k] == t[r]) res = (res + solve(l, r - 1)) % MOD;
return dp[l][r] = res;
}
void run() {
cin >> (s + 1) >> (t + 1);
n = strlen(s + 1), m = strlen(t + 1);
memset(dp, -1, sizeof(dp));
int ans = 0;
for(int i = m; i <= n; i++) {
ans = (ans + solve(1, i)) % MOD;
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}
Codeforces Round #635 (Div. 1)的更多相关文章
- Codeforces Round #635 (Div. 2) 题解
渭城朝雨浥轻尘,客舍青青柳色新. 劝君更尽一杯酒,西出阳关无故人.--王维 A. Ichihime and Triangle 网址:https://codeforces.com/contest/133 ...
- Codeforces Round #635 (Div. 2)
Contest Info Practice Link Solved A B C D E F 4/6 O O Ø Ø O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Sol ...
- Codeforces Round #635 (Div. 2)部分(A~E)题解
虽然打的是div1,但最后半小时完全处于挂机状态,不会做1C,只有个 \(O(n^3)\) 的想法,水了水论坛,甚至看了一下div2的AB,所以干脆顺便写个div2的题解吧,内容看上去还丰富一些(X) ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- C#扫盲篇(四):.NET Core 的异步编程-只讲干货(async,await,Task)
关于async,await,task的用法和解释这里就不要说明了,网上一查一大堆.至于为啥还要写这篇文章,主要是其他文章水分太多,不适合新手学习和理解.以下内容纯属个人理解,如果有误,请高手指正.本文 ...
- ORACLE的还原表空间UNDO写满磁盘空间,解决该问题的具体步骤
产生问题的原因主要以下两点:1. 有较大的事务量让Oracle Undo自动扩展,产生过度占用磁盘空间的情况:2. 有较大事务没有收缩或者没有提交所导制:说明:本问题在ORACLE系统管理中属于比较正 ...
- 【Java基础】枚举类与注解
枚举类与注解 枚举类的使用 当需要定义一组常量时,强烈建议使用枚举类. 枚举类的理解:类的对象只有有限个,确定的. 若枚举只有一个对象, 则可以作为一种单例模式的实现方式. 枚举类的属性: 枚举类对象 ...
- windows下使用mingw和msvc静态编译Qt5.15.xx
windows下使用mingw和msvc静态编译Qt5.15.xx 下载并安装相关依赖软件 Python version 2.7 https://www.python.org/downloads/ ( ...
- SpringBoot Logback无法获取配置中心属性
SpringBoot Logback无法获取配置中心属性 前言 最近在做项目中,需要把项目中的日志信息通过RabbitMQ将规定格式的消息发送到消息队列中,然后ELK系统通过消息队列拿日志并且保存起来 ...
- 攻防世界—pwn—level0
题目分析 下载文件后首先使用checksec检查文件保护机制 文件名太长了,就更改了一下 发现是一个64位程序,使用ida查看伪代码 注意到一个特殊的函数名callsystem 确定思路,直接栈溢出 ...
- fileinput模块用法
fileinput模块功能: 提供拼接一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行,从而进行逐行处理(如进行显示.替换.添加行号等). 其功能类似于linux命令的 ...
- oracle动态采样导致数据库出现大量cursor pin s wait on x等待
生产库中,突然出现了大量的cursor pin s wait on x等待,第一反应是数据库出现了硬解析,查看最近的DDL语句,没有发现DDL.那么有可能这个sql是第一次进入 在OLTP高并发下产生 ...
- 用CSS实现蒙德里安名画|学习麻瓜编程以项目为导向入门前端 HTML+CSS+JS
实现项目:用CSS实现蒙德里安名画 1.首先,献上代码和效果图 1.1代码: <head> <style> .centerframe{ display: flex; heigh ...
- 爬虫+django,打造个性化API接口
简述 今天也是同事在做微信小程序的开发,需要音乐接口的测试,可是用网易云的开放接口比较麻烦,也不能进行测试,这里也是和我说了一下,所以就用爬虫写了个简单网易云歌曲URL的爬虫,把数据存入mysql数据 ...