Codeforces 1221 div2 D. Make The Fence Great Again (dp)
纪念下第一次在训练的时候自己独立做出来了1800 的dp题,等巩固之后冲一下1900
自闭了一天,也就这题有点搞头
首先题意是给出n个数,要求每个数字都不能和前面的数字相同,每次操作都只能在当前位上+1。其实这个无后效性很明显,我们只需要做好当前位,然后后面从前面转移过来就好
然后还有个问题是不能相等,前面操作了后面可能会变,那就把操作的结果最小值继承过来,转移仅发生在加完之后两者不相等的情况下。另外,我们通过观察发现一个位置上的最多+10次,刚好可以开成状态,那么就有:
#include <bits/stdc++.h> using namespace std; #define limit (3000000 + 5)//防止溢出 #define INF 0x3f3f3f3f #define inf 0x3f3f3f3f3f #define lowbit(i) i&(-i)//一步两步 #define EPS 1e-9 #define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.tie(0); #define ff(a) printf("%d\n",a ); #define pi(a, b) pair<a,b> #define rep(i, a, b) for(ll i = a; i <= b ; ++i) #define per(i, a, b) for(ll i = b ; i >= a ; --i) #define MOD 998244353 #define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next) #define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin) #define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout) typedef long long ll; typedef unsigned long long ull; char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf; inline ll read() { #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) ll sign = 1, x = 0; char s = getchar(); while (s > '9' || s < '0') { if (s == '-')sign = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = (x << 3) + (x << 1) + s - '0'; s = getchar(); } return x * sign; #undef getchar }//快读 void print(ll x) { if (x / 10) print(x / 10); *O++ = x % 10 + '0'; } void write(ll x, char c = 't') { if (x < 0)putchar('-'), x = -x; print(x); if (!isalpha(c))*O++ = c; fwrite(obuf, O - obuf, 1, stdout); O = obuf; } const ll mod = 1e9 + 7; ll quickPow(ll base, ll expo) { ll ans = 1; while (expo) { if (expo & 1)(ans *= base) %= mod; expo >>= 1; base = base * base; base %= mod; } return ans % mod; } ll C(ll n, ll m) { if (n < m)return 0; ll x = 1, y = 1; if (m > n - m)m = n - m; rep(i, 0, m - 1) { x = x * (n - i) % mod; y = y * (i + 1) % mod; } return x * quickPow(y, mod - 2) % mod; } ll lucas(ll n, ll m) { return !m || n == m ? 1 : C(n % mod, m % mod) * lucas(n / mod, m / mod) % mod; } int kase; int n, m, k; int a[limit]; int dp[limit][13]; int b[limit]; void solve() { cin>>n; rep(i,1,n) { cin >> a[i] >> b[i]; } rep(i,1,n){ rep(j,0,10){ dp[i][j] = numeric_limits<ll>::max(); } } // 0 不变,1加1,2减一 rep(i,0,10){ dp[1][i] = i * b[1]; } rep(i,2,n){ rep(j,0,10){//代表从i为j个从i - 1位h个转移过来 rep(h, 0, 10){ // if(j - h < 0)break; if(a[i] + j != a[i - 1] + h){ dp[i][j] = min(dp[i - 1][h] + j * b[i], 1ll * dp[i][j]); } } } } cout<<*min_element(dp[n], dp[n] + 1 + 10)<<endl; } int32_t main() { #ifdef LOCAL FOPEN; // FOUT; #endif FASTIO cin >> kase; while (kase--) solve(); cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; }
天才选手zerol的主页:https://zerol.me/
|
WeepingDemon的个人主页:https://weepingdemon.gitee.io/blog/