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

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

中文题目

在喝茶的过程中,公主,除其他外,问为什么这样一个善良可爱的龙在城堡里被监禁Lpl?龙神秘地笑了笑,回答说这是个大秘密。暂停后,龙补充道:

- 我们有合同。租赁协议。他总是一整天都在工作。他喜欢沉默。除此之外,住在城堡还有更多的优势。比如说,很容易证明未接来电的合理性:电话铃声无法从手机离开的城堡的另一侧到达。因此,监禁只是一个故事。实际上,他思考一切。他很聪明。例如,他开始用整个城堡中的节能灯替换白炽灯......

Lpl选择了一种节能灯模型并开始更换,如下所述。他为城堡中的所有房间编号并计算每个房间需要更换多少盏灯。

在每个月初,Lpl购买m个节能灯替换房间里的灯泡根据他的名单。他从名单上的第一个房间开始。如果这个房间的灯还没有更换,Lpl有足够的节能灯来更换所有的灯,那么他将取代所有的灯并从列表中取出房间。否则,他只是跳过它并检查他列表中的下一个房间。这个过程一直重复,直到他没有节能灯或他已经检查了他的清单中的所有房间。如果他检查了清单上的所有房间后仍然有一些节能灯,那么他将在下个月保存其余的节能灯。

一旦完成所有工作,他就会停止购买新灯具。它们质量非常高,周期长。

您的任务是在给定的月份和房间描述中计算旧灯具将用节能灯替换的房间数量以及每个月底将保留多少节能灯。

输入

每个输入都包含一个测试用例。

第一行包含整数n和m

-房间在城堡的数量和节能灯的数量,这LPL购买每月。

第二行包含n整数k1,k2,...,kn

- 城堡房间的灯数。位置数量?j是灯的数量?第j个房间。房间号码根据Lpl的清单给出。

第三行包含一个整数 q(1<=q<=100000)-的查询的数量。

第四行包含q整数d1,d2,...,dq

- 形成查询的月数。

月份以编号开头1;在第一个月初,Lpl购买了第一批m个节能灯。

产量

打印q行。

线p包含两个整数 - 房间数量,其中所有旧灯泡已经更换,剩下的节能灯数量到最后dp月。

暗示

样本说明:

在第一个月,他买了4个节能灯他取代了他列表中的第一个房间并将其移除。然后他有1个节能灯,然后跳过所有房间。所以,第一个月的答案是1,1 ------ 1个房间的灯已经更换,11个节能灯仍然存在。

样例输入复制

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

 解题思路:从左到右每次查找小于等于剩余灯泡数目的房间,然后把该房间的数目改成无穷大,即表示成不可更换灯泡了

实现的话,当然是用线段树了,感觉好像就差不多就一个操作,就是查询操作,每次查询到结果就把该房间的灯泡数改成无穷大就OK啦。

需要注意的是,在同一天,可能会更换多个房间的灯泡,这时候就需要用个while语句来处理了。还有一点就是当全部房间都被更换完毕后,就不会每天有灯泡了,也就是说答案就不变了,这点也是需要注意的。

开始模板都敲错了,竟然找错误找了老半天,太菜了。。。

附上代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 100005
#define inf 0x3f3f3f3f
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pushup() tree[root]=min(tree[root<<1],tree[root<<1|1])
int tree[maxn<<2];
int n,m,q,day[maxn],cnt;
struct node{
    int room;  //房间数
    int num;   //剩余的灯泡数
}ans[maxn];

void build(int l,int r,int root)
{
    if(l==r)
    {
        scanf("%d",&tree[root]);
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup();
}
/*
void update(int pos,int val,int l,int r,int root)
{
    if(l==r)
    {
        tree[root]=val;
        return;
    }
    int mid=l+r>>1;
    if(pos<=mid)
        update(pos,val,lson);
    else
        update(pos,val,rson);
    pushup();
}
*/
int query(int val,int l,int r,int root)
{
    if(l==r)
    {
        int z=tree[root];
        tree[root]=inf;
        return z;
    }
    int mid=l+r>>1;
    int x=0;
    if(val>=tree[root])  //判断是否有小于等于灯泡数的房间
    {
        if(tree[root<<1]<=val)  //是否可以去靠左边的房间
            x=query(val,lson);
        else  //左边去不了就去右边的
            x=query(val,rson);
    }
    pushup();
    return x;
}

int main()
{
    scanf("%d%d",&n,&m);
    build(1,n,1);
    scanf("%d",&q);
    cnt=0;
    for(int i=1;i<=q;i++)
    {
        scanf("%d",&day[i]);
        if(ans[i-1].room!=n)   //当所有的房间的灯泡都被换了后,就不用加了
            cnt+=m;
        ans[i].room=ans[i-1].room;
        int k=query(cnt,1,n,1);
        while(k!=0)
        {
            cnt-=k;
            ans[i].room++;
            k=query(cnt,1,n,1);
        }
        ans[i].num=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d %d\n",ans[day[i]].room,ans[day[i]].num);
    return 0;
}

原文地址:https://www.cnblogs.com/zjl192628928/p/9684044.html

时间: 2024-07-29 04:25:43

ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(线段树)的相关文章

ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)

https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了..导致不断超时,我太弱了.每次询问符合要求的最左边的点. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #i

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

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

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

目录 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 题意 思路 ACM-ICPC 2018 南京赛区网络预赛 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

ACM-ICPC 2018 南京赛区网络预赛 J.Sum

Sum A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integers could be decomposed into

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze

262144K There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici?. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances

计蒜客 ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem-数学公式题

A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him.

ACM-ICPC 2018 南京赛区网络预赛 做题记录

比赛的时候被J题卡常然后B题自闭最后G题没调完 5题gg 现在补题进度:6/12 A. 题解: 高考数学题,裂项完之后答案就是n!-1 (n!)%n=0,所以就是n-1 1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 int T; 5 ll n; 6 int main() 7 { 8 cin>>T; 9 while(T--) 10 { 11 cin>>n; 12 cout

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

A An Olympian Math Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; ll T,n; int main(){ scanf("%lld",&T); while(T--){ scanf("%lld",&n); printf("%lld\n",n-1); } } B The writing on the w

【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies

G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1...N) and each time a child is inv