其他分享
首页 > 其他分享> > 节约用电(这个题有问题)

节约用电(这个题有问题)

作者:互联网

思路

我觉得这道题是有问题的,题目没有统计位置相同的灯,所以答案错了,数据也没有避免这样的情况,这里给出我的解法

下面那组数据的答案应该是10

https://nanti.jisuanke.com/t/T1742

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int maxn=1e6+10;
int a[maxn];
int main()
{
    // freopen("in.txt","r",stdin);
    int n,m,loc,last=-1;
    scanf("%d%d",&n,&m);
    for (int i=0;i<n;i++) {
        scanf("%d",&loc);
        a[loc]++;
        last=max(last,loc);
    }
    int pre=0;
    while (!a[pre]) {
        pre++;
    }
    for (int i=0;i<=last;i++) {
        if (a[i])
            printf("%d ",i);
    }
    // printf("i: %d\n",pre);
    int ans=0,i=pre;
    while (i<last) {
        printf("beg: %d\n",i);
        int r=i+m-1;
        i++;
        while (i<last&&i<=r) {
            if (a[i]) {
                ans+=a[i];
                printf("i a[i] %d %d\n",i,a[i]);
                a[i]=0;
                
            }
            i++;
        }
        printf("end: %d\n\n",i);
        // printf("ans %d\n",ans);
        while (i<last&&!a[i]) {
            i++;
        }
    }
    printf("%d\n",ans);
    return 0;
}
/*
7 2 
1 2 3 4 4 4 4

8 12 14 15 19 38 39 47 53 69 75 86 100
*/

标签:10,题有,int,节约用电,long,问题,maxn,答案
来源: https://www.cnblogs.com/xyqxyq/p/12381851.html