杭电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=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=1−x+yy. 对于定值yy, 显然xx 越大越好, 对于定值xx, 显然yy越小越好. 于是按照a_1a1和a_2a2尽量大, 其他元素尽量小的策略填数就好了.注意该数列为递减数列,所以要控制各个元素的值,因此引入 赋值区间(例如,输入测试样例 1 6 3 1 1 3 3 6 1,给a2,a4,a5赋值,a2=1,a4=1,a5=1,此处赋值区间为[1,1],[4,5]);
AC代码如下:
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=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=1−x+yy. 对于定值yy, 显然xx 越大越好, 对于定值xx, 显然yy越小越好. 于是按照a_1a1和a_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; }
相关文章推荐
- linux 操作系统中chgrp命令的使用
- Python 用文件保存游戏(1)
- yii redies 不同的工程缓存key的问题
- 字符串内容逆置(不能使用库函数只能开辟有限个空间)
- Hprof
- golang与PHP输出excel示例
- 红帽6 RHCE&RHCS认证培训笔记(1)
- 集成学习——Bootstrap Bagging AdaBoost算法
- NOI1994 最佳旅游路线 解题报告
- 2016.07.22回顾
- 【HDU-oj】-2124-Repair the Wall(贪心)
- Android基础&进阶--墙裂推荐
- Hibernate中使用HQL进行查询操作
- 动态申请二维数组做矩阵乘法
- js 数组操作
- Java 自动装箱与拆箱(Autoboxing and unboxing)
- C# 编码规范和编程好习惯
- 用SQL语句,删除掉重复项只保留一条
- C# 为枚举创建新方法
- uva10099+uvauva10048