杭电5742 之 It's All In The Mind

本文探讨了一个关于序列填充的问题,目标是找到一种策略,在已知部分元素的情况下,使特定分数最大化。通过分析序列特性,文章提出了一种有效的方法来解决这一问题,并给出了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
Professor Zhang has a number sequence a1,a2,...,an. However, the sequence is not complete and some elements are missing. Fortunately, Professor Zhang remembers some properties of the sequence:

1. For every i{1,2,...,n}, 0ai100.
2. The sequence is non-increasing, i.e. a1a2...an.
3. The sum of all elements in the sequence is not zero.

Professor Zhang wants to know the maximum value of a1+a2ni=1ai among all the possible sequences.
 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains two integers n and m (2n100,0mn) -- the length of the sequence and the number of known elements.

In the next m lines, each contains two integers xi and yi (1xin,0yi100,xi<xi+1,yiyi+1), indicating that axi=yi.
 

Output
For each test case, output the answer as an irreducible fraction "p/q", where p, q are integers, q>0.
 

Sample Input
2 2 0 3 1 3 1
 

Sample Output
1/1 200/201
分析:x=a_1+a_2,y=a_3+a_4+\cdots+a_nx=a1+a2,y=a3+a4++an, 那么\frac{a_1+a_2}{a_1+a_2+\cdots+a_n}=\frac{x}{x+y}=1-\frac{y}{x+y}a1+a2++ana1+a2=x+yx=1x+yy. 对于定值yy, 显然x越大越好, 对于定值xx, 显然yy越小越好. 于是按照a_1a1a_2a2尽量大, 其他元素尽量小的策略填数就好了.
注意该数列为递减数列,所以要控制各个元素的值,因此引入 赋值区间(例如,输入测试样例 1 6 3 1 1 3 3 6 1,给a2,a4,a5赋值,a2=1,a4=1,a5=1,此处赋值区间为[1,1],[4,5]);
AC代码如下:
<pre name="code" class="cpp">#include "iostream"
using namespace std;

int gcd(int a, int b);

int main(int argc, char* argv[])
{
	int t,i,j;
	int n,m;
	int son,mon;    //分子,分母
	int flag,ff,L;  //L为赋值区间的左界
	int a[110],b[110],c[110];
	cin>>t;
	while(t--)
	{
		flag=0;//标记a1是否为定值
		ff=0;  //标记a2是否为定值
		L=3;//赋值区间左界初始值为3,区间【1,2】的元素可直接确定
		son=mon=0;
		cin>>n>>m;
		for(i=1;i<=m;i++)
		{
			cin>>c[i]>>b[i];
			if (c[1]==1)
			{
				flag=1;//a1为定值b[1]
				a[1]=b[1];
			}
			if (c[i]==2)
			{
				ff=1;//a2为定值b[i]
				a[2]=b[i];
			}
			if (c[i]>2)
			{
				mon+=b[i];
				for(j=L;j<c[i];j++)//对赋值区间的元素赋值
				{
					a[j]=b[i];
					mon+=a[j];
				}
				L=c[i]+1;
			}
		}
		if (flag==0)	a[1]=100;//直接确定a1,a2
		if (ff==0)      a[2]=a[1];
		son+=a[1]+a[2];
		mon+=a[1]+a[2];
		int s=gcd(son,mon);//gcd函数求两个数的最大公因数
		if (s==0)   //约分
		{
			cout<<son<<"/"<<mon<<endl;
		}
		else
		{
			cout<<son/s<<"/"<<mon/s<<endl;
		}
	}
	return 0;
}

int gcd(int a, int b)
{
	if (a<b)
	{
		int temp=a;
		a=b;
		b=temp;
	}
	if (b==0)
	{
		return b;
	}
	int sum=a%b;
	while(sum!=0)
	{
		a=b;
		b=sum;
		sum=a%b;
	}
	return b;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值