zoukankan      html  css  js  c++  java
  • 【33.20%】【LA 4320】【Ping pong】

    【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. Iftwo players want to compete, they must choose a referee among other ping pong players and hold thegame in the referee’s house. For some reason, the contestants can’t choose a referee whose skill rank ishigher or lower than both of theirs. The contestants have to walk to the referee’s house, and becausethey are lazy, they want to make their total walking distance no more than the distance between theirhouses. Of course all players live in different houses and the position of their houses are all different. Ifthe referee or any of the two contestants is different, we call two games different. Now is the problem:how many different games can be held in this ping pong street?

    【Input】

    The first line of the input contains an integer T (1 ≤ T ≤ 20), indicating the number of test cases,followed by T lines each of which describes a test case.Every test case consists of N + 1 integers. The first integer is N, the number of players. Then Ndistinct integers a1, a2 . . . aN follow, indicating the skill rank of each 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, the total number of different games.

    【Sample Input】

    1

    3 1 2 3

    【Sample Output】

    1


    【题解】

    就是说要进行乒乓球比赛。

    必须两个player和一个裁判。

    然后裁判的能力值和住的地方都在两个players之间。

    然后每个人住的房间在一条由西到东的笔直街道上。

    每个人的能力值都不一样。

    每个人都能当裁判。

    设c[i]表示前i个人里面有几个人的能力值比第i个人小。

    设d[i]表示i+1..n这些人里面有几个人的能力值比第i个人小。

    i-1-ci 就是前i个人里面比第i个人的能力值大的人的个数

    n-i-di就是i+1..n这些人里面比第i个人的能力值大的人的个数。

    则答案就是

    ∑ ci*(n-i-di) + (i-ci-1)*di

    因为ai不大最多为10W.

    则用树状数组处理出ci和di即可。

    然后ci是从前往后。di是从后往前。

    LA不支持%I64d的输出

    【代码】

    #include <cstdio>
    #include <cstring>
    
    const int MAXN = 101000;
    
    int n,a[MAXN],bmi[MAXN],ami[MAXN],c[MAXN],d[MAXN];
    long long ans;
    
    void init()
    {
    	memset(bmi, 0, sizeof(bmi));
    	memset(ami, 0, sizeof(ami));
    	ans = 0;
    }
    
    int lowbit(int x)
    {
    	return x & (-x);
    }
    
    void input_data()
    {
    	scanf("%d", &n);
    	for (int i = 1; i <= n; i++)
    		scanf("%d", &a[i]);
    	for (int i = 1; i <= n; i++)
    	{
    		int leijia = 0;
    		int temp = a[i]; //累加a[i]的前缀和
    		while (temp > 0)
    		{
    			leijia += bmi[temp];
    			temp -= lowbit(temp);
    		}
    		c[i] = leijia;
    		temp = a[i];
    		while (temp <= 100000)
    		{
    			bmi[temp]++; //只加1
    			temp += lowbit(temp);
    		}
    	}
    	for (int i = n; i >= 1; i--)//di与ci处理相同
    	{
    		int leijia = 0;
    		int temp = a[i];
    		while (temp > 0)
    		{
    			leijia += ami[temp];
    			temp -= lowbit(temp);
    		}
    		d[i] = leijia;
    		temp = a[i];
    		while (temp <= 100000)
    		{
    			ami[temp]++;
    			temp += lowbit(temp);
    		}
    	}
    }
    
    void get_ans()
    {
    	for (int i = 2; i <= n - 1; i++)
    	{
    		ans += (long long) c[i] * (n - i - d[i]);
    		ans += (long long ) (i - 1 - c[i]) * d[i];
    	}
    }
    
    void output_ans()
    {
    	printf("%lld
    ", ans);
    }
    
    int main()
    {
    	//freopen("F:\rush.txt", "r", stdin);
    	int T;
    	scanf("%d", &T);
    	while (T--)
    	{
    		init();
    		input_data();
    		get_ans();
    		output_ans();
    	}
    	return 0;
    }



  • 相关阅读:
    WCF基础 (续 更多关于配置文件的内容)
    WCF基础 (续 暴露元数据交换节点)
    简单的asp.net文件上传类
    根据年份月份,获得此月份的所有日期[转]
    JS 设为首页/加入收藏
    WCF基础 (续 使用代码生成WCF服务)
    WPF自定义标题栏——窗口移动和按钮状态转换[转]
    WCF元数据交换
    WCF基础 (续 为一个ASMX服务实现一个WCF客户端) 完结
    WCF基础 (续 通过代码和配置文件写一个WCF服务)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632256.html
Copyright © 2011-2022 走看看