• Codeforces 1498D Bananas in a Microwave 题解


    (mathcal{Solution})

    (f_i) 为到达 (i) 的答案,不能到达则为 (inf)

    (g_i) 为考虑完前面的操作时,单独使用当前操作来到达 (i) 的最小步数,不能到达则为 (inf)

    每次读进一个操作就把 (g) dp 一次,然后更新 (f)

    具体的:

    1. 初始化 (g_i=left{egin{matrix}0,f_i eq inf\inf,f_i=inf end{matrix} ight.)

    2. (g_i eq y),则 (g_{left lceil i+x ight ceil }=min(g_{left lceil i+x ight ceil },g_i+1))(g_{left lceil i*x ight ceil }=min(g_{left lceil i*x ight ceil },g_i+1))

    3. 根据 (g) 更新 (f),若 (g_i eq inf),则对 (f_i) 更新。

    时间复杂度 (mathcal{O}(nm))

    (mathcal{Code})

    //Code by do_while_true
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    namespace do_while_true {
    	#define ld double
    	#define ll long long
    	#define re register
    	#define pb push_back
    	#define fir first
    	#define sec second
    	#define pp std::pair
    	#define mp std::make_pair
    	const ll mod = 1e9 + 7;
    	template <typename T>
    	inline T Max(T x, T y) { return x > y ? x : y; }
    	template <typename T>
    	inline T Min(T x, T y) { return x < y ? x : y; }
    	template <typename T>
    	inline T Abs(T x) {	return x < 0 ? -x : x; }
    	template <typename T>
    	inline T& read(T& r) {
    		r = 0; bool w = 0; char ch = getchar();
    		while(ch < '0' || ch > '9') w = ch == '-' ? 1 : 0, ch = getchar();
    		while(ch >= '0' && ch <= '9') r = r * 10 + (ch ^ 48), ch = getchar();
    		return r = w ? -r : r;
    	}
    	template <typename T>
    	inline T qpow(T x, T y) {
    		re T sumq = 1; x %= mod;
    		while(y) {
    			if(y&1) sumq = sumq * x % mod;
    			x = x * x % mod;
    			y >>= 1;
    		}
    		return sumq;
    	}
    	char outch[110];
    	int outct;
    	template <typename T>
    	inline void print(T x) {
    		do {
    			outch[++outct] = x % 10 + '0';
    			x /= 10;
    		} while(x);
    		while(outct >= 1) putchar(outch[outct--]);
    	}
    }
    using namespace do_while_true;
    
    const int N = 1e5 + 10;
    const int INF = 0x3ffffff;
    
    int n, m;
    int f[N], g[N];
    
    void work(int q) {
    	int t, y; ll x;
    	for(int i = 1; i <= m; ++i) g[i] = INF;
    	read(t); read(x); read(y);
    	if(t == 1) {
    		x = x / 100000 + (x % 100000 > 0);
    		g[x] = 1;
    		for(int i = 1; i <= m; ++i)
    			if(f[i] != INF) g[i] = 0;
    		for(int i = 1; i <= m; ++i)
    			if(g[i] != y && g[i] != INF && i+x <= m)
    				g[i+x] = Min(g[i+x], g[i]+1);
    		for(int i = 1; i <= m; ++i)
    			if(g[i] <= y && f[i] == INF)
    				f[i] = q;
    	}
    	else {
    		for(int i = 1; i <= m; ++i)
    			if(f[i] != INF) g[i] = 0;
    		for(int i = 1; i <= m; ++i) {
    			int p = (int)std::ceil(1.0 * i * x / 100000);
    			if(g[i] != y && g[i] != INF && p <= m)
    				g[p] = Min(g[p], g[i]+1);
    		}
    		for(int i = 1; i <= m; ++i)
    			if(g[i] <= y && f[i] == INF)
    				f[i] = q;
    	}
    }
    
    signed main() {
    	read(n); read(m);
    	for(int i = 1; i <= m; ++i) f[i] = INF;
    	for(int i = 1; i <= n; ++i)
    		work(i);
    	for(int i = 1; i <= m; ++i) printf("%d ", f[i] == INF ? -1 : f[i]);
    	return 0;
    }
    
  • 相关阅读:
    BZOJ4503 两个串
    【挖坟】HDU3205 Factorization
    webpack打包 The 'mode' option has not been set, webpack will fallback to
    echarts js报错 Cannot read property 'getAttribute' of null
    微信支付 get_brand_wcpay_request fail,Undefined variable: openid
    layui动态设置checbox选中状态
    layui 获取radio单选框选中的值
    js 获取数组最后一个元素
    js生成指定范围内的随机数
    layer重复弹出(layui弹层同时存在多个)的解决方法
  • 原文地址:https://www.cnblogs.com/do-while-true/p/14622348.html
Copyright © 2020-2023  润新知