Data Structure?
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2133 Accepted Submission(s): 682
Problem Description
Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like problem for you.
Original, there are N numbers, namely 1, 2, 3...N. Each round, iSea find out the Ki-th smallest number and take it away, your task is reporting him the total sum of the numbers he has taken away.
Original, there are N numbers, namely 1, 2, 3...N. Each round, iSea find out the Ki-th smallest number and take it away, your task is reporting him the total sum of the numbers he has taken away.
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case includes two integers N, K, K indicates the round numbers. Then a line with K numbers following, indicating in i (1-based) round, iSea take away the Ki-th smallest away.
Technical Specification
1. 1 <= T <= 128
2. 1 <= K <= N <= 262 144
3. 1 <= Ki <= N - i + 1
Each test case includes two integers N, K, K indicates the round numbers. Then a line with K numbers following, indicating in i (1-based) round, iSea take away the Ki-th smallest away.
Technical Specification
1. 1 <= T <= 128
2. 1 <= K <= N <= 262 144
3. 1 <= Ki <= N - i + 1
Output
For each test case, output the case number first, then the sum.
Sample Input
2 3 2 1 1 10 3 3 9 1
Sample Output
Case 1: 3 Case 2: 14
//long过不了,WA
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
private static Tree f[];
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int N=input.nextInt();
int e=0;
while(e++<N){
int n=input.nextInt();
int m=input.nextInt();
f=new Tree[n*4];
BuildTree(1,1,n);
BigInteger result=BigInteger.ZERO;//long过不了
for(int i=1;i<=m;i++){
int num=input.nextInt();
result=result.add(Show(1,num));
}
System.out.println("Case "+e+": "+result);
}
}
private static BigInteger Show(int n, int num) {
--f[n].len;
if(f[n].left==f[n].right){
return BigInteger.valueOf(f[n].left);
}
int p=n<<1;
if(num<=f[p].len){
return Show(p,num);
}
else{
return Show(p+1,num-f[p].len);
}
}
private static void BuildTree(int n, int left, int right) {
f[n]=new Tree(left,right,right-left+1,(left+right)>>1);
if(left==right)
return;
int num=n<<1;
BuildTree(num,left,f[n].mid);
BuildTree(num+1,f[n].mid+1,right);
}
}
class Tree{
int left,right,len,mid;
public Tree(int left, int right, int len, int mid) {
this.left = left;
this.right = right;
this.len = len;
this.mid = mid;
}
}
//long过不了,9007800 2013-08-19 19:23:06 Accepted 4217 8390MS 32508K 1459 B Java zhangyi
import java.util.Scanner;
public class Main{
private static Tree f[];
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int N=input.nextInt();
int e=0;
while(e++<N){
int n=input.nextInt();
int m=input.nextInt();
f=new Tree[n*4];
BuildTree(1,1,n);
double result=0;
for(int i=1;i<=m;i++){
int num=input.nextInt();
result+=Show(1,num);
}
System.out.println("Case "+e+": "+String.format("%.0f", result));
}
}
private static double Show(int n, int num) {
--f[n].len;
if(f[n].left==f[n].right){
return f[n].left;
}
int p=n<<1;
if(num<=f[p].len){
return Show(p,num);
}
else{
return Show(p+1,num-f[p].len);
}
}
private static void BuildTree(int n, int left, int right) {
f[n]=new Tree(left,right,right-left+1,(left+right)>>1);
if(left==right)
return;
int num=n<<1;
BuildTree(num,left,f[n].mid);
BuildTree(num+1,f[n].mid+1,right);
}
}
class Tree{
int left,right,len,mid;
public Tree(int left, int right, int len, int mid) {
this.left = left;
this.right = right;
this.len = len;
this.mid = mid;
}
}
import java.util.LinkedList;
import java.util.Scanner;
public class Main{//Time Limit Exceeded 4217 10000MS 23284K 534 B Java zhangyi
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int N=input.nextInt();
for(int j=1;j<=N;j++){
int n=input.nextInt();
LinkedList<Integer> list=new LinkedList<Integer>();
for(int i=1;i<=n;i++){
list.add(i);
}
int m=input.nextInt();
long sum=0;
for(int i=0;i<m;i++){
int a=input.nextInt();
sum+=list.remove(a-1);
}
System.out.println("Case "+j+": "+sum);
}
}
}