Ping pong【树状数组】

Ping pong

 UVALive - 4329 

题目传送门

题目大意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术。每个人都有一个不同的技能值ai。每场比赛需要三个人:两名选手,一名裁判。他们有一个奇怪的规定,即裁判必须住在两名选手的中间,并且技能值也在两名选手之间。问一共能组织多少场比赛。输入第一行表示共有T组测试数据,每组数据占一行,先输入整数n(3<=n<=20000),后面跟着输入n个不同的整数,即a1,a2,a3...an(1<=ai<=100000),按照从左到右的顺序给出每个乒乓球爱好者的技能值。

解决方法:树状数组解决,考虑第i个人当裁判的情况。假设a1到ai-1中有ci个比ai小,那么就有(i-1)-ci个比ai大;同理,假设a(i+1)到an中有di个比ai小,则其中就有n-i-di个比其大,所以以ai为裁判的比赛场数为:ci*(n-i-di)+di*(i-1-ci),因此先用树状数组求每个数前面比其小的数字的数目,再反向求其后面比它小的数目,即可得到答案。

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e6 + 5;
const ll mod = 1e9+7;
int C1[maxn];   
int C2[maxn];  
int arr[maxn];   //存技能值
int sum11[maxn];     //存的每个数前面的比其小的数目
int sum22[maxn];     //存的每个数后面比其小的数目
int n;
int lowbit(int x)
{return x&(-x);
}
void add1(int x,int d)
{while(x<=100000){C1[x]+=d;x+=lowbit(x);}
}
int sum1(int x)
{int ret=0;while(x>0){ret+=C1[x];x-=lowbit(x);}return ret;
}
void add2(int x,int d)
{while(x<=100000){C2[x]+=d;x+=lowbit(x);}
}
int sum2(int x)
{int ret=0;while(x>0){ret+=C2[x];x-=lowbit(x);}return ret;
}
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int T;cin>>T;while(T--){ms(C1);ms(C2);ms(arr);ms(sum11);ms(sum22);cin>>n;rep(i,1,n) {cin>>arr[i];add1(arr[i],1);sum11[i]=sum1(arr[i]-1);}lep(i,n,1) {add2(arr[i],1);sum22[i]=sum2(arr[i]-1);}ll ans=0;rep(i,1,n) {ll c1=sum11[i];ll d1=sum22[i];ans+=c1*(n-i-d1)+d1*(i-1-c1);}cout<<ans<<endl;}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536330.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Frequent values【线段树】

Frequent values UVA - 11235 题目传送门 题目大意&#xff1a;给出一个非降序的整数数组a1,a2,a3...an&#xff0c;你的任务是对一系列的询问&#xff08;i,j&#xff09;&#xff0c;回答ai,ai1,ai2...aj中出现次数最多的值所出现的次数。输入包括多组数据。每组数据第一行…

求二叉树节点个数、叶子节点、节点层次与宽度

需实现&#xff1a;&#xff08;1&#xff09;输出二叉树b的节点个数 &#xff08;2&#xff09;输出二叉树b的叶子节点个数 &#xff08;3&#xff09;求二叉树b中指定节点值&#xff08;假设所有节点值不同&#xff09;的节点的层次。 &#xff08;4&#xff09;利用层次遍历…

UVA - 227 Puzzle

Puzzle UVA - 227 题目传送门 注意点&#xff1a;每两个输出点间有一个换行&#xff0c;但最后一个输出无换行 恶心模拟题&#xff0c;很卡输入输出&#xff01;&#xff01;&#xff01; AC代码1:(自己的代码&#xff0c;提交时需要选择C11) #include <cstdio> #i…

UVA - 232 ​​​​​​​Crossword Answers

Crossword Answers UVA - 232 题目传送门 直接按照要求寻找遍历一遍即可 AC代码&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <m…

UVA - 1368 ​​​​​​​DNA Consensus String

DNA Consensus String UVA - 1368 题目传送门 解决方法&#xff1a;寻找每列中出现最多的字母。 AC代码 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #inc…

UVA - 202 Repeating Decimals

Repeating Decimals UVA - 202 题目传送门 解决方法&#xff1a;模拟一下除法&#xff0c;及时记录余数&#xff0c;当一个余数第二次出现时证明开始循环 AC代码 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #…

UVA - 10340 ​​​​​​​All in All

All in All UVA - 10340 题目传送门 将两个字符串对比一下即可。 AC代码&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> …

UVA - 1587 ​​​​​​​Box

Box UVA - 1587 题目传送门 解决方法&#xff1a;按照边在12个长宽出现的次数和出现在几个矩形里来判定就行了 总共出现一个长度&#xff0c;满足条件 总共出现两个长度&#xff0c;则其中一个长度在12个数里出现4次&#xff0c;并在四个矩形中出现 总共出现三个长度&#x…

UVA - 1588 ​​​​​​​Kickdown

Kickdown UVA - 1588 题目传送门 解决方法&#xff1a;上板不动&#xff0c;下板向左移&#xff1b;上板不动&#xff0c;下板向右移。 AC代码&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #inclu…

UVA - 1339 ​​​​​​​Ancient Cipher

Ancient Cipher UVA - 1339 题目传送门 解决方法&#xff1a;模拟一下转换过程即可。 AC代码&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #i…

UVA - 489 ​​​​​​​Hangman Judge

Hangman Judge UVA - 489 题目传送门 PS.此题Udebug有毒&#xff0c;即使100组样例全过&#xff0c;但还是WA&#xff0c;心塞。 这是我自己的代码&#xff0c;悲催的WA了 #include <cstdio> #include <iostream> #include <algorithm> #include <cm…

UVA - 133 ​​​​​​​The Dole Queue

The Dole Queue UVA - 133 题目传送门 模拟一遍过程&#xff0c;注&#xff1a;可能会选中同一个人 AC代码&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <c…

UVA - 213 Message Decoding

Message Decoding UVA - 213 题目传送门 emmmm&#xff0c;此题按照紫书上的思路来即可&#xff0c;要么太复杂 AC代码&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #in…

UVA - 512 ​​​​​​​Spreadsheet Tracking

Spreadsheet Tracking UVA - 512 题目传送门 紫书第二个思路十分巧妙&#xff0c;能用很少的代码解出此题。 AC代码&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #inclu…

UVA - 1589 ​​​​​​​Xiangqi

Xiangqi UVA - 1589 题目传送门 解决方法&#xff1a;判断黑棋是否能有可以下的地方 AC代码&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #in…

UVA - 12412 ​​​​​​​A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

A Typical Homework (a.k.a Shi Xiong Bang Bang Mang) UVA - 12412 题目传送门 emmmm&#xff0c;不想表达什么&#xff0c;udbug上的数据全过&#xff0c;可就是WA。。。。 AC了的代码&#xff08;大佬的代码&#xff09; #include <bits/stdc.h> using namespace…

【思维】draw!

题目&#xff1a; You still have partial information about the score during the historic football match. You are given a set of pairs (ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai: bibi". It is known that if t…

【数学】Birthday

题目&#xff1a; Cowboy Vlad has a birthday today! There are nn children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a…

【递推】Ayoub and Lost Array

题目&#xff1a;Ayoub had an array aa of integers of size nn and this array had two interesting properties: All the integers in the array were between ll and rr (inclusive). The sum of all the elements was divisible by 33. Unfortunately, Ayoub has lost hi…

Super-palindrome【字符串+思维】

Super-palindrome 时间限制: 1 Sec 内存限制: 128 MB 提交: 595 解决: 231 [提交] [状态] [命题人:admin] 题目描述 You are given a string that is consisted of lowercase English alphabet. You are supposed to change it into a super-palindrome string in minimum ste…