Beijing 2008/uva 1428/poj 3928/hdu 2492 Ping pong (tree-like array)

Source: Internet
Author: User
Tags time limit
Ping Pong
http://poj.org/problem?id=3928, http://acm.hdu.edu.cn/showproblem.php?pid=2492, http://uva.onlinejudge.org/ index.php?option=com_onlinejudge&itemid=8&category=501&page=show_problem&problem=4174

Time limit:1000ms
Memory limit:65536k

Description N (3<=n<=20000) Ping pong players live along a West-east street (consider the street as a line segment) . Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If-Players want to compete, they must choose a referee among other ping pong players and hold the game in the referee ' S house. For some reason, the contestants can ' t choose a referee whose skill rank is higher or lower than both of theirs. The contestants has to walk to the referee's house, and because they is lazy, they want to make their total walking dist Ance no more than the distance between their houses. Of course all players live in different houses and the position of their houses is all different. If the referee or any of the contestants are different, we call both games different. Now are the problem:how many different games can being held in this ping Pong street?

Input the first line of the input contains a integer T (1<=t<=20), indicating the number of test cases, followed by T lines Each of the which describes a test case.
Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1, A2 ... an follow, indicating the skill rank of all player, in the order of West to east. (1 <= AI <= 100000, i = 1 ... N).

Output for each test case, output a single line contains an integer with the total number of different games.

Sample Input

1 
3 1) 2 3

Sample Output

1


Test instructions Abstract: Known quantity column {an} (3<=n<=20000), take three different numbers from left to right (can be nonadjacent), so that the three numbers are sorted in ascending or descending order.
Solution: Complexity:
Idea: Maintaining a tree-like array number of values per competency。 1. For AI (1<i<n), on the left or right side and less than its number ll[i] and rl[i].
const int MAXN = 20002;
int A[MAXN], LL[MAXN], rl[maxn];//leftlower,rightlower

scanf ("%d", &a[1]);
Update (a[1], 1);
for (i = 2; i < n; i++)
{
    scanf ("%d", &a[i]);
    Update (A[i], 1);
    Ll[i] = SUM (a[i]-1);
}
scanf ("%d", &a[n]);

Notice here to empty
the tree memset (rank, 0, sizeof (rank));
Update (A[n], 1);
for (i = n-1; i > 1; i--)
{
    update (a[i], 1);
    Rl[i] = SUM (a[i]-1);
}

2. Note that when the sequence {an} is the case of incrementing or decreasing a sequence, the number of the N-2 is maximum, (N-1) N/6, the data is the most 10^15, so a long long is used here. calculate σ (i=2,n-1) ll[i] * (N-i-rl[i]) + (I-1-ll[i]) * rl[i], done.
A long long ans = 0;
for (i = 2; i < n; i++)
{
    ans + = ll[i] * (N-i-rl[i]) + (I-1-ll[i]) * rl[i];
}

Full code:

/*uva:0.269s*//*poj:235ms,792kb*//*hdu:109ms,904kb*/#include <cstdio> #include <cstring> const int MAXN =
100002;

const int MAXN2 = 20002;

int RK[MAXN], a[maxn2], ll[maxn2], rl[maxn2];
		void Update (int a, int val) {while (a <= maxn) {Rk[a] + = val;
	A + = A & A;
	}} int sum (int a) {int sum = 0;
		while (a > 0) {sum + = Rk[a];
	A-=-A & A;
} return sum;
	} int main () {int i, t, N;
	Long long ans;
	scanf ("%d", &t);
		while (t--) {scanf ("%d", &n);
		memset (RK, 0, sizeof (RK));
		scanf ("%d", &a[1]);
		Update (a[1], 1);
			for (i = 2; i < n; i++) {scanf ("%d", &a[i]);
			Update (A[i], 1);
		Ll[i] = SUM (a[i]-1);
		} scanf ("%d", &a[n]); memset (RK, 0, sizeof (RK));
		Note here to empty the update (A[n], 1);
			for (i = n-1; i > 1; i--) {update (a[i], 1);
		Rl[i] = SUM (a[i]-1);
		} ans = 0;
		for (i = 2; i < n; i++) ans + = ll[i] * (N-i-rl[i]) + (I-1-ll[i]) * Rl[i];
	printf ("%lld\n", ans); } RETUrn 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.