Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import java.util.Scanner; | ||
import java.util.Vector; | ||
|
||
class Main { | ||
public static void main(String[] args) { | ||
(new UVa1241()).solve(); | ||
} | ||
} | ||
|
||
class Tournament { | ||
private int competitors_num, matches[]; | ||
|
||
Tournament(int competitors_num, Vector<Integer> withdrawn) { | ||
this.competitors_num = competitors_num; | ||
matches = new int[(int)Math.pow(2, competitors_num)]; | ||
for (int i = 0, sz = (int)Math.pow(2, competitors_num); i < sz; i++) | ||
if (withdrawn.contains(i+1)) | ||
matches[i] = -1; | ||
else | ||
matches[i] = i + 1; | ||
} | ||
|
||
public int countWO() { | ||
int result = 0; | ||
|
||
while (competitors_num > 0) { | ||
int competitors_left = (int)Math.pow(2, competitors_num); | ||
|
||
for (int i = 0; i < competitors_left; i+=2) { | ||
if (matches[i] != -1 && matches[i+1] != -1) { | ||
matches[i/2] = matches[i]; | ||
} else if (matches[i] == -1 && matches[i+1] != -1) { | ||
result++; | ||
matches[i/2] = matches[i+1]; | ||
} else if (matches[i] != -1 && matches[i+1] == -1) { | ||
result++; | ||
matches[i/2] = matches[i]; | ||
} else { | ||
matches[i/2] = -1; | ||
} | ||
} | ||
|
||
competitors_num--; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
class UVa1241 { | ||
private int n, m, test_cases, tmp; | ||
final private Scanner sc = new Scanner(System.in); | ||
|
||
public void solve() { | ||
test_cases = sc.nextInt(); | ||
|
||
while (test_cases-- > 0) { | ||
n = sc.nextInt(); | ||
m = sc.nextInt(); | ||
|
||
Vector<Integer> withdrawn = new Vector<Integer>(); | ||
|
||
while (m-- > 0) { | ||
tmp = sc.nextInt(); | ||
withdrawn.add(new Integer(tmp)); | ||
} | ||
|
||
Tournament t = new Tournament(n, withdrawn); | ||
System.out.println(t.countWO()); | ||
} | ||
} | ||
} |