NOJ1553---Beautiful Walls(尺取法)

问题描述
To simplify the building process, XadillaX built some template on the ground. The template is a very big wall and the height of each unit may be different.

8Mao and Hungar have to choose any part of this wall as their own wall.

The part(i, j) means the wall between unit(i) and unit(j) with their heights.
What Hungar thinks a beautiful wall is that the height of each unit is unique.
Now give you a wall-template, you should tell Hungar that how many ways he can choose to copy his own wall?
输入
This problem contains several cases, ends with EOF.
The first line of each case is one integer N (0 < N ≤ 100000) which indicates the side length (number of units) of the wall-template.
The second line contains N integers which indicate the height of each unit. (0 < height <= 100000)
输出
For each case, you should output the number of ways that Hungar can choose.
样例输入

5
3 4 5 5 2
3
1 2 3

样例输出

9
6

提示

来源

cjl

操作

Wine93出的一道题 我是用尺取做的,只要记录每个数上一次出现的位置就行了

/*************************************************************************
    > File Name: NOJ1553.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月16日 星期四 19时36分28秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 100100;
int height[N];
int last[N];
int _hash[N];

int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        memset(last, -1, sizeof(last));
        memset(_hash, -1, sizeof(_hash));
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &height[i]);
            int x = _hash[height[i]];
            last[i] = x;
            _hash[height[i]] = i;
        }
        LL ans = 0;
        int l = 1, r = 1, len = 0;
        while (1)
        {
            while (r <= n && last[r] < l)
            {
                ++len;
                ++r;
            }
            ans += len;
            --len;
            ++l;
            if (l > r)
            {
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值