栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C. Mortal Kombat Tower

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
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 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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/511689.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号