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 ...
随机推荐
- WindowManager.LayoutParams.type属性
type 的取值: 应用程序窗口. public static final int FIRST_APPLICATION_WINDOW = 1; 所有程序窗口的“基地”窗口,其他应用程序窗口都 ...
- Android ContentProvider和Uri详解 (绝对全面)
ContentProvider的基本概念 : 1.ContentProvider为存储和读取数据提供了统一的接口 2.使用ContentProvider,应用程序可以实现数据共享 3.andr ...
- Struts2系列——struts2的result
在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作. 在ActionSupport基类中定 ...
- 前端JS对后台传递的timestamp的转换
后台传递的timestamp类型的数据的JSON: Date.prototype.format = function(format) { var o = { "M+" : this ...
- CodeForces 474.D Flowers
题意: 有n朵花排成一排,小明要么吃掉连续的k朵白花,或者可以吃单个的红花. 给出一个n的区间[a, b],输出总吃花的方法数模 109+7 的值. 分析: 设d(i)表示吃i朵花的方案数. 则有如下 ...
- UVa 10817 (状压DP + 记忆化搜索) Headmaster's Headache
题意: 一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师. 每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两 ...
- bzoj2005: [Noi2010]能量采集
lsj师兄的题解 一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) * 2 - 1. 设g(i)为 gcd(x, y) = i ( 1 < ...
- 自定义ShareDialog视图
1. share_item.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayou ...
- context:property-placeholder
这个在spring中配置文件中是非常常用的. context:property-placeholder大大的方便了我们数据库的配置. 只需要在spring的配置文件里添加一句:<context: ...
- LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)
题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...