CF44C Holidays

https://www.luogu.com.cn/problem/CF44C
涉及知识点:模拟,桶排序,差分,前缀和
黄色题

主要思路(有点桶排序的思想,x为桶):

  • 输入n和m
  • 循环输入a和b(不用数组,边输入边执行)
  • 循环a~b,把x(初始为0)的a~b个元素+1(因为我是从0开始,所以会减1)
  • 循环n遍,如果不符合要求直接输出,结束程序
  • 程序未结束输出OK

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,a,b;    //RT
    cin>>n>>m;    //输入
    int x[n]={0};    //初始为0
    for(int i=0;i<m;i++)    /循环m遍
    {
        cin>>a>>b;    //输入
        for(int i=a-1;i<b;i++)    //从0开始,所以-1
        {
            x[i]++;    //x相当于“桶”
        }
    }
    for(int i=0;i<n;i++)    //循环n遍模拟每天浇花
    {
        if(x[i]!=1)    //如果有问题
        {
            cout<<i+1<<' '<<x[i];    //主要+1然后输出
            return 0;    //直接结束
        }
    }
    cout<<"OK";    //如果还没结束就是符合要求,输出
    return 0;    //结束
}


 

 

 

posted @ 2022-07-13 18:35  -イレイナ  阅读(29)  评论(0)    收藏  举报