最初有N
的學生都有0分。現在我們在每個查詢中給出Q
查詢,我們增加A index students by B
的標記。現在重新安排學生我增加排名即最高分將是一個,第二個最高第二個等等......
對於每個學生,他或她的地方等於擁有更多分數的學生人數,增加1。用查詢更新數組
讓我們將hash of participant
定義爲他/她的位置和點數的乘積。每次查詢後,發現參與者
例如哈希,sum
:
N=4 Q=4
3 100
2 60
1 80
2 20
1 20
After the first query, participant 3 has 100 and is 1, with hash equal to
100. The sum of hashes is 100+0+0+0=100
After the second query, participant 2 has the second place with 60 and hash
120, in total 100+120+0+0 = 220
After the third query, the Rank looks like follows:
(100,80,60,0). The sum of hashes is 200+160+180+0= 440
In the fourth query participant the rank is (100,80,80,0) then, with the sum
100⋅1+80⋅2+80⋅2+0⋅4=420.
我們怎樣纔能有效的一個簡單的計算策略做的是在這裏找到索引並替換:
while(Q>0){
Q--;
int a = in.nextInt()-1;
long b = in.nextInt();
if(score.size()==0){
score.add(b);
A[a]=b;
System.out.println(b);
}else{
int index =-1;
if(A[a]!=0){
int s =0;
int e = score.size()-1;
while(s<=e){
int mid = (s+e)/2;
if(score.get(mid)==A[a]){
index = mid;
break;
}else if(score.get(mid)>A[a]) s = mid+1;
else e = mid-1;
}
}
A[a]+=b;
int replace= score.size();
int s =0;
int e = score.size()-1;
while(s<=e){
int mid = (s+e)/2;
if(score.get(mid)>A[a]) s = mid+1;
else{
replace = mid;
e = mid-1;
}
}
score.add(replace,A[a]);
if(index!=-1) score.remove(index+1);
long o= score.get(0);
long prev =1;
for(int i=1;i<score.size();i++){
if(score.get(i)!=score.get(i-1)){
prev =i+1;
o+=score.get(i)*prev;
}else o+=score.get(i)*(prev);
}
System.out.println(o);
}
}
如何我們可以通過使用分段樹或任何其他數據結構來更有效地實現它。
你可以用一個例子來解釋..... – user6250837
@ user6250837我已經添加了一個例子,希望它更清楚。 – Sorin