Description
Recently, Jvruo became obsessed with a fighting game. In this game, n characters stand on an arena from left to right, and the combat power of the ith character is a i a_i ai . For each operation, Jvruo will choose two adjacent characters x and y on the arena for a duel. If a x a_x ax > a y a_y ay, then x wins, if a x a_x ax < a y a_y ay, then y wins. If a x a_x ax = a y a_y ay, then both x and y have a probability of 50% to win. The victorious character will stay in the arena and double the combat power; the losing character will leave the arena.
Now Jvruo will perform n − 1 operations, after n − 1 operations, there will only be one character left in the ring. Obviously, Jvruo has (n − 1)! operation modes. In all these modes of operation, which characters have the probability to stay till the end and become the final winner.
Input
The first line contains a positive integer n ( 1 ≤ n ≤ 5 ∗ 1 0 5 ) n(1 ≤ n ≤ 5∗10^5) n(1≤n≤5∗105), which represents the number of the characters.
The second line contains n integers separated by spaces a i ( 1 ≤ a i ≤ 1 0 9 ) a_i(1 ≤ a_i ≤ 10^9) ai(1≤ai≤109), which represents the the combat power of the ith character.
Output
The first line contains a positive integer m, which represents the number of the characters who have the probability to stay till the end and become the final winner.
The second line contains m integers in ascending order separated by spaces b i ( b 1 < b 2 < … … < b m ) b_i(b_1 < b_2 < …… < b_m) bi(b1<b2<……<bm), which represents the the index of the characters who have the probability to stay till the end and become the final winner.
Sample Input5 1 2 30 16 1
Sample Output
2 3 4
解题思路:
读取的时候顺便找到该组数据的最大值。为什么需要这个最大值,因为你的战斗力加倍后大于等于最大值,说明你谁都打得过,就不用再比了,是必赢的。怎么比?每一个人都从中间往两边比(双指针),比到两边都打不过或者比完了。如果到达最大值/边界外,就是最后的赢家。
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+10;
int all;
int a[N], b[N];
int main()
{
int n; cin>>n;
int mx = 0;
for(int i=1;i<=n;i++)
{
cin>>b[i];
mx = max(mx, b[i]);
}
for(int i=1;i<=n;i++)
{
int l=i-1, r=i+1;
int sum=b[i];
bool flag = true;
while(flag)
{
flag=false;
if(l>=1 && b[l]<=sum)
{
flag = true;
sum *= 2;
l--;
if(sum >= mx) break;
}
if(r<=n && b[r]<=sum)
{
flag = true;
sum *= 2;
r++;
if(sum >= mx) break;
}
}
if((l==0 && r==n+1) || sum>=mx) a[all++]=i;
}
printf("%d\n", all);
for(int i=0; i<all; i++)
{
if(i) printf(" ");
printf("%d", a[i]);
}
return 0;
}