ZOJ 3941 Kpop Music Party 贪心
题目链接:
http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3941
题解:
先吧所给的区间合并,得到若干个独立的区间。
然后从左到右把所有的区间都铺满个,并且对每个独立的区间的最后一个点考虑放和不放两种情况(dfs做,总复杂度也就2^10),然后去所有答案里面最大的那个。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL; int n, k, m; struct Node {
LL l, r;
bool operator < (const Node& tmp) const {
return l < tmp.l || l == tmp.l&&r < tmp.r;
}
}arr[], nds[]; int tot; //pos表示当前已经覆盖到了pos-1的位置,从pos开始是还未覆盖的部分
void solve(LL pos, int cur, LL cnt, LL ret, LL &ans) {
if (cnt > m) return;
if (cur == tot || cnt == m) {
ans = max(ans, ret); return;
}
LL lef, len, sum;
if (pos <= nds[cur].r) {
lef = max(pos, nds[cur].l);
len = nds[cur].r - lef + ;
sum = len / k;
if (len%k) sum++;
if (cnt + sum>m) { ans = max(ans, ret + (m - cnt)*k); }
//最后一个点不放
solve(lef + k*sum, cur + , cnt + sum, ret + sum*k, ans);
//最后一个点放
solve(nds[cur].r + k, cur + , cnt + sum + , ret + nds[cur].r + k - lef, ans);
}
else {
//最后一个点不放
solve(pos, cur + , cnt, ret, ans);
//最后一个点放
solve(max(pos, nds[cur].r + k), cur + , cnt + , ret + max((LL), nds[cur].r + k - pos), ans);
}
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
tot = ;
scanf("%d%d%d", &n, &k, &m);
for (int i = ; i < n; i++) {
scanf("%lld%lld", &arr[i].l, &arr[i].r);
}
sort(arr, arr + n);
//合并区间
for (int i = ; i < n; i++) {
if (tot == ) {
nds[tot++] = arr[i];
}
else {
if (arr[i].l <= nds[tot - ].r) {
nds[tot - ].r = max(nds[tot - ].r, arr[i].r);
}
else {
nds[tot++] = arr[i];
}
}
}
LL ans = ;
solve(, , , , ans);
printf("%lld\n", ans);
}
return ;
}
ZOJ 3941 Kpop Music Party 贪心的更多相关文章
- ZOJ 3941 Kpop Music Party(省赛, 贪心)
Kpop Music Party Time Limit: 2 Seconds Memory Limit: 65536 KB Marjar University often hosts Kpo ...
- ZOJ 3941 Kpop Music Party
先把能合并的区间都合并起来. 考虑最裸的贪心策略,从左到右一段一段的取. 但是,这样会有错,错在没有考虑每段区间选取最后一个点. 因为N只有10,所以枚举一下哪些区间最后一个点会被选择,然后按照最裸的 ...
- ZOJ 3699 Dakar Rally(贪心)
这是一道贪心题,他的贪心思想很容易想明白,我们保证油箱里的油始终是最便宜的我们最后的花费就能是最少的.实现方法就是:比如现在在i点,我们看邮箱满载能最远到达哪里,不妨设最远到达j,(j >= i ...
- ZOJ 2002 Copying Books 二分 贪心
传送门:Zoj2002 题目大意:从左到右把一排数字k分,得到最小化最大份,如果有多组解,左边的尽量小. 思路:贪心+二分(参考青蛙过河). 方向:从右向左. 注意:有可能最小化时不够k分.如 ...
- ZOJ 3607 Lazier Salesgirl(贪心)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3607 题意:一个卖面包的小姑娘,给第i个来买面包的人的价格是pi, ...
- ZOJ 2968 Difference Game 【贪心 + 二分】
题意: 有Ga.Gb两堆数字,初始时两堆数量相同.从一一堆中移一一个数字到另一一堆的花费定义为两堆之间数 量差的绝对值,初始时共有钱C.求移动后Ga的最小小值减Gb的最大大值可能的最大大值. 思路: ...
- ZOJ 3829 Known Notation(贪心)题解
题意:给一串字符,问你最少几步能变成后缀表达式.后缀表达式定义为,1 * 1 = 1 1 *,题目所给出的字串不带空格.你可以进行两种操作:加数字,交换任意两个字符. 思路:(不)显然,最终结果数字比 ...
- ZOJ - 3829 Known Notation(模拟+贪心)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...
- ZOJ - 3987 - Numbers (大数 + 贪心)
参考自:https://blog.csdn.net/u013534123/article/details/78484494 题意: 给出两个数字n,m,把n分成m份,使得以下最小 思路: 或运算只有0 ...
随机推荐
- 发送Ajax请求获取JSON格式数据
Aspx前端页面: <script type="text/javascript"> $(function () { $.getJSON("Ajax/TestA ...
- Thinking about think-time functions
You will find yourself very familier to this topic. Ok, let me ask you one question: Let me know th ...
- BGP学习笔记
源自红茶三杯: BGP应用于大规模网络或运营商,用作在AS间传递路由信息 使用BGP的三大理由 1. 大量路由需要承载, IGP只能容纳千条,而BGP可以容纳上万(应该是IGP结合BGP使用?) 2. ...
- 使用memcached实现tomcat集群session共享
环境centos6.7,下载安装必要的软件:yum -y install epel-release(tomcat7在此源上,tomcat7是现在主流版本) yum -y install tomcat ...
- WordPress 主题开发 - (七) 让主题更安全 待翻译
We're just about ready to start building our theme's template files. Before we do this, however, it' ...
- phpMyAdmin安装
phpMyAdmin是MySql的一个Web操作界面. phpMyAdmin官网貌似被和谐了,经常无法访问.不过我们可以从GitHub下载phpMyAdmin. 然后解压,搭建与普通的PHP网站一样. ...
- ROS多个master消息互通
需求 有时候我们需要有几个不同的master, 他们之间要交换topic的内容,这时候就不能使用ros自带的设置同一个master的方法. 我们的处理方法是,构造一个client和一个server,他 ...
- python学习第二天第一部分
备注:写程序不能写重复性的代码 学习内容:数据类型.for循环.while循环.字符编码.文件处理 一.for循环 1.简单的for循环 for i in range(10): # 此处意思为:循环r ...
- WPF控件数据单项绑定
建立一个姓名,年龄输入框,如图: XAML代码: <TextBox Name="txtName" Text="{Binding Name}" ToolTi ...
- Mybatis 实现手机管理系统的持久化数据访问层
最近公司需要对客户手机进行管理并提供二维码存储手机串号的加密字符.供其他接入系统通过扫面二维码解析使用.系统提供手机信息管理,客户管理,用户管理功能. 1.使用到的POJO类 1.1 User pac ...