zoukankan      html  css  js  c++  java
  • ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(线段树)

    链接:https://nanti.jisuanke.com/t/30996

    题意:很简单,懒得讲了

    思路:我们可以直接用线段树求出区间最左边小于某个数的数,线段树存1-n房间的灯数,维护区间最小值,查询的时候优先向左走就好了,

    一个房间满了我们就用线段树将这个房间的值更新为inf,然后模拟下就好了。这样操作次数最多不大于1e5次,复杂度为 O(nlog(n))。

    这道题算是一道非常基础的线段树,没什么复杂的操作,注意一次更新可能会有多个房间被填满,加个while处理下就好了。

    之前那份代码忘了加个判断条件:当所有房间都被填满,那么值会不变,忘了加这个也能过数据有点水啊。

    实现代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    const int inf = 1e9;
    const int M = 2e5+10;
    int sum[M<<2],n,k,q;
    int a[M],b[M],x[M],y[M];
    
    void update(int p,int c,int l,int r,int rt)
    {
         if(l == r && l == p){
            sum[rt] = c;
            return ;
         }
         int m = (l + r) >> 1;
         if(p <= m) update(p,c,lson);
         else update(p,c,rson);
         sum[rt] = min(sum[rt<<1],sum[rt<<1|1]);
    }
    
    int query(int p,int l,int r,int rt)
    {
        if(l == r) return l;
        int m = (l + r) >> 1;
        if(sum[rt<<1] <= p) return query(p,lson);
        if(sum[rt<<1|1] <= p) return query(p,rson);
        return -1;
    }
    
    int main()
    {
        scanf("%d%d",&n,&k);
        for(int i = 1;i <= n;i ++){
            scanf("%id", &a[i]);
            update(i,a[i],1,n,1);
        }
        scanf("%d",&q);
        int mx = 0;
        for(int i = 1;i <= q;i++){
            scanf("%d",&b[i]);
            mx = max(b[i],mx);
        }
        int ans = 0, cnt = 0;
        for(int i = 1;i <= mx;i++){
            if(cnt == n){
                x[i] = cnt; y[i] = ans;
                continue;
            }
            else
            ans += k;
            while(1){   //一次更新可能有多个房间被填满
                int num = query(ans, 1, n, 1);
                if(num != -1) {
                    cnt++, ans -= a[num];
                    update(num, inf, 1, n, 1);
                    x[i] = cnt, y[i] = ans;
                }
                else {
                    x[i] = cnt, y[i] = ans;
                    break;
                }
            }
    
        }
        for(int i = 1;i <= q;i++)
            printf("%d %d
    ",x[b[i]],y[b[i]]);
    
        return 0;
    }
  • 相关阅读:
    用asp生成PDF文件
    对长了的文章进行分页显示!
    用ASP读INI配置文件的函数
    JMail发送邮件代码
    SqlServer数据库的备份和恢复措施
    java—mediator中介模式
    javascript王者归来公有和私有:属性的封装
    断点续传多线程连接下载
    C语言实现对图像的二值化
    javascript王者归来类和对象
  • 原文地址:https://www.cnblogs.com/kls123/p/9571154.html
Copyright © 2011-2022 走看看