您的位置:首页 > 其它

杭电5742 之 It's All In The Mind

2016-07-22 15:28 337 查看
[align=left]Problem Description[/align]
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},
0≤ai≤100.

2. The sequence is non-increasing, i.e. a1≥a2≥...≥an.

3. The sum of all elements in the sequence is not zero.

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

[align=left]Input[/align]
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
(2≤n≤100,0≤m≤n)
-- the length of the sequence and the number of known elements.

In the next m
lines, each contains two integers xi
and yi
(1≤xi≤n,0≤yi≤100,xi<xi+1,yi≥yi+1),
indicating that axi=yi.
 

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

[align=left]Sample Input[/align]

2
2 0
3 1
3 1

 

[align=left]Sample Output[/align]

1/1
200/201
分析:令x=a_1+a_2,y=a_3+a_4+\cdots+a_nx=a​1​​+a​2​​,y=a​3​​+a​4​​+⋯+a​n​​, 那么\frac{a_1+a_2}{a_1+a_2+\cdots+a_n}=\frac{x}{x+y}=1-\frac{y}{x+y}​a​1​​+a​2​​+⋯+a​n​​​​a​1​​+a​2​​​​=​x+y​​x​​=1−​x+y​​y​​. 对于定值yy, 显然xx 越大越好, 对于定值xx, 显然yy越小越好. 于是按照a_1a​1​​和a_2a​2​​尽量大, 其他元素尽量小的策略填数就好了.注意该数列为递减数列,所以要控制各个元素的值,因此引入 赋值区间(例如,输入测试样例 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;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: