Ping pong

 Ping pong

Description

Download as PDF

N(3$ \le$N$ \le$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 two 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 have to walk to the referee's house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the 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$ \le$T$ \le$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 N distinct integers a1a2...aN follow, indicating the skill rank of each player, in the order of west to east ( 1$ \le$ai$ \le$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


思路:  采用前缀和,后缀和得到结果
        再求前缀和,后缀和的时候后用树状数组算法得出来。
       
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,a[20005],c[20005],d[20005];
int e[200005]; 
inline int lowbit(int x)
{
  return x&(-x);//2^k
}

int sum(int x) //求前 N项的和  
  {
    int r=0;
	while(x>0)
	{
	 r+=e[x];
	 x-=lowbit(x);
	}
	return r;
  }
void add(int x,int d)//求前 N项sum的和  
  {
    while(x<=100000)
	{
	  e[x]+=d;x+=lowbit(x);
	}
  }


int main()
{
  int t,i;
  scanf("%d",&t);
  while(t--)
  {
    scanf("%d",&n);
    memset(a,0,sizeof(a));
    memset(c,0,sizeof(c));
    memset(d,0,sizeof(d));
    memset(e,0,sizeof(e));
	for(i=1;i<=n;i++)
	  scanf("%d",&a[i]);
	for(i=1;i<=n;i++)//前缀和 
	{
	  add(a[i],1);//求前 N项sum的和  
	  c[i]=sum(a[i]-1);//求前 N项的和  
	}
      memset(e,0,sizeof(e));
	for(i=n;i>=1;i--)//后缀和 
	{
	  add(a[i],1);
	  d[i]=sum(a[i]-1);
	}
	long long ans=0;
	for(i=2;i<n;i++)
		ans+=(long long)c[i]*(n-i-d[i])+(long long)d[i]*(i-1-c[i]);
	printf("%lld\n",ans);
  }
  return 0;
}


 
 
### 关于PingPong模式在IT领域中的应用 #### TCP协议中的ACK PingPong交互模式 在TCP协议中,ACK的pingpong模式是一种优化机制,旨在减少不必要的单独ACK报文发送。当TCP连接两端的数据传输呈现双向交替特性时,即数据在一个方向上传输后立即跟随另一个方向上的响应数据,则可以启用此模式来提高效率[^1]。 具体而言,在接收到对端发出的`FIN`信号后,如果本地套接字的状态仍处于`TCP_SYN_RECV`或`TCP_ESTABLISHED`阶段,则会激活ACK的pingpong模式。这是因为对方已停止接受新数据输入的情况下,无需再额外发送独立的ACK确认消息回传给源节点[^2]。 以下是实现该逻辑的部分C语言代码片段展示: ```c void tcp_fin(struct sock *sk) { struct tcp_sock *tp = tcp_sk(sk); inet_csk_schedule_ack(sk); sk->sk_shutdown |= RCV_SHUTDOWN; sock_set_flag(sk, SOCK_DONE); switch (sk->sk_state) { case TCP_SYN_RECV: case TCP_ESTABLISHED: tcp_set_state(sk, TCP_CLOSE_WAIT); /* 转移到CLOSE_WAIT */ inet_csk(sk)->icsk_ack.pingpong = 1; // 开启pingpong标志位 break; } } ``` #### Android WebSocket中的Ping-Pong机制 除了在网络层面上的应用外,更高层次通信协议如WebSocket也引入了类似的ping-pong概念用于保持链接活跃状态以及检测远端存活情况。例如,在某些Android应用程序开发场景下,服务器可能会定期向客户端发起ping请求;而一旦客户端捕获到此类事件便会自动生成对应的pong回应动作完成整个循环过程[^3]。 下面是关于如何通过覆盖方法来自定义处理这些特定帧类型的Java示例程序清单之一: ```java @Override public void onWebsocketPing(WebSocket conn, Framedata f) { conn.sendFrame(new PongFrame((PingFrame)f)); // 自动回复pong帧 } /** * 此默认实现在不采取任何行动的前提下允许重写它. * * @see org.java_websocket.WebSocketListener#onWebsocketPong(WebSocket, Framedata) */ @Override public void onWebsocketPong(WebSocket conn, Framedata f) { // 可在此处添加自定义行为 } ``` 尽管名称相同,“PingPong”在这里并不涉及实际的游戏玩法或是体育运动项目本身,而是借用这一术语形象化描述了一种技术现象——即两方之间快速来回交换短小精悍的信息包以维持某种形式的有效互动关系。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值