C. Mortal Kombat Tower

Educational Codeforces Round 95 (Rated for Div. 2)
date:2021/11/15

题目大意:

在这里插入图片描述

也就是说:
两个人A,B从左到右轮流取数(A先取),每次每个人可以选择取1或 2个,问A 最少取到1的个数;

思路:

一眼dp不会写qwq

怎么个dp思路?
状态方程 f [ i ] [ 2 ] f[i][2] f[i][2] i 表示取到第几个数,0 表示 A取,1表示 B取
因为这题是问A取得最小值,并不考虑B的情况,那么用f[][1] 来去存储 0的值;
转移方程:

		f[i][0] = min(f[i-1][1]+a[i],f[i-2][1]+a[i]+a[i-1]);
		// 1 只用来记录0的状态以方便0去转移;
		//没有想到这一丶 qwq
		f[i][1] = min(f[i-1][0],f[i-2][0]);

Code

// Problem: C. Mortal Kombat Tower
// Contest: Codeforces - Educational Codeforces Round 95 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1418/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define _orz ios::sync_with_stdio(false),cin.tie(0)
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a;i <= b;i++)
#define forn(i,n) for(int i = 0; i < n; i++)
#define all(a) (a.begin(),a.end())
#define dbg() cout << "0k!" << endl;
//#define _DEBUG
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 2e5+10;
const ll MOD = 1e9+7;

int f[N][2]; // 0 rr / 1 lalasao
void so1ve(){
    
    
	int n; cin >> n;
	vector<int> a(n+1);
	forr(i,1,n) cin >> a[i];
	
	f[1][0] = f[1][1]= a[1];
	f[2][0] = a[1] + a[2];
	f[2][1] = f[1][0];
	
	forr(i,3,n){
    
    
		f[i][0] = min(f[i-1][1]+a[i],f[i-2][1]+a[i]+a[i-1]);
		f[i][1] = min(f[i-1][0],f[i-2][0]);
	}
	cout << min(f[n][0],f[n][1]) << endl;

}
int main()
{
    
    
#ifdef _DEBUG
    //freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int t; cin >> t;
    while(t--) so1ve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51687628/article/details/121329654
今日推荐