zoukankan      html  css  js  c++  java
  • [USACO06NOV]糟糕的一天Bad Hair Day BZOJ 1660 单调栈

    农民John的某 N 头奶牛 (1 <= N <= 80,000) 正在过乱头发节!由于每头牛都 意识到自己凌乱不堪的发型, FJ 希望统计出能够看到其他牛的头发的牛的数量。 每一头牛 i有一个高度 h[i] (1 <= h[i] <= 1,000,000,000)而且面向东方排成 一排(在我们的图中是向右)。因此,第i头牛可以看到她前面的那些牛的头, (即i+1, i+2,等等),只要那些牛的高度严格小于她的高度。 例如这个例子: = = = = = = - = 牛面向右侧 --> = = = = - = = = = = = = = = 1 2 3 4 5 6 牛#1 可以看到她们的发型 #2, 3, 4 牛#2 不能看到任何牛的发型 牛#3 可以看到她的发型 #4 牛#4 不能看到任何牛的发型 牛#5 可以看到她的发型 6 牛#6 不能看到任何牛的发型! 让 c[i] 表示第i头牛可以看到发型的牛的数量;请输出 c[1] 至 c[N]的和。 如上面的这个例子,正确解是3 + 0 + 1 + 0 + 1 + 0 = 5。
    Input
    * Line 1: 牛的数量 N。 * Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度。
    Output
    * Line 1: 一个整数表示c[1] 至 c[N]的和。
    Sample Input

    6
    10
    3
    7
    4
    12
    2


    输入解释:

    六头牛排成一排,高度依次是 10, 3, 7, 4, 12, 2。

    Sample Output
    5
    我们用单调栈去维护它;
    读入完毕后,我们从后往前遍历;
    用 sum 去维护一个后缀和;
    这是基于这样的一个思想,如果我在你后面而且我比你高,那么你能看见的我也能看见;
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize("O3")
    using namespace std;
    #define maxn 200005
    #define inf 0x3f3f3f3f
    #define INF 9999999999
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-3
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    inline ll rd() {
    	ll x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    ll sqr(ll x) { return x * x; }
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    
    
    ll qpow(ll a, ll b, ll c) {
    	ll ans = 1;
    	a = a % c;
    	while (b) {
    		if (b % 2)ans = ans * a%c;
    		b /= 2; a = a * a%c;
    	}
    	return ans;
    }
    
    int a[maxn];
    int sum[maxn];
    ll ans;
    int sk[maxn];
    int main()
    {
    	//ios::sync_with_stdio(0);
    	int n; rdint(n); int top = 0;
    	for (int i = 1; i <= n; i++)rdint(a[i]);
    	for (int i = n; i >= 1; i--) {
    		while (top&&a[i] > a[sk[top]])sum[i] += sum[sk[top]] + 1, top--;
    		sk[++top] = i;
    	}
    	for (int i = 1; i <= n; i++)ans +=(ll) sum[i];
    	cout << ans << endl;
        return 0;
    }
    
    
    
    
    
    
    EPFL - Fighting
  • 相关阅读:
    redis实现分布式缓存
    redis持久化
    Redis五种数据类型
    Azure Digital Twins(1)-创建实例并设置角色
    Azure Digital Twins(2)- 在本地使用ADT Explorer 管理数字孪生
    Azure Digital Twins(3)- 数字孪生体和数字孪生图
    Azure + 5G + AI + IOT可以这么玩
    使用Azure Storage API 上传 文件解决微信小程序中上传图片的问题
    Azure入门(1)- Azure核心概念
    利用 Management Group 和Policy 控制Azure 指定资源的创建
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/9977016.html
Copyright © 2011-2022 走看看