链接:http://acm.ecnu.edu.cn/problem/3263/

题意:

从 1 到 n 的一条数轴。有 m 个区间至少要安装一定数量的路灯,路灯只能装在整数点上,有k盏路灯已经安装好 ,现在求最少需要安装多少盏路灯。

分析:

一开始我的想法是按重叠部分给数轴每个整数点一个优先级,然后在区间中优先度高的先补灯。但比赛中无论如何都是WA,最后找出错误是因为这样补灯优先级相同时候他会按顺序补灯。

最后看题解发现他是根据右端点升序排序进行处理,然后不满足的话尽量往右边补灯,因为如果按右端点排序,那么每一个区域的左边肯定先被前一个区域考虑到了。

给出两组组数据, 第一组证明我第一个想法是错误的, 第二组测试。

8 3 1

8

1 6 2

1 3 1

4 6 1

1

答案是:2
9 5 6
1 2 3 4 5 7
1 5 4
4 8 5
1 3 3
2 9 7
1 8 3

答案是:2

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4;
struct K
{
int x, y, num;
friend bool operator < (K a, K b)
{
return a.y < b.y;
} };
K ins[1000];
bool lap[maxn];
int n,m,k,ans,flag;
int main()
{
// freopen("1.txt","r",stdin);
int t;
scanf("%d", &t);
for(int kase = 1; kase <= t; kase ++)
{
memset(lap,0,sizeof(lap));
scanf("%d %d %d", &n, &m, &k);
for(int i = 0; i < k; i++)
{
int t;
scanf("%d", &t);
lap[t] = 1;
}
for(int i = 0; i < m; i++)
{
scanf("%d %d %d", &ins[i].x, &ins[i].y, &ins[i].num);
}
sort(ins, ins+m); ans = 0,flag = 0;
for(int i = 0; i < m; i++)
{
int b = ins[i].x, e = ins[i].y, need = ins[i].num;
int sum = 0;
for(int j = e; j >= b; j--)
{
if(lap[j])
{
sum++;
} }
int pos = e;
while(sum < need && pos >= 1)
{
if(!lap[pos])
{lap[pos] = 1;
sum++;
ans++;
}
pos--;
}
if(sum < need)
{
flag = 1;
break;
}
}
if(flag) printf("Case %d: -1\n",kase);
else printf("Case %d: %d\n",kase,ans);
} }

  

最新文章

  1. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
  2. nio
  3. Tomcat服务器本地的搭建,以及在 IDEA软件下的配置,以及项目的测试运行(基于supermvc框架下的web)
  4. Objective-C
  5. Shiro--权限控制
  6. HTTP请求方法对照表
  7. php用curl获取远端网页内容
  8. 如何用命令的方式查看你的Office2010密钥是否是永久的有效
  9. Servlet中乱码问题
  10. [UOJ 74] 【UR #6】破解密码
  11. cocos2d&amp;amp;cocos2dx学习资源
  12. ASP.NET中的C#基础知识
  13. PC版模块滚动不显示滚动条效果
  14. UNIX网络编程——套接字选项(SO_RCVBUF和SO_SNDBUF)
  15. mysql进阶(六)模糊查询的四种用法介绍
  16. 初探WebAssembly
  17. iUAP云运维平台v3.0全面支持基于K8s的微服务架构
  18. 关于JavaScript(脚本语言)
  19. mac python3 conda pytorch出错:libc++abi.dylib: terminating with uncaught exception of type NSException
  20. springsession 实现session 共享

热门文章

  1. poj 3159 Candies dijkstra + queue
  2. Wannafly挑战赛10:A题:小H和迷宫
  3. ASP.NET URLRewriter重写
  4. spring实现模板文件下载
  5. Spring Cloud是什么?
  6. negroni-gzip源码简单分析解读
  7. Java用SAX解析XML
  8. vue组件中—bus总线事件回调函数多次执行的问题
  9. robotframework + python2.7.9 + selenium 2.44.0 + selenium2library1.7 测试环境搭建成功!
  10. .net 操作xml --移除注释节点