码迷,mamicode.com
首页 > 其他好文 > 详细

A - Excellent Team

时间:2014-08-23 11:12:50      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   for   ar   div   

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 n contenders 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: a1, a2, …, an, where ai is the number of disadvantages of i-th contender in Jack‘s opinion (1 ≤ ai ≤ 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

inputoutput
6
2 5 3 4 1 9
1

 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define MM 100005
struct st
{
    int num;
    int count;
} a[MM];

int main()
{
    int n,min,i,temp,flag,max=0;

    while(~scanf("%d",&n))
    {
        max=-1;
        memset(a,0,sizeof a);
        scanf("%d",&a[0].num);
        min=a[0].num;
        temp=0;
        for(i=1; i<n; i++)
        {
            scanf("%d",&a[i].num);
            if(a[i].num>=min)  ///相当于从后往前看了   每一次都更新一遍  最后总结
            {
                a[temp].count++;  ///0的
                a[i].count++;  ///0之后的
            }
            else
            {
                min=a[i].num; ///替换
                a[temp].count++; ///0的
                a[i].count++;///0之后的
                temp=i;  ///记录编号

            }
        }
        for(i=0; i<n; i++) ///比较
        {

            if(max<a[i].count)
            {
                max=a[i].count;
                flag=i+1;
            }
        }
        printf("%d\n",flag);  ///输出
    }

    return 0;
}

 

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
int a[100000];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        int j=1,i;
        int num=0;
        int max=0;
        int ans=1;
        for(i=2;i<=n;i++)
        {

            if(a[j]<a[i])
            {
                num++;
            }
            else
            {
                num++;
                if(max<num)
                {
                    max=num;
                    ans=j;
                }
                j=i;
                num=1;
            }
        }
        if(num>max)   ans=j;
        printf("%d\n",ans);
    }
    return 0;
}

 

A - Excellent Team

标签:des   style   blog   color   os   io   for   ar   div   

原文地址:http://www.cnblogs.com/zhangying/p/3930769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!