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 ai . For each operation, Jvruo will choose two adjacent characters x and y on the arena for a duel. If ax > ay, then x wins, if ax < ay, then y wins. If ax = 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∗10e5), which represents the number of the characters.
The second line contains n integers separated by spaces ai(1 ≤ ai ≤ 10e9), 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 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
Inputcopy | Outputcopy |
---|---|
5 1 2 30 16 1 | 2 3 4 |
先提供一下简单的代码:运行时长148ms
#include <bits/stdc++.h>
using namespace std;
#define int long long
int N, M, K;
const int maxn = 500005;
vector<int>nums(maxn);
vector<int>ans;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> nums[i];
K = max(K, nums[i]);
}
for (int i = 0; i < nums.size(); i++)
{
int L = i - 1, R = i + 1, sum = nums[i];
bool win = false;
while (1)
{
if (sum >= K)
{
win = true;
break;
}
if (L >= 0 && nums[L] < sum)
{
sum *= 2;
L--;
}
else if (R < N && nums[R] < sum)
{
sum *= 2;
R++;
}
else
break;
}
if (win)ans.emplace_back(i + 1);
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++)
{
if (i)cout << " " << ans[i];
else cout << ans[i];
}
return 0;
}
快读代码(90ms)可以优化,这边省略了
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define lowbit(x) ((x)&(-(x)))
#define int long long
#define rep(i,a,b) for(int i=(int)a,i##i=(int)b;i<=i##i;i++)
#define per(i,a,b) for(int i=(int)a,i##i=(int)b;i>=i##i;i--)
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ULL;
const int N = 1000005;
const double PI = acos(-1);
int all;
int a[500005];
int b[500005];
inline int read(){
int res=0,f=1;
char c;
while((c=getchar())<'0'||c>'9') if('-'==c) f=-1;else if(EOF==c)exit(0);
do res=(res<<1)+(res<<3)+(c^48); while((c=getchar())>='0'&&c<='9');
return res*f;
}
void solve()
{
int n;
n=read();
for(int i=1;i<=n;i++){
b[i]=read();
}
for(int i=1;i<=n;i++){
int l=i-1,r=i+1;
int flag=1;
int sum=b[i];
while(flag){
int flagl=0,flagr=0;
while(l>=1&&b[l]<=sum){
flagl = 1;
sum+=sum;
if(sum<0){
l=0;r=n+1;
break;
}
l--;
}
while(r<=n&&b[r]<=sum){
flagr = 1;
sum+=sum;
if(sum<0){
l=0;r=n+1;
break;
}
r++;
}
if(flagl || flagr) flag =1;
else flag = 0;
}
if(l==0 && r==n+1)a[all++]=i;
}
printf("%lld\n",all);
for(int i=0;i<all;i++){
if(i)printf(" %lld",a[i]);
else printf("%lld",a[i]);
}
}
signed main()
{
int _ = 1;
while(_--)
solve();
}