Skip to content

Commit

Permalink
upload 491 519 598PS
Browse files Browse the repository at this point in the history
  • Loading branch information
Hao Wang committed Dec 15, 2017
1 parent 3e6824c commit 992ae7e
Show file tree
Hide file tree
Showing 7,412 changed files with 80,942 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
35 changes: 35 additions & 0 deletions 491AdvCompetitiveAlgorithmProg/homework1/POJ1477.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String args[]) throws Exception
{
int counter = 1;
Scanner cin=new Scanner(System.in);
int stacks=cin.nextInt();
while(stacks!=0)
{
int[] numbers = new int[stacks];
int result = 0;
int sum = 0;
int avg = 0;
for (int i=0; i<stacks; i++)
{
numbers[i] = cin.nextInt();
sum = sum + numbers[i];
}
avg = sum/stacks;
for (int j=0; j<stacks; j++)
{
result = result + Math.abs(numbers[j]-avg);
}
result = result/2;

System.out.println("Set #"+counter);
System.out.println("The minimum number of moves is "+result+".\n");
counter = counter + 1;
stacks=cin.nextInt();
}
}
}
25 changes: 25 additions & 0 deletions 491AdvCompetitiveAlgorithmProg/homework1/POJ3030.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String args[]) throws Exception
{
Scanner cin=new Scanner(System.in);
int counter=cin.nextInt();
for (int i=0; i<counter; i++)
{
double r=cin.nextDouble();
double e=cin.nextDouble();
double c=cin.nextDouble();
if (r<(e-c))
{
System.out.println("advertise");
}else if (r>(e-c)) {
System.out.println("do not advertise");
}else{
System.out.println("does not matter");
}
}
}
}
43 changes: 43 additions & 0 deletions 491AdvCompetitiveAlgorithmProg/homework1/POJ3096.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String args[]) throws Exception
{
Scanner cin=new Scanner(System.in);
String data = cin.nextLine();
while (!data.equals("*"))
{
String[] parts = data.split("");
int length = parts.length;
boolean surprising = true;
for (int i=1; i<length; i++)
{
Map library = new HashMap();
for (int j=0; j<length-i; j++) {
if (library.containsKey(parts[j].concat(parts[j+i])))
{
surprising = false;
break;
}else
{
library.put(parts[j].concat(parts[j+i]),1);
}
}
if (surprising==false)
{
break;
}
}
if (surprising)
{
System.out.println(data+" is surprising.");
}else
{
System.out.println(data+" is NOT surprising.");
}
data = cin.nextLine();
}
}
}
Binary file not shown.
124 changes: 124 additions & 0 deletions 491AdvCompetitiveAlgorithmProg/homework11/UVA10480.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <cstdio>
#include <vector>
#include <utility>
#include <bitset>
#include <queue>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define INF 1e9
#define MAX_V 55

int res[MAX_V][MAX_V], mf, f, s, t;
vector<vi> AdjList;
vi p;

int n, m, cnt = 1;

bitset<MAX_V> dfsv;
set<int> SSet;

void dfs(int u) {
dfsv.set(u);
SSet.insert(u);
for (int i = 0; i < (int) AdjList[u].size(); i++) {
int v = AdjList[u][i];
if (!dfsv.test(v) && res[u][v] > 0) {
dfs(v);
}
}
}

void mincut() {
SSet.clear();
for (int i = 0; i < MAX_V; i++)
dfsv.reset();
dfs(s);
set<int>::iterator it;

for (it = SSet.begin(); it != SSet.end(); it++) {
int u = *it;
for (int i = 0; i < (int) AdjList[u].size(); i++) {
int v = AdjList[u][i];
if (SSet.count(v))
continue;
if (res[u][v] <= 0) {
printf("%d %d\n", u + 1, v + 1);
}
}
}
printf("\n");
}

void augment(int v, int minEdge) {
if (v == s) {
f = minEdge;
return;
} else if (p[v] != -1) {
augment(p[v], min(minEdge, res[p[v]][v]));
res[p[v]][v] -= f;
res[v][p[v]] += f;
}
}

void EdmondKarps() {
mf = 0;
while (1) {
f = 0;
bitset<MAX_V> visited;
visited.set(s);
queue<int> q;
q.push(s);
p.assign(MAX_V, -1);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == t)
break;
for (int i = 0; i < (int) AdjList[u].size(); i++) {
int v = AdjList[u][i];
if (res[u][v] > 0 && !visited.test(v)) {
visited.set(v);
q.push(v);
p[v] = u;
}
}
}
augment(t, INF);
if (f == 0)
break;
mf += f;
}
}

int main() {
while (scanf("%d %d", &n, &m), n || m) {
memset(res, 0, sizeof res);
AdjList.assign(n, vi());

s = 0;
t = 1;

for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
u--;
v--;
res[u][v] += w;
res[v][u] += w;
AdjList[u].push_back(v);
AdjList[v].push_back(u);
}

EdmondKarps();
mincut();
}

return 0;
}
34 changes: 34 additions & 0 deletions 491AdvCompetitiveAlgorithmProg/homework12/POJ3480.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.*;
import java.io.*;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int testcase = in.nextInt();
while (testcase-- > 0) {
int number = in.nextInt();
int xor = 0;
boolean allOnes = true;
for (int i = 1; i <= number; ++i) {
int temp = in.nextInt();
if (temp != 1) {
allOnes = false;
}
xor ^= temp;
}
if (allOnes) {
if (xor == 0) {
System.out.println("John");
} else {
System.out.println("Brother");
}
} else {
if (xor == 0) {
System.out.println("Brother");
} else {
System.out.println("John");
}
}
}
}
}
115 changes: 115 additions & 0 deletions 491AdvCompetitiveAlgorithmProg/homework13/UVA10181.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#define LLU unsigned long long
using namespace std;
struct status {
char board[4][4];
int ix, iy;
} init;
int pos[16][2], mxdep;
int dir[4][2] = {{0,-1},{-1,0},{1,0},{0,1}};/*u,l,r,d*/
char dirc[4] = {'L', 'U', 'D', 'R'}, path[100];
int solved;
bool solvable() {
int sum = 0, row, i, j;
for(i = 0; i < 16; i++) {
if(init.board[i/4][i%4] == 0) {
row = i/4 + 1;
continue;
}
for(j = i+1; j < 16; j++) {
if(init.board[j/4][j%4] < init.board[i/4][i%4]) {
if(init.board[j/4][j%4])
sum++;
}
}
}
return 1-(sum+row)%2;
}
int H() {
static int i, j, sum, num;
sum = 0;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
num = init.board[i][j];
if(num == 0)
continue;
sum += abs(i-pos[num][0]) + abs(j-pos[num][1]);
}
}
return sum;
}
int Htable[4][4][16];
int IDA(int dep, int hv, int prestep) {
if(hv == 0) {
solved = dep;
path[dep] = '\0';
puts(path);
return dep;
}
if(dep + 5*hv/3 > mxdep) {
return dep + 5*hv/3;
}
int i, tx, ty, x = init.ix, y = init.iy;
int submxdep = 0xfff, val = 0xfff, shv;

for(i = 0; i < 4; i++) {
if(i + prestep == 3) continue;
tx = x + dir[i][0], ty = y + dir[i][1];
if(tx < 0 || ty < 0 || tx > 3 || ty > 3)
continue;

shv = hv;
shv -= Htable[tx][ty][init.board[tx][ty]];
shv += Htable[x][y][init.board[tx][ty]];
init.ix = tx, init.iy = ty;
swap(init.board[x][y], init.board[tx][ty]);

path[dep] = dirc[i];
val = IDA(dep+1, shv, i);

swap(init.board[x][y], init.board[tx][ty]);
init.ix = x, init.iy = y;
if(solved) return solved;
submxdep = min(submxdep, val);
}
return submxdep;
}
int main() {
int test, i, j, k, initH;
int cases = 0;
for(i = 0, k = 0; i < 4; i++)
for(j = 0; j < 4; j++)
pos[++k][0] = i, pos[k][1] = j;
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
for(k = 1; k < 16; k++)
Htable[i][j][k] = abs(i - pos[k][0]) + abs(j - pos[k][1]);
scanf("%d", &test);
while(test--) {
cases++;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
scanf("%d", &k);
init.board[i][j] = k;
if(init.board[i][j] == 0) {
init.ix = i, init.iy = j;
}
}
}
if(solvable()) {
solved = 0, initH = mxdep = H();
if(!mxdep) {
puts("");
continue;
}
while(solved == 0)
mxdep = IDA(0, initH, -1);
//printf("%d\n", solved);
}else {
puts("This puzzle is not solvable.");
}
}
return 0;
}
Loading

0 comments on commit 992ae7e

Please sign in to comment.