HDU 5742 It's All In The Mind (贪心)
It's All In The Mind
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5742
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 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
Source
2016 Multi-University Training Contest 2
##题意:
对于一个数组a1 - an,部分元素已知,部分未知.
数组满足性质:0≤ai≤100, 非严格递减, 所有数之和非0.
求所有满足情况的数组中,a1+a2/sum(ai) 的最大值.
##题解:
贪心的想法:
a1和a2应该尽量大; 其余数尽量小.
WA点:a1已知但a2未知,注意不要把a2直接赋成100;
WA了一个下午....弱的不行
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define double LL
#define eps 1e-8
#define maxn 150
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n,m;
int num[maxn];
int gcd(int a, int b) {
return b==0? a:gcd(b,a%b);
}
int main(void)
{
//IN;
int t; cin >> t;
while(t--)
{
memset(num, -1, sizeof(num));
cin >> n >> m;
for(int i=1; i<=m; i++) {
int x,y; scanf("%d %d", &x,&y);
num[x] = y;
}
int mimi = 0;
for(int i=n; i>=3; i--) {
if(num[i] == -1) {
num[i] = mimi;
} else {
mimi = num[i];
}
}
if(num[1] == -1) num[1] = 100;
if(num[2] == -1) num[2] = min(100, num[1]);
int sum1 = num[1] + num[2];
int sum2 = sum1;
for(int i=3; i<=n; i++) sum2 += num[i];
if(sum2 == 0) sum2 = 1;
int gcds = gcd(sum1, sum2);
printf("%d/%d\n", sum1/gcds, sum2/gcds);
}
return 0;
}
HDU 5742 It's All In The Mind (贪心)的更多相关文章
- HDU 5742 It's All In The Mind (贪心) 2016杭电多校联合第二场
题目:传送门. 题意:求题目中的公式的最大值,且满足题目中的三个条件. 题解:前两个数越大越好. #include <iostream> #include <algorithm> ...
- hdu 5742 It's All In The Mind 水题
It's All In The Mind 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5742 Description Professor Zhan ...
- HDU 5742 Chess SG函数博弈
Chess Problem Description Alice and Bob are playing a special chess game on an n × 20 chessboard. ...
- HDU 5742 It's All In The Mind
It's All In The Mind Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- hdu 5742 It's All In The Mind(2016多校第二场)
It's All In The Mind Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5055 Bob and math problem(简单贪心)
http://acm.hdu.edu.cn/showproblem.php?pid=5055 题目大意: 给你N位数,每位数是0~9之间.你把这N位数构成一个整数. 要求: 1.必须是奇数 2.整数的 ...
- HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Problem Description Here is a famous story in Ch ...
- HDU 1053 Entropy(哈夫曼编码 贪心+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1053 Entropy Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1052 Tian Ji -- The Horse Racing (贪心)(转载有修改)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- 【问底】徐汉彬:Web系统大规模并发——电商秒杀与抢购
[导读]徐汉彬曾在阿里巴巴和腾讯从事4年多的技术研发工作,负责过日请求量过亿的Web系统升级与重构,目前在小满科技创业,从事SaaS服务技术建设. 电商的秒杀和抢购,对我们来说,都不是一个陌生的东西. ...
- 通知角标(2)只用一个TextView实现
可以只用一个TextView实现通知角标,TextView的setCompoundDrawables函数可以在TextView的上,下,左,右,4条边处分别指定一个图片.见图1: 这个图片如果在角上, ...
- php扩展函数调用扩展中的标准函数
这几天在写php的扩展函数,在网上学习步骤什么的都有,一般问题也都能查到,所以就不再此啰嗦,写这篇博客的原因是因为遇到的一个问题,百度谷歌都没找到,对于初学者,这个或许有用,对于过来人,我想他们肯定也 ...
- poj1088
这题是dp还是dfs+记忆化?(其实好像没什么区别?) 用f[i,j]表示滑到(i,j)时之后最多能滑多远,依次穷举每一个起点(i,j)则 f[i,j]=max{f[i,j-1],f[i-1,j],f ...
- POJ 1088 滑雪【记忆化搜索】
题意:给出一个二维矩阵,要求从其中的一点出发,并且当前点的值总是比下一点的值大,求最长路径 记忆化搜索,首先将d数组初始化为0,该点能够到达的路径长度保存在d数组中,同时把因为路径是非负的,所以如果已 ...
- Asp.Net多线程用法1
Asp.Net多线程简单用法 一个web页面 default.aspx 里面有两个控件GridView1,GridView2,通过两个线程分别加载绑定数据. protected void Page_L ...
- JQuery Ajax 在asp.net中使用小结
自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...
- chrome 远程调试(转)
http://www.tuicool.com/articles/ZJfeAzi 由于 appspot.com被墙,一般调试不成功. 随着智能手机的普及,移动设备的浏览器功能越来越强大,我们用手机上网时 ...
- directdraw的多画面显示rgb
// showpicDlg.cpp : 实现文件 // #include "stdafx.h" #include "showpic.h" #include &q ...
- 【转】android 自定义控件 使用declare-styleable进行配置属性(源码角度)
原文网址:http://blog.csdn.net/vipzjyno1/article/details/23696537 最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行 ...