2017 华东师范大学网赛 F.丽娃河的狼人传说 Problem #3263( 贪心)
丽娃河的狼人传说
Time limit per test: 1.0 seconds
Time limit all tests: 1.0 seconds
Memory limit: 256 megabytes
丽娃河是华师大著名的风景线。但由于学校财政紧缺,丽娃河边的路灯年久失修,一到晚上就会出现走在河边要打着手电的情况,不仅非常不方便,而且影响安全:已经发生了大大小小的事故多起。
方便起见,丽娃河可以看成是从 1 到 n 的一条数轴。为了美观,路灯只能安装在整数点上,每个整数点只能安装一盏路灯。经专业勘测,有 m 个区间特别容易发生事故,所以至少要安装一定数量的路灯,
请问至少还要安装多少路灯。
Input
第一行一个整数 T (1≤T≤300) ,表示测试数据组数。
对于每组数据:
第一行三个整数 n,m,k (1≤n≤103,1≤m≤103,1≤k≤n) 。
第二行 k 个不同的整数用空格隔开,表示这些位置一开始就有路灯。
接下来 m 行表示约束条件。第 i 行三个整数 li,ri,ti 表示:第 i 个区间 [li,ri] 至少要安装 ti 盏路灯 (1≤li≤ri≤n,1≤ti≤n) 。
Output
对于每组数据,输出 Case x: y
。其中 x 表示测试数据编号(从 1 开始),y 表示至少要安装的路灯数目。如果无解,y 为 −1 。
Examples
3 5 1 3 1 3 5 2 3 2 5 2 3 1 3 5 2 3 2 3 5 3 5 2 3 1 3 5 2 3 2 4 5 1
Case 1: 1 Case 2: 2 Case 3: 1
Note
因为今天不是满月,所以狼人没有出现。
Source
2017 华东师范大学网赛
这题数据量大点,可以差分约束做~:http://blog.csdn.net/qq_34374664/article/details/75287020
按右端点排序,然后对于不满足条件的尽量往右垒就好了。贪心的证明:由于左边的都已经垒满了,所以垒左边的肯定是没意义的。垒中间肯定没有垒右边的号,因为右边的区间不可能长得断开,使得垒在左边收益更大。这样就可以实现 O(n2) 。
本题虽然不作要求,但是可以做到 O(nlog2n) ,用线段树维护,不符合条件可能需要二分。具体细节留给读者思考。
暴力代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1e3 + 5;
int book[maxn], n, m, k;
struct node
{int l, r, val;
}a[maxn];
int cmp(node a, node b)
{return a.r < b.r;
}
int main()
{int t, ca = 1;cin >> t;while(t--){memset(book, 0, sizeof(book)); scanf("%d%d%d", &n, &m, &k);int x;while(k--){scanf("%d", &x);book[x] = 1;}for(int i = 1; i <= m; i++)scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].val);sort(a+1, a+1+m, cmp);int flag = 0, ans = 0;for(int i = 1; i <= m; i++){int l = a[i].l, r = a[i].r;if(r-l+1 < a[i].val) flag = 1;if(flag) break;int cnt = 0;for(int j = l; j <= r; j++)if(book[j]) cnt++;cnt = max(0, a[i].val-cnt);ans += cnt;for(int j = r; j >= l; j--){if(!cnt) break;if(!book[j])book[j] = 1, cnt--;} }if(flag) printf("Case %d: -1\n", ca++);elseprintf("Case %d: %d\n", ca++, ans);}return 0;
}
线段树:用到了set标记。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e3 + 5;
int sum[maxn*4], set[maxn*4], lazy[maxn*4], n, q;
struct node
{int l, r, val;
}a[maxn];
int cmp(node a, node b)
{return a.r < b.r;
}
void pushup(int rt)
{sum[rt] = sum[rt*2] + sum[rt*2+1];
}
void pushdown(int rt, int l, int r)
{int mid = (l+r)/2;if(set[rt] != -1){set[rt*2+1] = set[rt*2] = set[rt];lazy[rt*2] = lazy[rt*2+1] = 0;sum[rt*2] = set[rt]*(mid-l+1);sum[rt*2+1] = set[rt]*(r-mid);set[rt] = -1;}if(lazy[rt]){lazy[rt*2] += lazy[rt];lazy[rt*2+1] += lazy[rt];sum[rt*2] += lazy[rt]*(mid-l+1);sum[rt*2+1] += lazy[rt]*(r-mid);lazy[rt] = 0;}
}
void Set(int rt, int l, int r, int i, int j, int val)
{if(i <= l && j >= r){set[rt] = val;lazy[rt] = 0;sum[rt] = val*(r-l+1);return;}pushdown(rt, l, r);int mid = (l+r)/2;if(i <= mid) Set(rt*2, l, mid, i, j, val);if(j > mid) Set(rt*2+1, mid+1, r, i, j, val);pushup(rt);
}
void update(int rt, int l, int r, int i, int j, int val)
{if(i <= l && j >= r){lazy[rt] += val;sum[rt] += val*(r-l+1);return ;}pushdown(rt, l, r);int mid = (l+r)/2;if(i <= mid) update(rt*2, l, mid, i, j ,val);if(j > mid) update(rt*2+1, mid+1, r, i, j, val);pushup(rt);
}
int query(int rt, int l, int r, int i, int j)
{if(i <= l && j >= r) return sum[rt];pushdown(rt, l, r);int mid = (l+r)/2;int res = 0;if(i <= mid) res += query(rt*2, l, mid, i, j);if(j > mid) res += query(rt*2+1, mid+1, r, i, j);pushup(rt);return res;
}
int main()
{int t, n, x, m, k, ca = 1;cin >> t;while(t--){memset(sum, 0, sizeof(sum));memset(set, 0, sizeof(set));scanf("%d%d%d", &n, &m, &k);while(k--){scanf("%d", &x);Set(1, 1, n, x, x, 1);}for(int i = 1; i <= m; i++)scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].val);sort(a+1, a+1+m,cmp);int flag = 0, ans = 0;for(int i = 1; i <= m; i++){int l = a[i].l, r = a[i].r;if(r-l+1 < a[i].val) flag = 1;if(flag) break;int li = l, ri = r, mid;int cnt = max(0, a[i].val-query(1,1,n,l,r));if(cnt <= 0) continue;int res;while(li <= ri){mid = (li+ri)/2;if(r-mid+1-query(1,1,n,mid,r) >= cnt) li = mid + 1, res = mid;else ri = mid - 1; }Set(1,1,n,res,r,1);ans += cnt; }if(flag) printf("Case %d: -1\n", ca++);elseprintf("Case %d: %d\n", ca++, ans);}return 0;
}
2017 华东师范大学网赛 F.丽娃河的狼人传说 Problem #3263( 贪心)相关推荐
- 2017华东师范大学网赛-拼音魔法
拼音魔法 Time limit per test: 1.0 seconds Time limit all tests: 1.0 seconds Memory limit: 256 megabytes ...
- ECNU 3263 丽娃河的狼人传说(差分约束)
丽娃河的狼人传说 Time limit per test: 1.0 seconds Memory limit: 256 megabytes 丽娃河是华师大著名的风景线.但由于学校财政紧缺,丽娃河边的路 ...
- 华东师范大学计算机模拟试题,华东师范大学网院《计算机入门》模拟试题(卷)(ABCD卷合,含详细答案解析).doc...
WORD文档下载可编辑 PAGE 专业技术资料 精心整理分享 华东师范大学网院<计算机入门>模拟试卷 (ABCD卷合,含详细答案) 一.单选题(40分) 1.世界上第一台电子数字计算机采用 ...
- 华东师范大学计算机科学与软件工程6,2017华东师范大学计算机科学与软件工程学院考试科目范围一览.pdf...
2017 华东师范大学计算机科学与软件工程学院考试科目范围一览 序 招 生 招生院系代 考试科目 初试范围 号 年 码及名称 代码级名称 160 计算机 101 思想 教育部统一命题,参见教育部考试中 ...
- 2017 ACM-ICPC西安网赛B-Coin
B-Coin Bob has a not even coin, every time he tosses the coin, the probability that the coin's front ...
- 大学生程序设计邀请赛(华东师范大学)
C. 袋鼠妈妈找孩子 Time limit per test: 1.5 seconds Memory limit: 256 megabytes Accept / Submit: 41 / 172 袋鼠 ...
- 拼音魔法-华东师范大学程序设计竞赛-ecnu3256
拼音魔法click here 模拟题,轻声判断v转换为u还是ü:1至4升是一样的解法.先判断有无a o e, 若有,替换为带声调的:若无,继续判断有无i u,若有,替换:若无,继续判断有无v,若有,替 ...
- 大学生程序设计邀请赛(华东师范大学) 黑心啤酒厂
黑心啤酒厂 Time limit per test: 1.0 seconds Time limit all tests: 1.0 seconds Memory limit: 256 megabytes ...
- 2017大学生程序设计邀请赛(华东师范大学) A.拼音魔法
传送门:http://acm.ecnu.edu.cn/problem/3256/ 魔法学校小学一年级有一种题.就是给一个字的拼音,给一个声调,让你正确地注音.但魔法老师给了巨量的题,你不用魔法根本不可 ...
最新文章
- 用STM32实现:摄像头扫到二维码后提取二维码中的信息分别放到数组中
- UA MATH567 高维统计I 概率不等式7 亚指数性与亚指数分布
- tomcat端口被占用
- UPS故障案例集(二)
- NOIP2001-普及组复赛-第一题-数的计算
- Spring quartz 并发性研究
- 《财富》:盖茨的四项黄金法则
- java iterator_Java ArrayDeque iterator()方法与示例
- angularjs -- 监听angularJs列表数据是否渲染完毕
- 11.MongoDB之副本集与Oplog
- Hadoop组件启动的三种方式及配置SSH无密码登入
- MySQL MYISAM引擎表锁和行锁详解
- html圆圈里面有歌词,html5+js带滚动歌词的音乐播放器(同时支持列表,json) | 小灰灰博客...
- Pygame小游戏之俄罗斯方块凭什么火了30年?(史上最畅销单机游戏)
- 由《爆裂鼓手》引发的产品思考
- 世界互联网大会上有哪些黑客科技值得关注?
- 企业注销的债权债务如何处理
- 居中小圆点html,圆点怎么打
- adblock plus 广告过滤器的使用
- [转]建行B2B支付回调参数乱码现象解析