NOJ1553 --- Beautiful Wils (ruler acquisition method)
Problem description
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 shoshould tell Hungar that how many ways he can choose to copy his own wall?
Input
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 Output
For each case, you shoshould output the number of ways that Hungar can choose.
Sample Input
5
3 4 5 5 2
3
1 2 3
Sample output
9
6
Prompt
None
Source
Cjl
Operation
I used a ruler to take a question from Wine93. I only need to record the last appearance position of each number.
/*************************************** * *********************************> File Name: NOJ1553.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: thursday, April 16, 2015, 28 seconds ******************************** **************************************** /# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
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 ;}