It’s All In The Mind
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 857 Accepted Submission(s): 401
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:
- For every i∈{1,2,…,n}, 0≤ai≤100.
- The sequence is non-increasing, i.e. a1≥a2≥…≥an.
- 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.
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 (2≤n≤100,0≤m≤n) – the length of the
题意:给你
n
个数,已知了
思路:我们可以将式子转化一下得:求 1−∑ni=3ai∑ni=1ai 的最大值,由式子得要保证 ∑ni=3ai 取最小值,这样 a1+a2 就要取最大值, a1 和 a2 我们可以通过检查是否赋过值来进行定义值, a2 后面的数,根据条件 a1≥a2≥...≥an ,我们可以从 an 开始从后向前扫,初始化为0,如果遇到赋过值的,改变数值,最后求和就好了,具体看代码。
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int a[MAXN];
int main()
{
int t;scanf("%d",&t);
while(t--)
{
int n,m;scanf("%d%d",&n,&m);
memset(a,-1,sizeof(a));
for(int i=1;i<=m;i++)
{
int b,c;scanf("%d%d",&b,&c);
a[b]=c;
}
if(a[1]==-1)
{
a[1]=100;
if(a[2]==-1) a[2]=100;
}
else if(a[2]==-1)
a[2]=a[1];
int k=0;
for(int i=n;i>=3;i--)
{
if(a[i]==-1)
a[i]=k;
else
k=a[i];
}
ll down=0,up=a[1]+a[2];
for(int i=1;i<=n;i++)
down+=a[i];
ll gc=gcd(up,down);
printf("%I64d/%I64d\n",up/gc,down/gc);
}
return 0;
}