洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day

P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    • 75通过
    • 153提交
  • 题目提供者洛谷OnlineJudge
  • 标签USACO2006云端
  • 难度普及/提高-
  • 时空限制1s / 128MB

提交  讨论  题解

最新讨论更多讨论

  • 题目标题

题目描述

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.

农民约翰的某N(1 < N < 80000)头奶牛正在过乱头发节!由于每头牛都意识到自己凌乱不堪 的发型,约翰希望统计出能够看到其他牛的头发的牛的数量.

每一头牛i有一个高度所有N头牛面向东方排成一排,牛N在最前面,而 牛1在最后面.第i头牛可以看到她前面的那些牛的头,只要那些牛的高度严格小于她的高度,而且 中间没有比hi高或相等的奶牛阻隔.

让N表示第i头牛可以看到发型的牛的数量;请输出Ci的总和

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1:

6
10
3
7
4
12
2

输出样例#1:

5分析:很显然是单调栈,至于单调栈的用法可以去看我的前几篇博客,这里计数器累加的top代表这只牛的头发能被前面的多少只牛看到(因为前面有top只牛比这只牛高).
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n,h[500010],stk[500010],top;
long long ans;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &h[i]);

    for (int i = 1; i <= n; i++)
    {
        while (top != 0 && stk[top] <= h[i])
            top--;
        ans += top;
        stk[++top] = h[i];
    }
    printf("%lld", ans);

    return 0;
}
				
时间: 2024-10-14 01:25:22

洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day的相关文章

洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day

P2866 [USACO06NOV]糟糕的一天Bad Hair DaY 题目描述 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 cow

P2866 [USACO06NOV]糟糕的一天Bad Hair Day

P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题目描述 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 cow

洛谷 P1879 [USACO06NOV]玉米田Corn Fields

P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the

POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks

http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15680   Accepted: 5510 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get

洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】题解+AC代码

题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't b

洛谷P1519 穿越栅栏 Overfencing

P1519 穿越栅栏 Overfencing 69通过 275提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 USACO是100分,洛谷是20分 为什么只有十分 题目描述 描述 农夫John在外面的田野上搭建了一个巨大的用栅栏围成的迷宫.幸运的是,他在迷宫的边界上留出了两段栅栏作为迷宫的出口.更幸运的是,他所建造的迷宫是一个“完美的”迷宫:即你能从迷宫中的任意一点找到一条走出迷宫的路.给定迷宫的宽度W(1<=W<=38)及高度H(1<=H&

洛谷P1198 [JSOI2008]最大数

P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都过了,洛谷竟然过不了… 为什么过不了 = =我想说这题加优读会WA?… 谁说pascal只能80,要换c++… 线段树为什么是80? 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超

【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线

P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另外一个公交站往往要换几次车,例如从公交站A到公交站D,你就至少需要换3次车. Tiger的方向感极其糟糕,我们知道从公交站A到公交E只需要换4次车就可以到达,可是tiger却总共换了n次车,注意tiger一旦到达公交站E,他不会愚蠢到再去换车.现在希望你计算一下tiger有多少种可能的乘车方案. 题

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3