A

B

C

等比数列的性质,前面的i项的和,不会超过第i+1

D

有若干个区间,要求每一个点被区间覆盖的次数不能超过k个。问移除的最少的区间的数目。

贪心:

若某个点被覆盖了k次以上,那么肯定是移除这些区间里面右端点最右的点的区间。

时间复杂度要求是\(O(nlog(n))\)

模拟就好了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define pb(x) push_back(x)
#define cls(x, val) memset(x, val, sizeof(x))
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define inc(i, l, r) for(int i=l; i<=r; i++)
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+10;
int l[maxn], r[maxn];
int n, k;
vector<int> node[maxn];
set<pii> s; int main(){
ios::sync_with_stdio(false);
cin>>n>>k;
for(int i=1; i<=n; i++){
cin>>l[i]>>r[i];
node[l[i]].push_back(i);
node[r[i]+1].push_back(-i);
}
vector<int> ans;
for(int i=1; i<=2e5; i++){
for(int j=0; j<node[i].size(); j++){
int u = node[i][j];
if(u>0){
s.insert(mp(r[u], u));
}
else s.erase(mp(r[-u], -u));
}
while(s.size()>k){
set<pii>::iterator it = s.end();
--it;
ans.push_back(it->se);
s.erase(*it);
}
}
cout<<ans.size()<<endl;
for(int i=0; i<ans.size(); i++){
cout<<abs(ans[i])<<" ";
}
cout<<endl; return 0;
}

E

简单dp

F

题目:

要求一个点的集合,使得他们之间的任意两点之间的距离大于k,并且这个集合的点的权重和最大

树形dp

学习了一种很trick的写法,不具有普遍性

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200 + 10; vector<int> G[maxn];
int n, k, m;
int a[maxn], vis[maxn];
void dfs(int u, int fa, int d){
if(d > k) return ;
a[u] -= m;
for(int &v : G[u]) if(v != fa) dfs(v, u, d + 1);
}
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) scanf("%d", a + i);
for(int i = 1; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
queue<int> q;
vector<int> b;
q.push(1); vis[1] = 1;
while(!q.empty()){
int u = q.front(); q.pop();
b.push_back(u);
for(int &v : G[u]){
if(!vis[v]){
q.push(v);
vis[v] = 1;
}
}
}
int ans = 0;
for(int i = b.size() - 1; i >= 0; i--){
//这里写的很trick, 从叶子节点开始取。若上面有更好的选择,那么相当于就叶子的贡献转移到上面了。
m = a[b[i]];
if(m < 0) continue;
ans += m;
dfs(b[i], 0, 0);
}
printf("%d\n", ans);
return 0;
}

树形dp的写法有点像另外的一个题目:

给一个树形的结构,然后任意一个节点到关键节点的距离不能超过k。

问最小的关键节点的数目。

#include <bits/stdc++.h>

using namespace std;

const int N = 210;

int n, k;
vector<int> a;
vector<vector<int>> g, dp; void dfs(int v, int p) {
dp[v][0] = a[v];
for (auto to : g[v]) {
if (to != p) dfs(to, v);
}
for (int dep = 0; dep < N; ++dep) {
if (dep == 0) {
for (auto to : g[v]) {
if (to == p) continue;
dp[v][dep] += dp[to][max(0, k - dep - 1)];
}
} else {
for (auto to : g[v]) {
if (to == p) continue;
int cur = dp[to][dep - 1];
for (auto other : g[v]) {
if (other == p || other == to) continue;
cur += dp[other][max(dep - 1, k - dep - 1)];
}
dp[v][dep] = max(dp[v][dep], cur);
}
}
}
for (int dep = N - 1; dep > 0; --dep) {
dp[v][dep - 1] = max(dp[v][dep - 1], dp[v][dep]);
}
} int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif cin >> n >> k;
++k;
a = vector<int>(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
g = vector<vector<int>>(n);
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
} dp = vector<vector<int>>(n, vector<int>(N));
dfs(0, -1);
cout << dp[0][0] << endl; return 0;
}

codeforces1249-div3的更多相关文章

  1. Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)

    Problem  Codeforces #550 (Div3) - G.Two Merged Sequences Time Limit: 2000 mSec Problem Description T ...

  2. codeforces-1144 (div3)

    赛后经验:div3过于简单,以后不做了 A.存在以下情况即为NO 1.存在相同字母 2.最大字母-最小字母 != 字符串长度 #include <map> #include <set ...

  3. 12.27 cf div3 解题报告

    12.27 cf div3 解题报告 wxy.wxy,带上分拉,全场做了个无脑小白 比赛场地 A: T1,跟着模拟就好了 B: sort一遍之后 去除的数一定是a[1]或者a[n] 比较去除谁小就输出 ...

  4. CodeForces 1029E div3

    题目链接 第一道场上自己做出来的E题...虽然是div3,而且是原题... 当时做完ABC,D题没有思路就去怼E了,然后发现貌似原题? 事实上就是原题... 给个原题链接... [HNOI2003]消 ...

  5. CodeForces Round #527 (Div3) B. Teams Forming

    http://codeforces.com/contest/1092/problem/B There are nn students in a university. The number of st ...

  6. 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3

    ◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...

  7. Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]

    hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...

  8. CodeForces Round#480 div3 第2场

    这次div3比上次多一道, 也加了半小时, 说区分不出1600以上的水平.(我也不清楚). A. Remove Duplicates 题意:给你一个数组,删除这个数组中相同的元素, 并且保留右边的元素 ...

  9. codeforces #579(div3)

    codeforces #579(div3) A. Circle of Students 题意: 给定一个n个学生的编号,学生编号1~n,如果他们能够在不改变顺序的情况下按编号(无论是正序还是逆序,但不 ...

  10. CF598: div3解题报告

    CF598:div3解题报告 A: Payment Without Change 思路: 按题意模拟即可. 代码: #include<bits/stdc++.h> using namesp ...

随机推荐

  1. ubuntu和win10设置双显示器

    ubuntu:最右上角那个图标,点开找到系统设置,系统设置中找到“显示”中,在其中可以调节双屏显示或者只显示一个屏,图等会补... win10:现在是ubuntu系统所以操作忘记了写不出来,等下换系统 ...

  2. DOM 创建元素 删除元素(结点)

    创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素追加该元素. <script> var para=document. ...

  3. 机器学习之决策树(ID3)算法与Python实现

    机器学习之决策树(ID3)算法与Python实现 机器学习中,决策树是一个预测模型:他代表的是对象属性与对象值之间的一种映射关系.树中每个节点表示某个对象,而每个分叉路径则代表的某个可能的属性值,而每 ...

  4. xml-apis.jar getTextContent() jar包冲突解决(getTextContent()方法无法找到)

    1.引用包: import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList; 2.方法中应用: ...

  5. mybatis-plus思维导图,让mybatis-plus不再难懂

    mybatis-plus与mybatis mybatis Mybatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置 ...

  6. 洛谷 P4205 [NOI2005]智慧珠游戏 DFS

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 总结 题面 题目链接 P4205 [NOI2005]智慧珠游戏 题目描述 智慧 ...

  7. 阿里云ecs环境配置

    在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境 https://ninghao.net/blog/1368 阿里云ecs从购买到环境搭建和建站!! ...

  8. typora 使用

    菜单 输入+换行键,产生标题,自动更新 [toc] [TOC] 段落 按换行键建立新的一行可在行尾插入打断线,禁止向后插入 按换行键建立新的一行<br/> 标题 开头#的个数表示,空格+文 ...

  9. laravel 图片上传 intervention/image

    1. composer require intervention/image 2). 修改 app/config/app.php 添加 ServiceProvider: // 将下面代码添加到 pro ...

  10. laravel 极验(Geetest) 让验证更安全。

    整理的有些仓促,在9月15号之后会更新更加详细更加全面的文档,供给大家参考,学习! 1.简述 在网站开发中使用频率最高的工具之一便是验证码,验证码在此也是多种多样,不过简单的图片验证码已经可以被机器识 ...