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. JQ效果 透明图片覆盖动画

    效果图呈上 先说思路 1,一个固定的框架,有两张图片,一张是狗狗的,一张是练习方式,想把做好的练习方式隐藏 2,效果上想要从下面滑动出来,所以透明框定位在下面 3,整理需要的东西,缓慢升起需要动画效果 ...

  2. vue 使用 element ui动态添加表单

    html部分 <div class="hello"> <el-form :model="dynamicValidateForm" ref=&q ...

  3. Leetcode598.Range Addition II范围求和2

    给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作. 操作用二维数组表示,其中的每个操作用一个含有两个正整数 a 和 b 的数组表示,含义是将所有符合 0 < ...

  4. 初探element+vue+vue-router

    基本步骤先准备好 npm install -g vue-cli npm init webpack cnpm i element-ui -S 修改/src/main.js // The Vue buil ...

  5. webpack学习之—— Plugins

    Plugins are the backbone of webpack! webpack 自身也是构建于你在 webpack 配置中用到的相同的插件系统之上! 插件目的在于解决 loader 无法实现 ...

  6. Android原生调用mui里面的js如何实现

    遍历所有运行中的webview页面,采用自带的SDK方法进行获取所有的IWebview.MUI中自带的webview是一个IWebviewArrayList<IWebview> webli ...

  7. js数组求交集

    求两个数组的交集 var arr1 = [1,2,3]; var arr2 = [2,3,4]; var arr3; arr3 = arr1.filter(function(num) { return ...

  8. 洛谷P1316 P1824

    P1316 丢瓶盖 题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以 ...

  9. CentOS下重启uwsgi

    使用Django开发项目,每次修改内容无法刷新,重启nginx也无效,每次都重启主机, 网上搜索很多资料,发现可以重启uwsgi来解决问题,但是发现uwsgi重启每个人都不一样,很多人写了脚本重启 我 ...

  10. 软件测试 → 第二章 基础-> 软件缺陷与缺陷管理

    一.缺陷定义与分类 1.1.软件缺陷 定义:在软件工程整个生命周期中任何背离需求.无法正确完成用户所要求的功能的问题,包括存在于组件.设备.或系统软件中因异常条件不支持而导致系统失败等都属于缺陷. 从 ...