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 ...
随机推荐
- [HDOJ1827]Summer Holiday(强连通分量,缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1827 缩点后统计入度和当前强连通分量中最小花费,然后记录入度为0的点的个数和花费和就行了. /* ━━ ...
- .propertie文件注释
在.properties文件中注释,前边加#就可以
- Qt之自定义界面(添加自定义标题栏)
简述 通过上节内容,我们实现了自定义窗体的移动,但是我们缺少一个标题栏来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭的按钮. 自定义标题栏后,所有的控件我们都可以定制,比如:在标题栏中添加换 ...
- [转] Asp.net Report Viewer 简单实例
原文链接:http://www.aspsnippets.com/Green/Articles/ASPNet-Report-Viewer-control-Tutorial-with-example.as ...
- 如果你只会JQuery的插件式开发, 那么你可以进来看看?
对于JQuery的学习,已经有3年多的时间了,直到去年与我的组长一起做项目,看到他写的JS,确实特别漂亮,有时甚至还看不太懂, 我才发现其实我不太会JQuery.所以我有时间就会去看看他写的JS代码, ...
- tomcat启动中提示 - consider increasing the maximum size of the cache
tomcat启动过程中提示: org.apache.catalina.webresources.Cache.getResource Unable to add the resource at xxx ...
- Sublime-text markdown with Vim mode and auto preview
说明 最近看到markdown相关的东西,被其书写方式吸引,其实以前就在找这种类似的工具,但是也没找到,由于习惯了Vim,可Vim不支持markdown预览,这点可能不是很好,于是找到Sublime- ...
- C语言深入学习系列 - 字节对齐&内存管理
用C语言写程序时需要知道是大端模式还是小端模式. 所谓的大端模式,是指数据的低位保存在内存的高地址中,而数据的高位,保存在内存的低地址中:所谓的小端模式,是指数据的低位保存在内存的低地址中,而数据的高 ...
- (转)TCP协议那些事
(上) TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过程,但对于学习的过程却能让人有很多收获.关于TCP这个协议的细节,我还是 ...
- MYSQL设计方案
Scale Out:横向扩展,增加处理节点提高整体处理能力Scale Up:纵向扩展,通过提升单个节点的处理能力达到提升整体处理能力的目的 ReplicationMySQL的replication是异 ...