『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号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...
随机推荐
- Linux基础之命令练习Day1-init,who,date,cal,man,clear,passwd,su,whoami,mkdir,touch,rm,cp,mv,head,tail,more,less,echo
开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 使用命令退出虚拟终端2上登录的用户 使用快捷键切 ...
- oozie安装总结
偶然的机会,去面试的时候听面试官讲他们的调度系统是基于hue+oozie,以前一直没有接触过,今天趁有空,尝试一下oozie 1.环境说明 cat /etc/issue CentOS release ...
- C# Winform DataGridView获取单元格的值
1,可以直接通过DataGridView的重载运算符[]直接获取 使用方法: dataGridView[columnIndex][rowsIndex].Value.ToString().//colum ...
- Python爬虫教程-28-Selenium 操纵 Chrome
我觉得本篇是很有意思的,闲着没事来看看! Python爬虫教程-28-Selenium 操纵 Chrome PhantomJS 幽灵浏览器,无界面浏览器,不渲染页面.Selenium + Phanto ...
- GreenDao 初体验
GreenDao 使用 环境搭建(android studio) project的build.gradle buildscript { repositories { google() jcenter( ...
- Genymotion模拟器拖入文件报An error occured while deploying the file的错误
今天需要用到资源文件,需要将资源文件拖拽到sd卡中,但老是出现这个问题: 资源文件拖不进去genymotion.查看了sd的DownLoad目录,确实没有成功拖拽进去. 遇到这种问题的,我按下面的思路 ...
- Oracle归档模式和非归档模式的区别
一.查看oracle数据库是否为归档模式: Sql代码1.select name,log_mode from v$database; NAME LOG_MODE ------------------ ...
- cocos2d在IOS嵌入UM应用推荐
因为cocos2d默认建立的项目,没用使用导航界面,所以如果直接导航到应用推荐页面将无法返回. 所以我做了一些修改: AppController.mm中 用导航界面包装一下默认的viewControl ...
- Hyperledger Fabric 1.0 学习搭建 (一)--- 基础环境搭建
1: 环境构建在本文中用到的宿主机环境是Centos ,版本为Centos.x86_64 7.2, 一定要用7版本以上, 要不然会安装出错. 通过Docker 容器来运行Fabric的节点,版本为v1 ...
- xcode9 unity3d 新坑
1.metal调试会报错,要在edit scheme里关掉