第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)(热身赛) C-GPA

博客围绕一个成绩修改问题展开,给定长度为n的a、b数组,若当天成绩小于前i - 1天平均成绩,Alice会伤心,Bob可将b数组值换入a数组。采用动态规划(dp)思路,计算前i天有j天伤心所需最小分数,根据情况更新dp值以求解Alice最少伤心天数。

题目

In this term, Alice took nn courses. Now, she has finished all final exams, and she will get her grades in the following nn days.

On the i-th day, Alice will know her grade of the i-th course, denoted as AiA_iAiis strictly less than the average grade of the first i−1i-1i1 courses, Alice will be sad on that day.

Now Bob hacks into the school’s database. Bob can choose a set S of courses (S can be empty), and then for each course ii in S, change Alice’s grade from AiA_iAi to BiB_iBiBob wants to minimize the number of days that Alice will be sad. Now you need to help him to decide which courses’ grades he should modify.

Note: Alice is always happy on the first day.

在这里插入图片描述

题意

给定长度为n的a数组和b数组,分别是Alice获得的成绩和Bob可以换的成绩,如果当前这天的成绩严格小于前i-1天的平均成绩,则Alice会伤心。Bob可以把b数组的值换到a数组中,问Alice最少可以伤心多少天。

思路

考虑dp,计算出前i天有j天伤心需要的最小分数,如果当前选a可以让Alice不伤心的话就更新dp[i][j]最小值(前i天伤心的天数没有增加),否则更新dp[i][j+1]的最小值(前i天伤心的天数增加1) ,最后从1-n如果有可实现的方案直接输出即可。
注意 由于:
sum[i-1]/i-1 < a[i]
所以可以变形为:
sum[i-1]<(i-1)*a[i]
详情见代码

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn = 4000+100;

int a[maxn],b[maxn];
int dp[maxn][maxn]; // 前i天有j天伤心的最小成绩和 
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i] >> b[i];
	memset(dp,0x3f,sizeof dp);
	
	dp[1][0] = min(a[1],b[1]); // 前1天有0天伤心的最小需要的成绩 
	for(int i=2;i<=n;i++)
	{
		for(int j=0;j<i;j++)
		{
			if(dp[i-1][j] <= (i-1) * a[i])//如果今天选择a可以不伤心 
			dp[i][j] = min(dp[i][j],dp[i-1][j] + a[i]); // 就不加伤心的天数,并且更新答案
			else // 如果今天选择a不能不伤心
			dp[i][j+1] = min(dp[i][j+1],dp[i-1][j] + a[i]); //就加一天伤心天数,并且更新答案 
			
			if(dp[i-1][j] <= (i-1) * b[i]) // 和a同理 
			dp[i][j] = min(dp[i][j],dp[i-1][j] + b[i]);
			else 
			dp[i][j+1] = min(dp[i][j+1],dp[i-1][j] + b[i]);
		}
	}
	
	for(int i=1;i<=n;i++)
	{
		if(dp[n][i] != 0x3f3f3f3f) return cout<<i,0;
	}
	cout<<n;
	return 0;
}
/*
sum[i-1]/i-1 <= a[i] 
变形: 
sum[i-1] <= (i-1)*a[i]
*/
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值