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.pswp.cn/news/536330.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536330.shtml
英文地址,請注明出處:http://en.pswp.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…