Happy Three Friends
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Dong-hao , Grandpa Shawn , Beautful-leg Mzry are good friends. One day , they want to play a game.
There are 6 numbers on the table.
Firstly , Dong-hao can change the order of 6 numbers.
Secondly , Grandpa Shawn take the first one and the last one , sum them up as his scores.
Thirdly , Beautiful-leg Mzry take any of 3 numbers from the last 4 numbers , and sum them up as his scores.
Finally , if Grandpa Shawn's score is larger than Beautiful-leg Mzry's , Granpa Shawn wins!
If Grandpa Shawn's score is smaller than Beautiful-leg Mzry's , Granpa Shawn loses.
If the scores are equal , there is a tie.
Nowadays , it's really sad that Grandpa Shawn loses his love. So Dong-hao wants him to win(not even tie). You have to tell Dong-hao whether he can achieve his goal.
Input
There is a number T shows there are T test cases below. ( T <= 50)
For each test case , there are 6 numbers Ai ( 1 <= Ai <= 100 ).
Output
If Dong-hao can achieve his goal , output "Grandpa Shawn is the Winner!"
If he can not , output "What a sad story!"
Sample Input
3 1 2 3 3 2 2 2 2 2 2 2 2 1 2 2 2 3 4
传送门:点击打开链接
解题思路:
排序。将最大的两个值得和与第3,4,5大的值的和比较。
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
int t, a[7];
scanf("%d", &t);
while(t--)
{
for(int i = 0; i < 6; ++i)
scanf("%d", &a[i]);
sort(a, a+6);
printf("%s\n", a[4]+a[5] > a[1]+a[2]+a[3]?
"Grandpa Shawn is the Winner!" : "What a sad story!");
}
return 0;
}
1002
Miaomiao's Geometry
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 10 Accepted Submission(s): 3
Problem Description
There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length.
There are 2 limits:
1.A point is convered if there is a segments T , the point is the left end or the right end of T.
2.The length of the intersection of any two segments equals zero.
For example , point 2 is convered by [2 , 4] and not convered by [1 , 3]. [1 , 2] and [2 , 3] are legal segments , [1 , 2] and [3 , 4] are legal segments , but [1 , 3] and [2 , 4] are not (the length of intersection doesn't equals zero), [1 , 3] and [3 , 4] are not(not the same length).
Miaomiao wants to maximum the length of segements , please tell her the maximum length of segments.
For your information , the point can't coincidently at the same position.
Input
There are several test cases.
There is a number T ( T <= 50 ) on the first line which shows the number of test cases.
For each test cases , there is a number N ( 3 <= N <= 50 ) on the first line.
On the second line , there are N integers Ai (-1e9 <= Ai <= 1e9) shows the position of each point.
Output
For each test cases , output a real number shows the answser. Please output three digit after the decimal point.
Sample Input
3 3 1 2 3 3 1 2 4 4 1 9 100 10
Sample Output
1.000 2.000 8.000
传送门:点击打开链接
解题思路:
贪心。这条题目不可以用二分做,这里给出一组数据(官方题解给的):
4
0 1 4 5
这组数据的答案应该是3,用二分做答案是1.5。
正确的贪心策略是这样的,枚举所有可能的区间长度(给出的数值的差或者差的一半),最大的满足条件的即为答案。判断一个区间长度是否满足题意,我们是这样做的:将给出的数按从小到大排列,因为每个数都必须是区间的端点,所以,这个数要么是左端点,要么是右端点,我们先看这个数能够做左端点,如果可以,就记录下来,将这个数作为左端点,否则,再看这个数能否作为右端点,如果可以,就记录下来作为右端点,否则,就是不符合条件的。
这里再给几组数据:
4
6
1 5 100 140 200 250
6
1 5 100 140 200 210
6
1 5 100 110 200 210
3
-1000000000 0 1000000000
答案:
47.500
40.000
45.000
1000000000.000
代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const double EPS = 1e-4;
int t, n, a[55];
double dis[100];
bool fun(double len)
{
double tmp = a[0];
for(int i = 1; i < n; ++i)
{
if(fabs(tmp-a[i]) < EPS)
continue;
if(tmp > a[i])
return false;
if(tmp + len <= a[i])
tmp = a[i];
else
tmp = a[i] + len;
}
return true;
}
int main()
{
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
sort(a, a+n);
int cnt = 0;
for(int i = 1; i < n; ++i)
{
dis[cnt++] = a[i] - a[i-1];
dis[cnt++] = (a[i]-a[i-1]) * 0.5;
}
sort(dis, dis+cnt);
double ans = 0;
for(int i = 0; i < cnt; ++i)
{
if(fun(dis[i])) ans = dis[i];
//printf("%.2f\n", dis[i]);
}
printf("%.3f\n", ans);
}
return 0;
}