1. 程式人生 > >『ACM C++』 Codeforces | 1066A - Points in Segments

『ACM C++』 Codeforces | 1066A - Points in Segments

努力 scanf side should 搜索 http it is fir next

  大一生活真 特麽 ”豐富多彩“ ,多彩到我要忙到哭泣,身為班長,很多班級的事情需要管理,也是,什麽東西都得體驗學一學,從學生會主席、團委團總支、社團社長都體驗過一番了,現在差個班長也沒試過,就來體驗了一番哈哈哈,其實這種精心服務一個班級的人還是很棒的一種感覺呢。思考思考最近的任務啊:

  (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 (1lirim1≤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 lxrl≤x≤r.

Input

The first line of the input contains two integers nn and mm (1n,m1001≤n,m≤100) — the number of segments and the upper bound for coordinates.

The next nn lines contain two integers each lili and riri (1lirim1≤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.

Examples

input

3 5
2 2
1 2
5 5

output

2
3 4 

input

1 7
1 7

output

 0

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",&times);
    while(times--)
    {
        ans_light = 0;
        scanf("%d %d %d %d",&L,&V,&left,&right);
        for(int i = 1;i<left;i++) if(i%V == 0) ans_light++;
        for(int i = right+1;i<=L;i++) if(i%V == 0) ans_light++;
        printf("%d\n",ans_light);
    }
    return 0;
 } 

    謹記千萬沒事就不要暴搜,看看有沒有快捷的方式可以快速得到答案,這是ACM要訓練我們的。

    第二個錯誤的想法:

    原本的想法是用了ceil函數,即向上取整,讓V輸入的數是個浮點數,代碼大概如下:

             ans_light_1 = ceil((left - 1)/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",&times);
    while(times--)
    {
        ans_light_1 = ans_light_2 = 0;
        scanf("%d %f %d %d",&L,&V,&left,&right); 
        if(V<left)
        {
             ans_light_1 = ceil((left - 1)/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) == 0)
            {
                printf("%d\n",ans_light_1 - ans_light_2 - 1);
                continue;
            }
            printf("%d\n",ans_light_1 - ans_light_2);
        }
    }
    return 0;
 } 

    結果寫到這裏的時候,突然發現可以直接用一個公式直接求解:

技術分享圖片

    這裏的燈籠數,實際上等於 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",&times);
    while(times--)
    {
        ans_light_1 = ans_light_2 = 0;
        scanf("%d %d %d %d",&L,&V,&left,&right); 
        ans_light_1 = L/V;
        ans_light_2 = right/V - (left-1)/V;
        printf("%d\n",ans_light_1 - ans_light_2);
    }
    return 0;
 } 

(四)AC截圖:

技術分享圖片

註:我還是個渣渣輝,代碼可能寫得不夠高效不夠好,我也會努力優化,如果有更好的解法,真心希望您能夠評論留言貼上您的代碼呢~互相幫助互相鼓勵才能成長鴨~~

『ACM C++』 Codeforces | 1066A - Points in Segments