LightOJ-1016-Brush (II)


題目傳送門


題意:有一個寬爲w的刷子,刷子只能沿着x軸刷,有n個灰塵的座標,問至少多少次才能刷完所有的灰塵。

思路:排序之後直接枚舉。

#include <bits/stdc++.h>

using namespace std;

int y[50000+1000];
int n, w;
bool cmp(int a, int b)
{
    return a<b;
}
int main(void)
{
    int T, cas=1;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d %d", &n, &w);
        for (int i = 0; i < n; i++)
            scanf("%*d %d", &y[i]);
        sort(y,y+n,cmp);
        int ans = 1, tmp = y[0]+w;
        for (int i = 1; i < n; i++)
        {
            if (y[i]>tmp)
            {
                tmp = y[i]+w;
                ans++;
            }
        }
        printf("Case %d: %d\n", cas++, ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章