HOJ2275 Number sequence

Number sequence

My Tags tag=&type=or" style="margin:0px; padding:0px; color:rgb(27,87,177); text-decoration:none">  (Edit)
  Source : 

id=SCU%20Programming%20Contest%202006%20Final" style="margin:0px; padding:0px; color:rgb(27,87,177); text-decoration:none">SCU Programming
Contest 2006 Final

  Time limit : 1 sec   Memory limit : 64 M

Submitted : 1632, Accepted : 440

Given a number sequence which has N element(s), please calculate the number of different collocation for three number Ai, Aj, Ak, which satisfy that Ai < Aj > Ak and i < j < k.

Input

The first line is an integer N (N <= 50000). The second line contains N integer(s): A1, A2, ..., An(0 <= Ai <= 32768).

Output

There is only one number, which is the the number of different collocation.

Sample Input

5
1 2 3 4 1

Sample Output

6

这题能够用树状数组做,开两个一维的树状数组分别记录当前点前面的比这点小的个数和后面比这点大的个数。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 50005
#define ll long long
int b1[maxn],b2[maxn],a[maxn];
int lowbit(int x){
	return x&(-x);
}
void update1(int pos,int num)
{
	while(pos<=maxn){
		b1[pos]+=num;pos+=lowbit(pos);
	}
}
int getsum1(int pos)
{
	int num=0;
	while(pos>0){
		num+=b1[pos];pos-=lowbit(pos);
	}
	return num;
}

void update2(int pos,int num)
{
	while(pos<=maxn){
		b2[pos]+=num;pos+=lowbit(pos);
	}
}
int getsum2(int pos)
{
	int num=0;
	while(pos>0){
		num+=b2[pos];pos-=lowbit(pos);
	}
	return num;
}

int main()
{
	int n,m,i,j;
	ll num=0;
	while(scanf("%d",&n)!=EOF)
	{
		memset(b1,0,sizeof(b1));
		memset(b2,0,sizeof(b2));
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
			a[i]++;
			if(i==1){
				update1(a[i],1);continue;
			}
			update2(a[i],1);
		}
		num=0;
		for(i=2;i<=n-1;i++){
			num+=getsum1(a[i]-1)*getsum2(a[i]-1);
			update1(a[i],1);
			update2(a[i],-1);
		}
		printf("%lld\n",num);
	}
	return 0;
}
时间: 2024-10-11 06:23:11

HOJ2275 Number sequence的相关文章

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

TOJ 1203: Number Sequence

1203: Number Sequence Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 957            Accepted:208 Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Gi

ACM—Number Sequence(HDOJ1005)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 主要内容: A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). 看到这样的公式很容易想到递归调用求解,但是在本题中n的取

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j ( i

poj 1019 Number Sequence 二分

Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 34391   Accepted: 9879 Description A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...

HDU - 5014 Number Sequence

Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows("⊕" deno