GDCPC2021 1171:Double

D-Double

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(1n5105), 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(1ai109), 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 Input

5
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花生ono

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值