zoukankan      html  css  js  c++  java
  • 节约用电

    题目链接

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

    思路

    我觉得这道题是有问题的,题目统计了位置相同的灯,但是答案错了,因为请看给的“标程”。

    解释写在里面了。

    ”标程“

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10;
    int a[maxn];
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i=0;i<n;i++) {
            scanf("%d",&a[i]);
        }    
        sort(a,a+n);
        int ans=0;
        int last=a[0];
        for (int i=1;i<n-1;i++) {
            printf("%d
    ",a[i+1]-last);
            if (a[i+1]-last<=m) {//错误写法,为什么从1开始还跳过了下标为1的灯呢
                ans++;
            }
            else {
                last=a[i];
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
    
    

    这里给出我的解法

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

    代码

    #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
    ",pre);
        int ans=0,i=pre;
        while (i<last) {
            printf("beg: %d
    ",i);
            int r=i+m-1;
            i++;
            while (i<last&&i<=r) {
                if (a[i]) {
                    ans+=a[i];
                    printf("i a[i] %d %d
    ",i,a[i]);
                    a[i]=0;
                    
                }
                i++;
            }
            printf("end: %d
    
    ",i);
            // printf("ans %d
    ",ans);
            while (i<last&&!a[i]) {
                i++;
            }
        }
        printf("%d
    ",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
    */
    
  • 相关阅读:
    note 11 字典
    note10 元组
    hibernate环境搭建及操作
    JAVA中解决Filter过滤掉css,js,图片文件等问题
    过滤器
    MySQL存储过程(转)
    用java调用oracle存储过程总结(转)
    Oracle分页查询语句的写法(转)
    数据访问类的封装
    事务的特性及事务的隔离级别(转)
  • 原文地址:https://www.cnblogs.com/xyqxyq/p/12381851.html
Copyright © 2011-2022 走看看