0
点赞
收藏
分享

微信扫一扫

poj Bad Hair Day 3250 (栈++技巧) 好题


Bad Hair Day


Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 15936

 

Accepted: 5380


Description


Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads.

Each cow i has a specified height hi (1 ≤ hi ≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.

Consider this example:



= = = = - = Cows facing right --> = = = = - = = = = = = = = = 1 2 3 4 5 6



Cow#1 can see the hairstyle of cows #2, 3, 4
Cow#2 can see no cow's hairstyle
Cow#3 can see the hairstyle of cow #4
Cow#4 can see no cow's hairstyle
Cow#5 can see the hairstyle of cow 6
Cow#6 can see no cows at all!

Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.


Input


Line 1: The number of cows, N.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.


Output


Line 1: A single integer that is the sum of c 1 through cN.


Sample Input


6 10 3 7 4 12 2


Sample Output


5


//开始没想到栈,只想到了归并排序(还挺高兴的,认为肯定能过),但写了之后,自己的测试数据都过了,单提交后一直WA(不明所以)。看了大神代码,才知道用栈写。


//又编了几组数据发现是自己错了。。。。。还是不行啊。。


AC代码


#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
	stack<int>s;
	long long sum;
	int n,h,t;
	while(scanf("%d",&n)!=EOF)
	{
		sum=0;
		scanf("%d",&h);
		s.push(h);
		for(int i=1;i<n;i++)
		{
			scanf("%d",&t);
			while(!s.empty()&&t>=s.top())
				s.pop();
			sum+=s.size();
			s.push(t);
		}
		printf("%lld\n",sum);
		while(!s.empty())
			s.pop();
	}
	return 0;
}



//我开始写的归并排序(WA。。)


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define N 80010
using namespace std;
int a[N];
int cnt;
int b[N];
void merge(int a[],int s,int m,int e)
{
    int i=s,j=m+1,k=s;
    while(i<=m&&j<=e)
    {
        if(a[i]<=a[j])
            b[k++]=a[i++];
        else
        {
            cnt+=j-k;
            b[k++]=a[j++];
        }
    }
    while(i<=m)
        b[k++]=a[i++];
    while(j<=e)
        b[k++]=a[j++];
    for(i=s;i<=e;i++)
        a[i]=b[i];
}
void mergesort(int a[],int s,int e)
{
    if(s<e)
    {
        int m=(s+e)/2;
        mergesort(a,s,m);
        mergesort(a,m+1,e);
        merge(a,s,m,e);
    }
}
int main()
{
    int n,i,j,k,kk;
	long long sum,ms;
    while(scanf("%d",&n)!=EOF)
    {
    	ms=0;k=0;sum=0;
    	memset(a,0,sizeof(a));
        for(i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
            ms=a[k];
			if(ms<=a[i])
			{
				kk=i;
				cnt=0;
				mergesort(a,k,kk-1);
				sum+=cnt;
				k=kk;
			}
        }      
        cnt=0;
		mergesort(a,k,n-1);
		sum+=cnt;
        printf("%lld\n",sum);
    }
    return 0;
}


举报

相关推荐

0 条评论