hdu-5742 It's All In The Mind(数学)
2024-09-03 12:01:56
题目链接:
It's All In The Mind
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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}, 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.
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 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.
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
题意:
给一个单调序列的一部分,让你求这个式子的最大值;
思路:
(a1+a2)/(∑ai)的最小值;就是∑ai/(a1+a2)的最大值;就是1+(a3+a4+...an)/(a1+a2)的最大值;然后就是分子上的尽量小,分母上的尽量大了,相当于不等式的放缩;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+10;
const int maxn=500+10;
const double eps=1e-6; int a[maxn]; int gcd(int x,int y)
{
if(y==0)return x;
return gcd(y,x%y);
}
int main()
{
int t;
read(t);
while(t--)
{
int n,m;
read(n);read(m);
mst(a,-1);
int x,y;
For(i,1,m)
{
read(x);read(y);
a[x]=y;
}
int sum=0;
if(a[n]==-1)a[n]=0;
sum+=a[n];
for(int i=n-1;i>2;i--)
{
if(a[i]==-1)a[i]=a[i+1];
sum+=a[i];
}
if(a[1]==-1)a[1]=100;
if(a[2]==-1)a[2]=a[1];
int p,q;
p=a[1]+a[2];
q=sum+p;
//if(q==0)
int g=gcd(p,q);
cout<<p/g<<"/"<<q/g<<endl;
}
return 0;
}
最新文章
- Event的Propagate
- mysql 重置root密码
- 工作随笔——Swift中的Range和一些字符操作
- CentOS 6.5部署安装Memcached
- ==和equals的区别
- POJ 2193 Lenny&#39;s Lucky Lotto Lists (DP)
- JavaScript中值的真真假假(true and false)
- CSS属性[text-overflow]使用问题
- New ipad install Metasploit(New ipad 安装Metasploit)
- oschina企业应用
- 观察者模式:猫叫鼠跑人醒(C#)
- PHP发送短信如何实现?
- C语言-指针、数组、结构体、分支、循环混合使用
- Node.js之包与npm包管理工具
- C语言程序设计第一次实验
- Python基础之 函数名,闭包,和迭代器
- p9半幺群
- Django--分页器(paginator)
- 【IDEA&;&;Eclipse】2、从Eclipse转移到IntelliJ IDEA一点心得
- python 全栈开发,Day108(客户管理之权限控制,客户管理之动态";一级";菜单,其他应用使用rbac组件,django static文件的引入方式)