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

Sample Input

Sample Output
/
/

题意:

  有一个数字序列,给出数字序列的性质是:

  1.每个数最大为100,最小为0

  2.序列非递增

  3.序列总和不为0

  求出这个数列的最大值

  样例:第一行是T,表示T组测试样例,第二行是n,m。表示序列的长度和已知数的个数。下m行为xy,表示axi=yi

      第一组 长度为2,已知0个。则最大值为(100+100)/(100+100)或者(1+1)/(1+1)最大值为1。

      第二组长度为3,已知1个。已知的这一个为a3=1 。则最大值为(100+100)/(100+100+1)

思路:

  想求(a1+a2)/(a1+a2+...+an) 的最大值,只要让分母尽可能大,分子尽可能小即可。

  有两种情况,第一种为已知a1,a2。让不知道的数取最小值(不是0,因为要保证序列非递增)。

  第二种情况,不知道a1或者a2。让a1和a2取最大值(也要注意序列非递增)。

  最后用GCD求出a1+a2与序列和的最大公因数,然后取分数。

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int a,int b)//求最大公约数
{
if(b==)
return a;
return gcd(b,a%b);
}
int main()
{
int a[],t,m,n;
scanf("%d",&t);
while(t--)
{
int i,j,x,y,sum1=,sum2=,ans=;
int w=;
scanf("%d%d",&n,&m);
for(i=; i<=n; i++)
a[i]=-;//初始化数组为-1,因为数组从0-100,所以可以用-1来表示
for(i=; i<=m; i++)
{
scanf("%d%d",&x,&y);
a[x]=y;//将已知条件赋值
}
for(i=n; i>=; i--)//从3-n尽量取最小
{
if(a[i]==-)//如果后面没有值,说明没有非递增限制,直接赋值为w,w初始为0
a[i]=w;
else//如果已经有值,则值前面的赋值为这个值。
{
w=a[i];
}
}
if(a[]==-)/*如果a[1]没有值,则赋值为100(最大)*/
a[]=;
if(a[]==-)/*如果a[2]没有值,则与a[1]相同*/
a[]=a[];
for(i=; i<=n; i++)
sum1+=a[i];
sum2=a[]+a[];
ans=gcd(sum2,sum1);
printf("%d/%d\n",(sum2/ans),(sum1/ans));
}
}

HDU5742 It's All In The Mind 数学思维题的更多相关文章

  1. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  2. Gym 100801D Distribution in Metagonia (数学思维题)

    题目:传送门.(需要下载PDF) 题意:t组数据,每组数据给定一个数ni(1 ≤ ni ≤ 10^18),把ni拆成尽可能多的数,要求每个数的素因子只包含2和3,且这些数不能被彼此整除,输出一共能拆成 ...

  3. HDU5742 It's All In The Mind(思维题,水题)

    Problem Description Professor Zhang has a number sequence a1,a2,...,an. However, the sequence is not ...

  4. 51Nod 1003 阶乘后面0的数量(数学,思维题)

    1003 阶乘后面0的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 5         难度:1级算法题 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 720 ...

  5. BZOJ4377 Kurs szybkiego czytania \ Luogu 3589[POI2015]KUR - 数学思维题

    Solution 我又双叒叕去看题解啦$QAQ$, 真的想不到鸭 输入 $a$ 和 $n$ 互质, 所以满足 $a \times i \ mod \ n$ $(0<=i<n)$ 肯定是不重 ...

  6. BZOJ4377[POI2015]Kurs szybkiego czytania——数学思维题

    题目描述 给定n,a,b,p,其中n,a互质.定义一个长度为n的01串c[0..n-1],其中c[i]==0当且仅当(ai+b) mod n < p.给定一个长为m的小01串,求出小串在大串中出 ...

  7. EOJ2018.10 月赛(B 数学+思维题)

    传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9739115.html 题意: 找到最小的包含子序列a的序列s,并且序列s是 p -莫干山序 ...

  8. EOJ2018.10 月赛(A 数学+思维题)

    传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9739115.html 题意: 能否通过横着排或竖着排将 1x p 的小姐姐填满 n x m ...

  9. zoj 2818 Root of the Problem(数学思维题)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2818 题目描述: Given positive integer ...

随机推荐

  1. 要back的题目 先立一个flag

    要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...

  2. 【uva11987】带删除的并查集

    题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是 ...

  3. 【bzoj3376-方块游戏】带权并查集

    题意: n块积木,m个操作或询问.每次移动积木的时候,约翰会选择两块积木X,Y,把X搬到Y的上方.如果X已经和其它积木叠在一起了,那么应将这叠积木整体移动到Y的上方:如果Y已经和其它积木叠在一起了的, ...

  4. 【BZOJ4236】JOIOJI [DP]

    JOIOJI Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description JOIOJI桑是JOI君的叔叔 ...

  5. 省队集训 Day6 序列

    [题目大意] 给出$n$个数的序列$a_1, a_2, ..., a_n$,有$m$次操作,为下面三种: $A~l~r~d$:区间$[l,r]$,全部加$d$. $M~l~r~d$:区间$[l,r]$ ...

  6. SSM三大框架整合详细总结(Spring+SpringMVC+MyBatis)(山东数漫江湖)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经很久了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录 ...

  7. javascript延迟对象

    1.模拟任务队列: function taskQueue() { var taskList = []; var isRun = false; this.addTask = function (task ...

  8. 一种通过HTTP传文件出网的姿势

    在外网机器上运行文件服务接收服务 root@kali:~/pentest-script/FileTransfer/HttpServer# python3 SimpleHttpUpload.py Ser ...

  9. 某p2p存在通用上传漏洞

    google链接查找: inurl:shouyi.asp inurl:itemlist_xq.asp?id= 很多存在Fckeditor上传链接: FCKeditor/editor/filemanag ...

  10. 一个文档让vim飞起来

    原文地址:http://www.cnblogs.com/songfy/p/5635757.html 引言 今天我们特地来讲讲这个vim的配置. vim这东西, 很多人装逼的时候经常会提到, 不过大部分 ...