zoukankan      html  css  js  c++  java
  • Lpl and Energy-saving Lamps(二分+线段树)

    描述

    传送门:我是传送门

    During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the Castle? Dragon smiled enigmatically and answered that it is a big secret. After a pause, Dragon added:

    — We have a contract. A rental agreement. He always works all day long. He likes silence. Besides that, there are many more advantages of living here in the Castle. Say, it is easy to justify a missed call: a phone ring can’t reach the other side of the Castle from where the phone has been left. So, the imprisonment is just a tale. Actually, he thinks about everything. He is smart. For instance, he started replacing incandescent lamps with energy-saving lamps in the whole Castle…

    Lpl chose a model of energy-saving lamps and started the replacement as described below. He numbered all rooms in the Castle and counted how many lamps in each room he needs to replace.

    At the beginning of each month, Lpl buys mm energy-saving lamps and replaces lamps in rooms according to his list. He starts from the first room in his list. If the lamps in this room are not replaced yet and Lpl has enough energy-saving lamps to replace all lamps, then he replaces all ones and takes the room out from the list. Otherwise, he’ll just skip it and check the next room in his list. This process repeats until he has no energy-saving lamps or he has checked all rooms in his list. If he still has some energy-saving lamps after he has checked all rooms in his list, he’ll save the rest of energy-saving lamps for the next month.

    As soon as all the work is done, he ceases buying new lamps. They are very high quality and have a very long-life cycle.

    Your task is for a given number of month and descriptions of rooms to compute in how many rooms the old lamps will be replaced with energy-saving ones and how many energy-saving lamps will remain by the end of each month.

    输入

    Each input will consist of a single test case.

    The first line contains integers nn andm(1n100000,1m100)(1≤n≤100000,1≤m≤100)— the number of rooms in the Castle and the number of energy-saving lamps, which Lpl buys monthly.

    The second line contains nn integers k1,k2,,knk1,k2,,knk1,k2,…,knk1,k2,…,kn
    (1kj10000,j=1,2,,n)(1kj10000,j=1,2,,n)(1≤kj≤10000,j=1,2,…,n)(1≤kj≤10000,j=1,2,…,n) — the number of lamps in the rooms of the Castle. The number in position jj is the number of lamps in jthj−th room. Room numbers are given in accordance with Lpl’s list.

    The third line contains one integer q(1q100000)q(1q100000)q(1≤q≤100000)q(1≤q≤100000) — the number of queries.

    The fourth line contains qq integers d1,d2,,dqd1,d2,,dqd1,d2,…,dqd1,d2,…,dq
    (1dp100000,p=1,2,,q)(1dp100000,p=1,2,,q)(1≤dp≤100000,p=1,2,…,q)(1≤dp≤100000,p=1,2,…,q) — numbers of months, in which queries are formed.

    Months are numbered starting with 11; at the beginning of the first month Lpl buys the first m energy-saving lamps.

    输出

    Print qq lines.

    Line pp contains two integers — the number of rooms, in which all old lamps are replaced already, and the number of remaining energy-saving lamps by the end of dpdpmonth.

    样例

    输入

    5 4
    3 10 5 2 7
    10
    5 1 4 8 7 2 3 6 4 7

    输出

    4 0
    1 1
    3 6
    5 1
    5 1
    2 0
    3 2
    4 4
    3 6
    5 1

    Note

    Explanation for the sample:

    In the first month, he bought 44 energy-saving lamps and he replaced the first room in his list and remove it. And then he had 11 energy-saving lamps and skipped all rooms next. So, the answer for the first month is 1,111,1———1 room’s lamps were replaced already, 11 energy-saving lamp remain.

    题意

    一共mm个房间,小A(A)(暂且叫他小A)每个月都要将房间的灯换成节能灯,但是小A换灯的时候有个习惯,必须要能够一次性将一个房间里的灯全部换掉才会去换掉这个房间的灯,否则就跳过这个房间去下一个房间重复这个步骤

    每个月小A都会去买mm个灯,然后一直换下去

    现在每次询问都要输出两个值,当前已经换掉的房间与手中剩下的灯数

    思路

    直接暴力肯定会超时
    直接套用线段树模板来维护区间最小值,每次找到最左端满足条件的房间然后将该房间的灯全部换掉

    查找的时候可以直接二分找到最左端满足条件的房间

    每次换掉一个房间以后该房间的灯的数量加1e5+101e5+10 ,因为正常情况下每个房间的灯的数量不会超过1e41e4,所以当所有房间的灯都超过1e5+101e5+10之后,也就代表了所有房间已经全部被换成了节能灯

    二分查找的过程还可以优化一下,但是不优化也不会超时,懒得改了

    线段树模板:线段树模板(含区间最大(小)值)

    代码

      1 /*
      2  * =========================================================================
      3  *
      4  *       Filename:  G.cpp
      5  *
      6  *           Link:  https://nanti.jisuanke.com/t/30996
      7  *
      8  *        Version:  1.0
      9  *        Created:  2018/09/01 14时55分32秒
     10  *       Revision:  none
     11  *       Compiler:  g++
     12  *
     13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
     14  *   Organization:  QLU_浪在ACM
     15  *
     16  * =========================================================================
     17  */
     18 #include <bits/stdc++.h>
     19 using namespace std;
     20 #define clr(a, x) memset(a, x, sizeof(a))
     21 #define rep(i,a,n) for(ll i=a;i<=n;i++)
     22 #define pre(i,a,n) for(int i=a;i>=n;i--)
     23 #define ll long long
     24 #define max3(a,b,c) fmax(a,fmax(b,c))
     25 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     26 const double eps = 1e-6;
     27 const int INF = 0x3f3f3f3f;
     28 const int inf = 1e5+10;
     29 const int mod = 1e9 + 7;
     30 const int MAXN = 1e6+100;
     31 int have[MAXN],now[MAXN];
     32 int n,m,q,que,haved;
     33 int a[MAXN];
     34 // a[]为原序列信息
     35 int mn[MAXN*5];
     36 // mn[]区间最小值
     37 
     38 void PushUp(int rt)
     39 {
     40     mn[rt] = min(mn[rt<<1],mn[rt<<1|1]);   // 区间最小值
     41 }
     42 
     43 void Build(int l,int r,int rt)
     44 {
     45     if(l == r)
     46     {
     47         mn[rt] = a[l];
     48         return ;
     49     }
     50     int mid = (l+r)>>1;
     51     Build(l,mid,rt<<1);
     52     Build(mid+1,r,rt<<1|1);
     53     PushUp(rt);
     54 }
     55 
     56 void Add(int L,int C,int l,int r,int rt)
     57 {
     58     if(l == r)
     59     {
     60         mn[rt] += C;
     61         return ;
     62     }
     63     int mid = (l+r) >> 1;
     64     if(L <= mid)
     65         Add(L,C,l,mid,rt<<1);
     66     else 
     67         Add(L,C,mid+1,r,rt<<1|1);
     68     PushUp(rt);
     69 }
     70 
     71 int Query(int L,int R,int l,int r,int rt)
     72 {
     73     if(L <= l && r <= R)
     74     {
     75         return mn[rt];
     76     }
     77     int mid = (l+r)>>1;
     78     int MIN = INF;
     79     if(L <= mid)
     80     {
     81         MIN = min(Query(L,R,l,mid,rt<<1),MIN);
     82     }
     83     if(R > mid)
     84     {
     85         MIN = min(Query(L,R,mid+1,r,rt<<1|1),MIN);
     86     }
     87     return MIN;
     88 }
     89 
     90 int Find()
     91 {
     92     int l = 1,r = n;
     93     while(l < r)
     94     {
     95         int mid = (l+r)/2;
     96         int tmp = Query(l,mid,1,n,1);
     97         if(tmp <= haved)
     98         {
     99             r = mid;
    100         }
    101         else 
    102         {
    103             l = mid+1;
    104         }
    105     }
    106     return l;
    107 }
    108 
    109 int main()
    110 {
    111 #ifdef ONLINE_JUDGE 
    112 #else 
    113         freopen("in.txt","r",stdin);
    114     // freopen("out.txt","w",stdout); 
    115 #endif
    116     scanf("%d %d",&n,&m);
    117     rep(i,1,n)
    118     {
    119         scanf("%d",&a[i]);
    120     }
    121     Build(1,n,1);
    122     haved = m;
    123 
    124     int time = 0,ca = 1;
    125     // time 换完的房间数量
    126     // ca 时间计数(月数)
    127     scanf("%d",&q);
    128     while(Query(1,n,1,n,1) < inf)
    129     {
    130         int t = Find();
    131         int tmp = Query(t,t,1,n,1);
    132         while(tmp <= haved)
    133         {
    134             haved -= tmp;
    135             // haved : 当前手中拥有的节能灯数量
    136             time++;
    137             Add(t,inf,1,n,1);
    138             t = Find();
    139             tmp = Query(t,t,1,n,1);
    140             if(haved == 0)
    141                 break;
    142         }
    143 
    144         have[ca] = time;
    145         now[ca++] = haved;
    146         haved += m;
    147         if(ca > 100000)
    148             break;
    149     }
    150     for(ll i = ca;i <= 100000;i++)
    151     {
    152         have[i] = have[ca-1];
    153         now[i] = now[ca-1];
    154     }
    155     rep(i,1,q)
    156     {
    157         scanf("%d",&que);
    158         printf("%d %d
    ",have[que],now[que]);
    159     }
    160     fclose(stdin);
    161     // fclose(stdout);
    162     return 0;
    163 }
  • 相关阅读:
    Oracle 删除重复数据的几种方法
    12.25模拟赛T3
    java实现第五届蓝桥杯圆周率
    java实现第五届蓝桥杯武功秘籍
    Oracle 审计初步使用
    [CERC2017]Intrinsic Interval——扫描线+转化思想+线段树
    ORA-12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN> in 12.2.0 Database
    12.25模拟赛T2
    java实现第五届蓝桥杯写日志
    java实现第五届蓝桥杯李白打酒
  • 原文地址:https://www.cnblogs.com/duny31030/p/14304882.html
Copyright © 2011-2022 走看看