Fighting Monsters(vector模拟+斐波那契数列)

Emma just discovered a new card game called Gwint: A wizard’s game. There are two types of cards: monster cards and spell cards. Monster cards are used to score points, while spell cards typically interact with the monsters in some way.

On each monster card there is an integer value, the power of the monster. Monsters can fight each other, and during these fights the power acts as both the strength and the health of the monster. The monsters take turns hitting each other until one of them dies. Whenever a monster AA hits a monster BB, this causes BB to lose an amount of power equal to the power of AA. Conversely, if BB hits AA, AA loses power equal to the power of BB (see the example below). This continues until one of the two monsters has a power of zero or less, at which point this monster is considered dead.

One of Emma’s most beloved cards in the game is a spell called Fight! which states:

Pick two monsters. They fight each other to the death. If the surviving monster has a power of exactly 11 left, return this card to your hand.

Of course, Emma would like to play as efficiently as possible by picking two monsters such that Fight! is returned to her hand. However, there are often a lot of monsters on the board, which makes it very time consuming to figure out whether this can be done or not. Can you help her find two monsters she can pick so that she gets the card back?

Input Format
The input consists of:one line with an integer nn (2 \le n \le 10^5)(2≤n≤10 5), the number of monsters;one line with nn integers m_1, \cdots, m_nm 1 ,⋯,m n(1 \le m_i \le 10^6)(1≤m i ≤10 6 ), giving the power of each monster.

Output Format
If there is no pair of monsters that Emma can pick, output impossible. Otherwise, output two distinct integers ii, jj (1 \le i, j \le n)(1≤i,j≤n), where ii is the index of the monster that starts the fight and jj is the index of the other monster. If multiple solutions exist, print the minimum pair(i,j)(i,j).

样例输入1
4
1 12 67 8
样例输出1
impossible
样例输入2
5
1 1 12 67 8
样例输出2
2 1
样例输入3
6
1 5 6 7 90 8
样例输出3
2 6
原文链接:https://blog.csdn.net/qq_43150596/article/details/100107594

题解:

1:fib数列:从题意中可得,此题为斐波那契数列

2:vector 模拟:查找和模拟的时候用vector比较简单方便

3:循环:循环vector只用循环40次!

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+6;
int fib[40];
vector<int>ve[maxn];//vector真好用!
int main()
{
    fib[1]=1;fib[2]=1;
    for(int i=3;i<=40;i++)
    {
        fib[i]=fib[i-1]+fib[i-2];
    }
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;cin>>x;
        ve[x].push_back(i);
    }
    if(ve[1].size()>=2)
    {
        cout<<ve[1][0]<<' '<<ve[1][1]<<'\n';
    }
    else
    {
        for(int i=2;i<40&&fib[i+1]<maxn;i++)
        {
            if(ve[fib[i]].size()&&ve[fib[i+1]].size())
            {
                cout<<ve[fib[i]][0]<<' '<<ve[fib[i+1]][0]<<'\n';
                return 0;
            }
        }
        cout<<"impossible\n";
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值