首页 > 其他分享 >D. Attribute Checks

D. Attribute Checks

时间:2024-10-15 10:35:14浏览次数:7  
标签:strength intel nums int Attribute IOS Checks dp

链接:https://codeforces.com/contest/2025/problem/D
题目:

思路:
动态规划。dp[i]记录当前0分配了i个给智力所能达到的最大分数。利用strength[N],intel[N]表示小于等于i的个数,所以加上前缀和赋值给dp[i],然后清空两个数组,方便这个零到下个零的这段。
代码:

#include<iostream>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int N = 2e6 + 10;

int strength[N], intel[N];
int nums[N],dp[N];
int n, m;
signed main()
{
	IOS;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> nums[i];
	}
	int dfn = 0;
	for (int i = 1; i <= n; i++)
	{
		if (nums[i] > 0)
		{
			intel[nums[i]]++;
		}
		else if (nums[i] < 0)
		{
			strength[-nums[i]]++;
		}
		if(nums[i] == 0 or i == n)
		{
			for (int j = 1; j <= dfn; j++)
			{
				strength[j] += strength[j - 1];
				intel[j] += intel[j - 1];
			}
			for (int j = 0; j <= dfn; j++)
			{
				dp[j] += intel[j] + strength[dfn - j];
			}
			dfn++;
			for (int j = dfn; j > 0; j--)
			{
				dp[j] = max(dp[j], dp[j - 1]);
			}
			for (int j = 1; j <= dfn; j++)
			{
				intel[j] = strength[j] = 0;
			}

		}
		
	}
	int ans = 0;
	for (int i = 1; i <= dfn; i++)ans = max(dp[i], ans);
	cout << ans;
	return 0;
}

标签:strength,intel,nums,int,Attribute,IOS,Checks,dp
From: https://www.cnblogs.com/zzzsacmblog/p/18466933

相关文章