Excellent Team(模拟,细节)

A - Excellent Team
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Gibbs: Next!
First Pirate: My wife ran off with my dog and I'm drunk for a month.
Gibbs: Perfect. Next!
Second Pirate: Me have one arm and a bum leg.
Gibbs: It's the crow's nest for you. Next!
In Tortuga the Captain Jack Sparrow and Will Turner set up an excellent team. And now Jack wants to elect a captain's mate — the most worthy pirate in the new crew, who has fewer disadvantages and can be a role model for the rest.
Without thinking a lot Jack decided to use the following uncomplicated plan to choose the best pirate of the crew. Firstly, he ranks  ncontenders in one long row, beckons the first one and this first pirate is a current contender to be the mate. Then Jack walks along the row and stares at everybody one by one. He compares the regular pirate with the current contender and if he sees that the regular pirate has fewer disadvantages, then he changes the current contendor to the regular pirate. In the end of this process the new mate will stand near Jack.
Will knows about Jack’s plan and wants to count what pirate will have most comparisons while Jack elects. Let’s help Will with his calculations.

Input

The first line contains an integer  n that is the number of pirates in the crew (1 ≤  n ≤ 10 5). Next line contains  n integers:  a 1a 2, …,  a n, where a i is the number of disadvantages of  i-th contender in Jack's opinion (1 ≤  a i ≤ 10 9). The pirates are numbered in the way they stood in the row in the beginning of the elections. It is guaranteed that the numbers of disadvantages, which the pirates have, are pairwise different.

Output

Output the number of a pirate who was compared with others maximal number of times. If there are several such pirates, you can output any of them.

Sample Input

input output
6
2 5 3 4 1 9
1

原题连接:http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=17352#problem/A
这题不难,但是很容易就WA……
关键点:当更换contendor时,新的contendor已经经历了一次比较。
AC Code:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define eps (1e-8)
#define dg(i) cout << "*" << i << endl;

using namespace std;

int a[100001];

int main()
{
	int n, m, cnt, maxCnt, ans, j;
	while (scanf("%d", &n) != EOF)
	{
		j = 0;
		cnt = 0;
		maxCnt = -1;
		ans = 0;
		scanf("%d", &a[0]);
		for (int i = 1; i < n; ++i)
		{//cout << "j = " << j << endl;
			scanf("%d", &a[i]);
			if (a[j] < a[i])
			{
				cnt++;  //比较次数+1
			}
			else
			{
			    cnt++;
                if (cnt > maxCnt)
				{
				    maxCnt = cnt;
                    ans = j;
				}
				j = i;
                cnt = 1;  //不能恢复cnt为0!!!因为替换contendor时已比较一次
			}
		}
		if(cnt > maxCnt) ans = j;
		printf ("%d\n", ans + 1);
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值