CF1498D Bananas in a Microwave

Description

You have a malfunctioning microwave in which you want to put some bananas. You have $ n $ time-steps before the microwave stops working completely. At each time-step, it displays a new operation.

Let $ k $ be the number of bananas in the microwave currently. Initially, $ k = 0 $ . In the $ i $ -th operation, you are given three parameters $ t_i $ , $ x_i $ , $ y_i $ in the input. Based on the value of $ t_i $ , you must do one of the following:

Type 1: ( $ t_i=1 $ , $ x_i $ , $ y_i $ ) — pick an $ a_i $ , such that $ 0 \le a_i \le y_i $ , and perform the following update $ a_i $ times: $ k:=\lceil (k + x_i) \rceil $ .

Type 2: ( $ t_i=2 $ , $ x_i $ , $ y_i $ ) — pick an $ a_i $ , such that $ 0 \le a_i \le y_i $ , and perform the following update $ a_i $ times: $ k:=\lceil (k \cdot x_i) \rceil $ .

Note that $ x_i $ can be a fractional value. See input format for more details. Also, $ \lceil x \rceil $ is the smallest integer $ \ge x $ .

At the $ i $ -th time-step, you must apply the $ i $ -th operation exactly once.

For each $ j $ such that $ 1 \le j \le m $ , output the earliest time-step at which you can create exactly $ j $ bananas. If you cannot create exactly $ j $ bananas, output $ -1 $ .

Solution

$O(nm^2)$的做法是对于每次操作,枚举每个数,更新它能更新的所有数

优化枚举的过程,如果枚举到已经访问过的数就可以停下了,因为之后还会再从它开始寻找,时间复杂度$O(mn)$

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int ans[100005];
long long n,m;
bool vst[100005];
queue<long long>q;
inline long long read(){
    long long f=1,w=0;
    char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')w=(w<<1)+(w<<3)+ch-'0',ch=getchar();
    return f*w;
}
int main(){
    n=read(),m=read(),memset(ans,-1,sizeof(ans)),vst[0]=true;
    for(int i=1;i<=n;i++){
        long long t=read(),x=read(),y=read();
        for(int j=0;j<=m;j++)if(vst[j]){
            long long temp=j;
            for(int k=1;k<=y;k++){
                if(t==1)temp+=(x+99999ll)/100000ll;
                else temp=(temp*x+99999ll)/100000ll;
                if(temp>m||vst[temp])break;
                q.push(temp),ans[temp]=i;
            }
        }
        while(q.size())vst[q.front()]=true,q.pop();
    }
    for(int i=1;i<=m;i++)printf("%d ",ans[i]);
    return 0;
}
Bananas in a Microwave

 

posted @ 2021-03-31 20:56  QDK_Storm  阅读(74)  评论(0编辑  收藏  举报