Codeforces Round #330 (Div. 2)C. Warrior and Archer
2016-08-24 09:54
746 查看
C. Warrior and Archer
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and
the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.
Vova and Lesha are friends. They often meet at Vova's place and compete against each other in a computer game named The Ancient Papyri: Swordsink. Vova always chooses a warrior as his fighter and Leshac chooses an archer. After that they should choose initial
positions for their characters and start the fight. A warrior is good at melee combat, so Vova will try to make the distance between fighters as small as possible. An archer prefers to keep the enemy at a distance, so Lesha will try to make the initial distance
as large as possible.
There are n (n is
always even) possible starting positions for characters marked along the Ox axis. The positions are given by their distinct coordinates x1, x2, ..., xn,
two characters cannot end up at the same position.
Vova and Lesha take turns banning available positions, Vova moves first. During each turn one of the guys bans exactly one of the remaining positions. Banned positions cannot be used by both Vova
and Lesha. They continue to make moves until there are only two possible positions remaining (thus, the total number of moves will be n - 2).
After that Vova's character takes the position with the lesser coordinate and Lesha's character takes the position with the bigger coordinate and the guys start fighting.
Vova and Lesha are already tired by the game of choosing positions, as they need to play it before every fight, so they asked you (the developer of the The Ancient Papyri: Swordsink) to write a module that would automatically determine the distance at which
the warrior and the archer will start fighting if both Vova and Lesha play optimally.
Input
The first line on the input contains a single integer n (2 ≤ n ≤ 200 000, n is
even) — the number of positions available initially. The second line contains n distinct integers x1, x2, ..., xn (0 ≤ xi ≤ 109),
giving the coordinates of the corresponding positions.
Output
Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.
Examples
input
output
input
output
Note
In the first sample one of the optimum behavior of the players looks like that:
Vova bans the position at coordinate 15;
Lesha bans the position at coordinate 3;
Vova bans the position at coordinate 31;
Lesha bans the position at coordinate 1.
After these actions only positions 0 and 7 will
remain, and the distance between them is equal to 7.
In the second sample there are only two possible positions, so there will be no bans.
题意:x坐标轴上的n个点的坐标有两个人依次从这些点中抹去一个点先手想让左后剩下的两点的距离最小,后手想让最后剩下的两点的距离最大两人均采取最优策略问最后剩下的两点之间的距离;
思路:脑洞啊,假设最后剩下的两个点的坐标为l,r可以确定这两个坐标之间的相隔的点数为n/2-1 因为先手会尽可能的让这两个点之间的相隔的点数尽量小所以他一定会取这两点之外的点而后手则希望这两点之间相隔的点数尽可能的多因此他一定会取中间的点都最后可保证l和r之间相隔的点数为n/2-1因为先手希望距离尽可能的小因此取最小值即可。
/* ***********************************************
Author : ryc
Created Time : 2016-08-23 Tuesday
File Name : E:\acm\codeforces\330C.cpp
Language : c++
Copyright 2016 ryc All Rights Reserved
************************************************ */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int maxn=1000010;
int num[maxn];
int main()
{
int n;cin>>n;
for(int i=1;i<=n;++i){
scanf("%d",&num[i]);
}
sort(num+1,num+n+1);
int ans=1e9+10;
for(int i=1;i<=n/2;++i){
ans=min(ans,num[n/2+i]-num[i]);
}
printf("%d\n",ans);
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and
the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.
Vova and Lesha are friends. They often meet at Vova's place and compete against each other in a computer game named The Ancient Papyri: Swordsink. Vova always chooses a warrior as his fighter and Leshac chooses an archer. After that they should choose initial
positions for their characters and start the fight. A warrior is good at melee combat, so Vova will try to make the distance between fighters as small as possible. An archer prefers to keep the enemy at a distance, so Lesha will try to make the initial distance
as large as possible.
There are n (n is
always even) possible starting positions for characters marked along the Ox axis. The positions are given by their distinct coordinates x1, x2, ..., xn,
two characters cannot end up at the same position.
Vova and Lesha take turns banning available positions, Vova moves first. During each turn one of the guys bans exactly one of the remaining positions. Banned positions cannot be used by both Vova
and Lesha. They continue to make moves until there are only two possible positions remaining (thus, the total number of moves will be n - 2).
After that Vova's character takes the position with the lesser coordinate and Lesha's character takes the position with the bigger coordinate and the guys start fighting.
Vova and Lesha are already tired by the game of choosing positions, as they need to play it before every fight, so they asked you (the developer of the The Ancient Papyri: Swordsink) to write a module that would automatically determine the distance at which
the warrior and the archer will start fighting if both Vova and Lesha play optimally.
Input
The first line on the input contains a single integer n (2 ≤ n ≤ 200 000, n is
even) — the number of positions available initially. The second line contains n distinct integers x1, x2, ..., xn (0 ≤ xi ≤ 109),
giving the coordinates of the corresponding positions.
Output
Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.
Examples
input
6 0 1 3 7 15 31
output
7
input
2
73 37
output
36
Note
In the first sample one of the optimum behavior of the players looks like that:
Vova bans the position at coordinate 15;
Lesha bans the position at coordinate 3;
Vova bans the position at coordinate 31;
Lesha bans the position at coordinate 1.
After these actions only positions 0 and 7 will
remain, and the distance between them is equal to 7.
In the second sample there are only two possible positions, so there will be no bans.
题意:x坐标轴上的n个点的坐标有两个人依次从这些点中抹去一个点先手想让左后剩下的两点的距离最小,后手想让最后剩下的两点的距离最大两人均采取最优策略问最后剩下的两点之间的距离;
思路:脑洞啊,假设最后剩下的两个点的坐标为l,r可以确定这两个坐标之间的相隔的点数为n/2-1 因为先手会尽可能的让这两个点之间的相隔的点数尽量小所以他一定会取这两点之外的点而后手则希望这两点之间相隔的点数尽可能的多因此他一定会取中间的点都最后可保证l和r之间相隔的点数为n/2-1因为先手希望距离尽可能的小因此取最小值即可。
/* ***********************************************
Author : ryc
Created Time : 2016-08-23 Tuesday
File Name : E:\acm\codeforces\330C.cpp
Language : c++
Copyright 2016 ryc All Rights Reserved
************************************************ */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int maxn=1000010;
int num[maxn];
int main()
{
int n;cin>>n;
for(int i=1;i<=n;++i){
scanf("%d",&num[i]);
}
sort(num+1,num+n+1);
int ans=1e9+10;
for(int i=1;i<=n/2;++i){
ans=min(ans,num[n/2+i]-num[i]);
}
printf("%d\n",ans);
return 0;
}
相关文章推荐
- asp Fix、Int、Round、CInt函数使用说明
- PHP四舍五入、取整、round函数使用示例
- sqlserver四舍五入使用round函数及cast和convert函数
- PHP中round()函数对浮点数进行四舍五入的方法
- php取整函数ceil,floo,round的用法及介绍
- C#使用round函数四舍五入的方法
- JavaScript里四舍五入函数round用法实例
- round robin权重轮循算法php实现代码
- python学之round()函数
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- Codeforces Educational Codeforces Round 16 部分题解
- 【WHUST 2016 Individual Contest #1】解题报告
- 【WHUST 2016 Individual Contest #2】解题报告
- 【WHUST 2016 Individual Contest #3 】解题报告
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation