ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps(线段树基础)

这篇博客介绍了在ACM-ICPC 2018南京赛区网络预赛中的一道题目,涉及龙族公主与Lpl之间的故事。Lpl在城堡中进行节能灯更换工作,每月购买一定数量的节能灯,按照房间列表依次更换。博客详细阐述了更换规则和计算每个月底有多少房间完成更换以及剩余的节能灯数量的方法。
摘要由CSDN通过智能技术生成

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 mmm 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.

Input

Each input will consist of a single test case.

The first line contains integers nnn and m(1≤n≤100000,1≤m≤100)m (1 \le n \le 100000, 1 \le m \le 100)m(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 nnn integers k1,k2,...,knk_1, k_2, ..., k_nk1​,k2​,...,kn​
(1≤kj≤10000,j=1,2,...,n)(1 \le k_j \le 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 jjj is the number of lamps in jjj-th room. Room numbers are given in accordance with Lpl's list.

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

The fourth line contains qqq integers d1,d2,...,dqd_1, d_2, ..., d_qd1​,d2​,...,dq​
(1≤dp≤100000,p=1,2,...,q)(1 \le d_p \le 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 111; at the beginning of the first month Lpl buys the first m energy-saving lamps.

Output

Print qqq lines.

Line ppp 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 dpd_pdp​ month.

Hint

Explanation for the sample:

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

样例输入复制

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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

代码高亮 Sublime Vim Emacs

环境配色亮色配色暗色配色 暗色配色

返回

  • 亮色配色
  • 暗色配色

代码缩进248 4

返回

  • 2
  • 4
  • 8

C 语言C++ 语言 (C++11)Java 语言 C++ 语言 (C++11)

返回

  • C 语言
  • C++ 语言 (C++11)
  • Java 语言
  • main.cpp

通用

逻辑

循环

数学

文本

列表

颜色

变量

函数

 

 

79

ll powmod(ll x,ll y){ll t;for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod;return t;}

 

59

    while(scanf("%d%d",&n,&m)!=EOF)

60

    {

61

        build(1,n,1);///顺便写入数组

62

        int tv=0,cnt=0;///tm表示最后一次修改的位置

63

        for(int i=1;i<maxn;i++)

64

        {

65

            tv+=m;

66

            while(~query(1,n,1,tv))  cnt++;

67

            ans1[i]=cnt,ans2[i]=tv;///已经换的次数,剩余的价值

68

            ///cout<<ans1[i]<<" "<<ans2[i]<<endl;

69

        }

70

        scanf("%d",&q);

71

        for(int i=0;i<q;i++)

72

        {

73

            scanf("%d",&x);

74

            printf("%d %d\n",ans1[x],ans2[x]);

75

        }

76

    }

77

    return 0;

78

}

79

关闭终端 终端 - 计蒜客

已通过 0 组测试数据,共 5 组

只看题面

#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)

#define root l,r,rt
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long

const int  maxn =1e5+5;
const int mod=1e9+7;
const int UB=maxn*1000;

ll gcd(ll x,ll y) { return y==0?x:gcd(y,x%y); }
ll powmod(ll x,ll y){ll t;for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod;return t;}
/*
题目大意:换灯泡,每个月都会增加m个价值,
然后每个房间都有对应的价值,必须按序扫,如果手中的价值大于等于房间的价值则必须换。
询问给定月数末尾总共换了多少次,手中剩余多少价值。

线段树操作秀一下就行。
查询就查询序列中第一个小于等于给定值的位置,二分一下,
如果左边区间的最小值符合要求则查询左边,否则查询右边,
边界时,判定一下是否符合题意,然后更新就行,否则返回-1表示不可行。

线段树的基础题。
顺便想提下线段树的数据对应关系,线段树的数据源应该是由树的
叶节点存储,然后节点价值是由rt索引,minv[rt]存储的,是子树中数据源的函数关系。
该函数满足区间性质。
*/

int n,m;
///线段树
int minv[maxn<<2];
void pushup(lrt) { minv[rt]=min(minv[rt<<1],minv[rt<<1|1]); }
void build(lrt)
{
    minv[rt]=UB;///最大值
    if(l==r) { scanf("%d",&minv[rt]); return ; }
    int mid=l+r>>1;
    build(lson),build(rson),pushup(root);
}
int query(lrt,int& v)///整体查询第一个小于等于v的位置
{
    if(l==r)
    {
        if(v<minv[rt]) return -1;
        else
        {
            v-=minv[rt];
            minv[rt]=UB;///这个点就不会再被选中了
            return rt;
        }
    }
    int mid=l+r>>1,ret;
    if(minv[rt<<1]<=v) ret=query(lson,v);
    else ret=query(rson,v);
    pushup(root);///因为查询时参杂着修改,所以每次都要刷新
    return ret;
}

///
int x,q;
int ans1[maxn],ans2[maxn];

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);///顺便写入数组
        int tv=0,cnt=0;///tm表示最后一次修改的位置
        for(int i=1;i<maxn;i++)
        {
            tv+=m;
            while(~query(1,n,1,tv))  cnt++;
            ans1[i]=cnt,ans2[i]=tv;///已经换的次数,剩余的价值
            ///cout<<ans1[i]<<" "<<ans2[i]<<endl;
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            scanf("%d",&x);
            printf("%d %d\n",ans1[x],ans2[x]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值