Codeforces 1498D Bananas in a Microwave 题解
作者:互联网
\(\mathcal{Solution}\)
设 \(f_i\) 为到达 \(i\) 的答案,不能到达则为 \(inf\)。
设 \(g_i\) 为考虑完前面的操作时,单独使用当前操作来到达 \(i\) 的最小步数,不能到达则为 \(inf\)。
每次读进一个操作就把 \(g\) dp 一次,然后更新 \(f\)。
具体的:
-
初始化 \(g_i=\left\{\begin{matrix}0,f_i\neq inf\\inf,f_i=inf \end{matrix}\right.\)
-
若 \(g_i\neq y\),则 \(g_{\left \lceil i+x \right \rceil }=\min(g_{\left \lceil i+x \right \rceil },g_i+1)\) 或 \(g_{\left \lceil i*x \right \rceil }=\min(g_{\left \lceil i*x \right \rceil },g_i+1)\)。
-
根据 \(g\) 更新 \(f\),若 \(g_i\neq 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;
}
标签:ch,Bananas,int,题解,Codeforces,while,inline,inf,define 来源: https://www.cnblogs.com/do-while-true/p/14622348.html