Two players play a game.
Initially there are nn integers a1,a2,…,ana1,a2,…,an written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. n−1n−1 turns are made. The first player makes the first move, then players alternate turns.
The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it.
You want to know what number will be left on the board after n−1n−1 turns if both players make optimal moves.
The first line contains one integer nn (1≤n≤10001≤n≤1000) — the number of numbers on the board.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106).
Print one number that will be left on the board.
3 2 1 3
2
3 2 2 2
2
In the first sample, the first player erases 33 and the second erases 11. 22 is left on the board.
In the second sample, 22 is left on the board regardless of the actions of the players.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
int a[1005];
cin>>n;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
if(n%2==0)
cout<<a[n/2-1]<<endl;
else
cout<<a[n/2]<<endl;
return 0;
}
#include<bits/stdc++.h>
#define _max 5000
using namespace std;
int a[_max],dp[_max][_max],ans[_max][_max];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++) {
cin>>a[i];
dp[i][i]=a[i];
ans[i][i]=a[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j+i<=n;j++){
int t=i+j;
dp[j][t]=dp[j+1][t]^dp[j][t-1];
ans[j][t]=max(dp[j][t],max(ans[j+1][t],ans[j][t-1]));
}
}
int q;
cin>>q;
while(q--){
int l,r;
cin>>l>>r;
cout<<ans[l][r]<<endl;
}
return 0;
}
You work in a big office. It is a 9 floor building with an elevator that can accommodate up to 4 people. It is your responsibility to manage this elevator.
Today you are late, so there are queues on some floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you know the order in which people came to the elevator.
According to the company's rules, if an employee comes to the elevator earlier than another one, he has to enter the elevator earlier too (even if these employees stay on different floors). Note that the employees are allowed to leave the elevator in arbitrary order.
The elevator has two commands:
- Go up or down one floor. The movement takes 1 second.
- Open the doors on the current floor. During this operation all the employees who have reached their destination get out of the elevator. Then all the employees on the floor get in the elevator in the order they are queued up while it doesn't contradict the company's rules and there is enough space in the elevator. Each employee spends 1 second to get inside and outside the elevator.
Initially the elevator is empty and is located on the floor 1.
You are interested what is the minimum possible time you need to spend to deliver all the employees to their destination. It is not necessary to return the elevator to the floor 1.
The first line contains an integer n (1 ≤ n ≤ 2000) — the number of employees.
The i-th of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 9, ai ≠ bi) — the floor on which an employee initially is, and the floor he wants to reach.
The employees are given in the order they came to the elevator.
Print a single integer — the minimal possible time in seconds.
2 3 5 5 3
10
2 5 3 3 5
12
正在解决