『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席、团委团总支、社团社长都体验过一番了,现在差个班长也没试过,就来体验了一番哈哈哈,其实这种精心服务一个班级的人还是很棒的一种感觉呢。思考思考最近的任务啊:
(1)英语剧
(2)三下乡公益策划
(3)兼职 - 影视剧组后期特效
(3)三月底程序设计大赛天梯赛
(4)班会以及班级细节事件处理
(5)多模态视频处理
(6)兼职 - 校方艺考航拍记录
(7)六月四级考试和三下乡
(8)七月暑假学车
(9)兼职 - Eddy工作
哇 大致想了想我真的可以忙死加学到死鸭,嘛,能者多劳,扛住就是胜利!!!好啦~不扯多,今天ACM集训队训练有道坑题得六个心眼,妈耶交了九次WA九次,最后一次交终于AC,哭迈
今日兴趣新闻:
MWC折叠屏刷屏背后的一场大戏:5家厂商恩怨情仇史
链接:https://mbd.baidu.com/newspage/data/landingsuper?context=%7B"nid"%3A"news_9543883370009568888"%7D&n_type=0&p_from=1
最近华为的新手机在朋友圈刷了不少圈,这种合并平板和手机的方式说不定真的会在未来畅销,因为这两者的用户需求量本来就是挺大的~,这篇新闻还是蛮有趣的讲了这几年的势头,可以看看。
------------------------------------------------题目----------------------------------------------------------
A. Points in Segments
You are given a set of nn segments on the axis OxOx, each segment has integer endpoints between 11 and mm inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers lili and riri (1≤li≤ri≤m1≤li≤ri≤m) — coordinates of the left and of the right endpoints.
Consider all integer points between 11 and mm inclusive. Your task is to print all such points that don't belong to any segment. The point xxbelongs to the segment [l;r][l;r] if and only if l≤x≤rl≤x≤r.
Input
The first line of the input contains two integers nn and mm (1≤n,m≤1001≤n,m≤100) — the number of segments and the upper bound for coordinates.
The next nn lines contain two integers each lili and riri (1≤li≤ri≤m1≤li≤ri≤m) — the endpoints of the ii-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that li=rili=ri, i.e. a segment can degenerate to a point.
Output
In the first line print one integer kk — the number of points that don't belong to any segment.
In the second line print exactly kk integers in any order — the points that don't belong to any segment. All points you print should be distinct.
If there are no such points at all, print a single integer 00 in the first line and either leave the second line empty or do not print it at all.
input
output
input
output
Note
In the first example the point 11 belongs to the second segment, the point 22 belongs to the first and the second segments and the point 55belongs to the third segment. The points 33 and 44 do not belong to any segment.
In the second example all the points from 11 to 77 belong to the first segment.
------------------------------------------------题目----------------------------------------------------------
(一) 原题大意:
题目愿意是:输入四个量:L、V、left、right,其中从1到L为范围界,即1<=x<=L,当满足x可以整除V时,说明该处有灯笼,但left和right这个范围内没有灯笼,请统计有几个灯笼。
题目感觉是不难的,但就是在一些端点特值处理上会有些问题。
(二) 题目分析:
这里记录一下我原本的错误思想:
第一次我的错误想法是直接就for暴力搜索了,结果就会超时Time limit exceeded on test 2
错误代码:
#include<stdio.h>
int times,L,V,left,right;
int ans_light;
int main()
{
scanf("%d",×);
while(times--)
{
ans_light = ;
scanf("%d %d %d %d",&L,&V,&left,&right);
for(int i = ;i<left;i++) if(i%V == ) ans_light++;
for(int i = right+;i<=L;i++) if(i%V == ) ans_light++;
printf("%d\n",ans_light);
}
return ;
}
谨记千万没事就不要暴搜,看看有没有快捷的方式可以快速得到答案,这是ACM要训练我们的。
第二个错误的想法:
原本的想法是用了ceil函数,即向上取整,让V输入的数是个浮点数,代码大概如下:
ans_light_1 = ceil((left - )/V);
ans_light_2 = ceil((L - right)/V);
printf("%d\n",ans_light_1+ans_light_2);
然而这个做法,在V小于left的时候,答案是正确的,交了半天发现当V大于等于left的时候就会出现问题了
所以第三个错误想法,我就把V和left的两种情况区分开来讨论了:
#include<stdio.h>
#include<math.h>
int times,L,left,right;
float V;
int ans_light_1,ans_light_2;
int main()
{
scanf("%d",×);
while(times--)
{
ans_light_1 = ans_light_2 = ;
scanf("%d %f %d %d",&L,&V,&left,&right);
if(V<left)
{
ans_light_1 = ceil((left - )/V);
ans_light_2 = ceil((L - right)/V);
printf("%d\n",ans_light_1+ans_light_2);
}
else
{
ans_light_1 = L/int(V);
ans_light_2 = right/int(V) - left/int(V);
if(left % int(V) == )
{
printf("%d\n",ans_light_1 - ans_light_2 - );
continue;
}
printf("%d\n",ans_light_1 - ans_light_2);
}
}
return ;
}
结果写到这里的时候,突然发现可以直接用一个公式直接求解:

这里的灯笼数,实际上等于 L / V 减去 ( r / V - ( l - 1 ) / V ) 这样就能得到结果了。
(三) AC代码:
因为代码比较简单,就不分块了,直接献上,给自己留个提醒。
#include<stdio.h>
#include<math.h>
int times,L,left,right;
int V;
int ans_light_1,ans_light_2;
int main()
{
scanf("%d",×);
while(times--)
{
ans_light_1 = ans_light_2 = ;
scanf("%d %d %d %d",&L,&V,&left,&right);
ans_light_1 = L/V;
ans_light_2 = right/V - (left-)/V;
printf("%d\n",ans_light_1 - ans_light_2);
}
return ;
}
(四)AC截图:

注:我还是个渣渣辉,代码可能写得不够高效不够好,我也会努力优化,如果有更好的解法,真心希望您能够评论留言贴上您的代码呢~互相帮助互相鼓励才能成长鸭~~
『ACM C++』 Codeforces | 1066A - Points in Segments的更多相关文章
- 『ACM C++』 Codeforces | 1005D - Polycarp and Div 3
今天佛了,魔鬼周一,在线教学,有点小累,但还好,今天AC了一道,每日一道,还好达成目标,还以为今天完不成了,最近任务越来越多,如何高效完成该好好思考一下了~最重要的还是学业的复习和预习. 今日兴趣新闻 ...
- 『ACM C++』 Codeforces | 1003C - Intense Heat
今日兴趣新闻: NASA 研制最强推进器,加速度可达每秒 40 公里,飞火星全靠它 链接:https://mbd.baidu.com/newspage/data/landingsuper?contex ...
- 『ACM C++』 Codeforces | 1066B - Heaters
今日不写日感,直接扔上今日兴趣点: 新研究称火星曾经有一个巨大的地下水系统 链接:https://mbd.baidu.com/newspage/data/landingsuper?context=%7 ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 007-011
真的是忙头晕了,学业.ACM打题.班级活动.自学新东西,哇这充实的大学~ ------------------------------------------------L1-007--------- ...
- 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)
呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...
- 『ACM C++』HDU杭电OJ | 1416 - Gizilch (DFS - 深度优先搜索入门)
从周三课开始总算轻松了点,下午能在宿舍研究点题目啥的打一打,还好,刚开学的课程还算跟得上,刚开学的这些课程也是复习以前学过的知识,下半学期也不敢太划水了,被各种人寄予厚望之后瑟瑟发抖,只能努力前行了~ ...
- 『ACM C++』HDU杭电OJ | 1425 - sort (排序函数的特殊应用)
今天真的是累哭了,周一课从早八点半一直上到晚九点半,整个人要虚脱的感觉,因为时间不太够鸭所以就回头看看找了一些比较有知识点的题来总结总结分析一下,明天有空了就开始继续打题,嘻嘻嘻. 今日兴趣电影: & ...
- 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)
今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...
- 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester
这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...
随机推荐
- Apache服务器运维笔记(5)----容器的处理顺序
容器在配置文件中是可以多次使用的,同时也可以嵌套使用,但是 Apache 在处理容器时却是有一定顺序的,因此在编写容器配置时需要按照一定的顺序来进行,否则Apache处理的结果很可能不是管理员想要的. ...
- 如何使用idea把web项目打成war包
如果是maven项目,打成war包很容易,如果是web项目,需要这样子 1. 2. 3. output directory是war包的目录 4.重新选择 第一步的操作,选择build即可.
- QML Delegate中访问该持有者的方式 附加属性(转载)
http://blog.csdn.net/yuxiaohen/article/details/17226971 用法很奇葩记录一下,实测可以,用于弱化delegate与持有者的依赖 delegat ...
- android webview 播放 video经验总结
在目前PC浏览器上,对video的支持基本都没什么问题了.但是如果用webview去跑这样的页面就会遇到许多问题. 下面一段html <!DOCTYPE html> <html> ...
- 【Leetcode】【Medium】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- antd Grid
import { Row, Col } from 'antd'; <Row type="flex" //内容布局(左靠齐,右靠齐,居中) justify="star ...
- February 8 2017 Week 6 Wednesday
Goals determine what you are going to be. 目标决定你将成为什么样的人. Owning some goals in life means you will ha ...
- 在Windows 10上部署Apache PredictionIO开发环境
Windows在初研究人员的探索下,研究出了一套更为精简的环境配置,极大的缩短了开发时间与效率,在此总结以供后来者参阅. 1.部署环境的配置 Windows10 64 home IntelliJ ID ...
- 不规矩的xml与JAVA对象互相转换的小技巧-使用Marshaller
摘要:将XML文档与JAVA对象互转是很常见的需求,如果XML定义很规整这很好实现.然而在现实中“不规矩”的XML可能更常见,Marshaller便无能为力了吗?下面是一个小技巧,调整一下思维便能重用 ...
- MQ中将消息发送至远程队列的配置
MQ中将消息发送至远程队列的配置 摘自MQ资源管理器帮助文档V7 在开始学习本教程之前,您需要从系统管理员处了解标识网络上接收机器的名称:IP地址.MQ的端口号.队列管理器.接收(远程机器)或者是发送 ...