Programming Challenge -PKU-
http://w.atwiki.jp/azounoman/
Programming Challenge -PKU-
ja
2006-06-05T00:38:12+09:00
1149435492
-
トップページ
https://w.atwiki.jp/azounoman/pages/1.html
Programming Challenges PKU Version. へようこそ!
PKU Online Judge の攻略記事を扱うサイトです.
いまのところ私が解いた問題の解答を集めた[[全掲載問題リスト]]がメインコンテンツです.いくつかの問題については解説も加えてあります.
Programming Challenges の PKU 版となることを目指しております.
申し訳ありませんが,管理人の都合によりしばらく更新を停止させていただきます.
2006-06-05T00:38:12+09:00
1149435492
-
全掲載問題リスト
https://w.atwiki.jp/azounoman/pages/4.html
[[1000 A+B Problem]]
[[1001 Exponentiation]]
[[1012 Joseph]]
[[1017 Packets]]
[[1026 Cipher]]
[[1039 Pipe]]
[[1040 Transportation]]
[[1041 John's trip]]
[[1049 Microprocessor Simulation]]
[[1050 To the Max]]
[[1125 Stockbroker Grapevine]]
[[1128 Frame Stacking]]
[[1129 Channel Allocation]]
[[1130 Alien Security]]
[[1131 Octal Fractions]]
[[1144 Network]]
[[1256 Anagram]]
[[1263 Reflections]]
[[1273 Drainage Ditches]]
[[1406 A Starship Hakodate-maru]]
[[1407 e-market]]
[[1408 Fishnet]]
[[1409 77377]]
[[1411 Calling Extraterrestrial Intelligence Again]]
[[1412 Equals are Equals]]
[[1413 GIGA Universe Cup]]
[[1414 Life Line]]
[[1415 Map of Ninja House]]
[[1416 Shredding Company]]
[[1417 True Liars]]
[[1418 Viva Confetti]]
[[1423 Big Number]]
[[1456 Supermarket]]
[[1458 Common Subsequence]]
[[1459 Power Network]]
[[1460 Firefighters]]
[[1480 Optimal Programs]]
[[1481 The Die Is Cast]]
[[1482 It's not a Bug, It's a Feature!]]
[[1484 Blowing Fuses]]
[[1485 Fast Food]]
[[1486 Sorting Slides]]
[[1602 Zip]]
[[1606 Jugs]]
[[1845 Sumdiv]]
[[1915 Knight Moves]]
[[1917 Automatic Poetry]]
[[1922 Ride to School]]
[[1923 Fouriers Lines]]
[[1957 Beehives]]
[[1980 Unit Fraction Partition]]
[[1981 Circle and Points]]
[[1983 Name the Crossing]]
[[2030 The Secret Number]]
[[2031 Building a Space Station]]
[[2032 Square Carpets]]
[[2041 Unreliable Message]]
[[2042 Lagranges Four-Square Theorem]]
[[2043 Area of Polygons]]
[[2044 Weather Forecast]]
[[2045 Molecular Formula]]
[[2046 Gap]]
[[2047 Concert Hall Scheduling]]
[[2048 Monster Trap]]
[[2067 Young, Poor and Busy]]
[[2068 Nim]]
[[2069 Super Star]]
[[2140 Herd Sums]]
[[2142 The Balance]]
[[2143 Make a Sequence]]
[[2144 Leaky Cryptography]]
[[2145 Pathological Paths]]
[[2146 Confusing Login Names]]
[[2147 Dice Puzzle]]
[[2149 Inherit the Spheres]]
[[2195 Going Home]]
[[2227 The Wedding Juicer]]
[[2440 DNA]]
[[2505 A multiplication game]]
[[2535 Very Simple Problem]]
[[2597 Line Segment Erase]]
[[2632 Crashing Robots]]
[[2633 Funny Games]]
[[2635 The Embarrassed Cryptographer]]
[[2636 Electrical Outlets]]
[[2637 WorstWeather Ever]]
[[2639 Necklace Decomposition]]
[[2640 Playground]]
[[2683 Ohgas' Fortune]]
[[2684 Polygonal Line Search]]
[[2685 Numeral System]]
[[2686 Traveling by Stagecoach]]
[[2688 Cleaning Robot]]
[[2739 Sum of Consecutive Prime Numbers]]
[[2769 Reduced ID Numbers]]
[[2782 Bin Packing]]
[[2800 Joseph's Problem]]
2006-06-05T00:30:15+09:00
1149435015
-
2800 Joseph's Problem
https://w.atwiki.jp/azounoman/pages/99.html
**2800 Joseph's Problem
**解答例
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
long k = sc.nextInt();
long r = 0;
/*
for(int i=1;i<=n;i++) r += k%i;
System.out.println(r);
*/
long n0 = (long)Math.sqrt(n);
for(int i=1;i<=n0;i++) r += k%i;
// n0 == n
if(n0==n){
System.out.println(r);
return;
}
// n0 != n
long c1, c2, r1, r2, q;
// k = c1 * q + r1
// k = c2 * q + r2
c1 = n0 + 1;
q = k / c1;
while(true){
if(q==0) c2 = n;
else c2 = Math.min(k/q, n);
r1 = k - c1 * q;
r2 = k - c2 * q;
r += (r1 + r2) * (c2 - c1 + 1) / 2;
if(c2==n) break;
c1 = c2 + 1;
q = k / c1;
}
System.out.println(r);
}
}
2006-05-14T01:01:00+09:00
1147536060
-
2782 Bin Packing
https://w.atwiki.jp/azounoman/pages/98.html
**2782 Bin Packing
**解答例
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int b = sc.nextInt();
int[] ls = new int[n];
for(int i=0;i<n;i++) ls[i] = sc.nextInt();
Arrays.sort(ls);
int inf = 0;
int sup = n-1;
int cnt = 0;
while(true){
cnt++;
int x = ls[sup];
sup--;
if(inf>sup) break;
int y = ls[inf];
if(x+y<=b) inf++;
if(inf>sup) break;
}
System.out.println(cnt);
}
}
2006-05-14T00:59:46+09:00
1147535986
-
2769 Reduced ID Numbers
https://w.atwiki.jp/azounoman/pages/97.html
**2769 Reduced ID Numbers
**解答例
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
int g = sc.nextInt();
int a[] = new int[g];
for(int j=0;j<g;j++){
a[j] = sc.nextInt();
}
Arrays.sort(a);
boolean[] ans = new boolean[1000000];
Arrays.fill(ans, true);
ans[0] = false;
if(g==1) System.out.println(1);
else{
for(int k=0;k<g;k++){
for(int l = k+1;l<g;l++){
int x = a[l] - a[k];
if(ans[x]==false) continue;
for(int m=1;m*m<=x;m++){
if(x%m==0){
ans[m] = false;
ans[x/m] = false;
}
}
}
}
for(int m=1;m<=1000000;m++){
if(ans[m]){
System.out.println(m);
break;
}
}
}
}
}
}
2006-05-14T00:58:23+09:00
1147535903
-
2739 Sum of Consecutive Prime Numbers
https://w.atwiki.jp/azounoman/pages/96.html
**2739 Sum of Consecutive Prime Numbers
**解答例
import java.util.*;
public class Main {
static final int SIZE = 2000;
static int[] primetable;
public static void main(String[] args) {
//make prime number table
makePrimeTable();
//scanner
Scanner sc = new Scanner(System.in);
//solve
int x;
while((x = sc.nextInt())!=0){
int ans = 0;
for(int i=0;i<SIZE;i++){
int sum = 0;
for(int j=i;j<SIZE;j++){
sum += primetable[j];
if(x==sum){
ans++;
break;
}
if(x<sum) break;
}
}
System.out.println(ans);
}
}
private static void makePrimeTable(){
primetable = new int[SIZE];
int i = 0;
int x = 2;
boolean flag;
while(i<SIZE){
flag = false;
for(int j=0;j<i;j++){
if(x%primetable[j]==0){
x++;
flag = true;
break;
}
}
if(flag) continue;
primetable[i] = x;
//System.out.println(x);
i++;
x++;
}
}
}
2006-05-14T00:56:26+09:00
1147535786
-
2688 Cleaning Robot
https://w.atwiki.jp/azounoman/pages/95.html
**2688 Cleaning Robot
**解答例
import java.util.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(true){
int w = sc.nextInt();
int h = sc.nextInt();
if(w==0&&h==0) break;
char[][] floor = new char[h][w];
for(int i=0;i<h;i++){
String s = sc.next();
for(int j=0;j<w;j++){
floor[i][j] = s.charAt(j);
}
}
Solver sol = new Solver(w, h, floor);
System.out.println(sol.solve());
}
}
}
class Solver{
int w;
int h;
char[][] floor;
int size; // 1 + num of dirty tile
Point loc[];
int[][] dist;
int max;
public Solver(int w, int h, char[][] floor) {
this.w = w;
this.h = h;
this.floor = floor;
// initialize information of robot and dirty tiles
size = 1;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(floor[i][j]=='*'){
size++;
}
}
}
int cnt = 1;
loc = new Point[size];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(floor[i][j]=='o') loc[0] = new Point(i,j);
if(floor[i][j]=='*'){
loc[cnt] = new Point(i,j);
cnt++;
}
}
}
// initialize distance table
initDist();
max = Integer.MAX_VALUE;
}
private void initDist() {
dist = new int[size][size];
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
char[][] f = new char[h][w];
for(int y=0;y<h;y++){
for(int x=0;x<w;x++){
f[y][x] = floor[y][x];
}
}
int sx = loc[i].x;
int sy = loc[i].y;
int ex = loc[j].x;
int ey = loc[j].y;
f[sx][sy] = '!';
int d = 0;
while(true){
if(f[ex][ey]=='!') break;
boolean end = true;
for(int y=0;y<h;y++){
for(int x=0;x<w;x++){
if(f[y][x]!='x'&&f[y][x]!='!'){
if(y>0 &&f[y-1][x]=='!'){ f[y][x] = '?'; end = false;}
if(y<h-1&&f[y+1][x]=='!'){ f[y][x] = '?'; end = false;}
if(x>0 &&f[y][x-1]=='!'){ f[y][x] = '?'; end = false;}
if(x<w-1&&f[y][x+1]=='!'){ f[y][x] = '?'; end = false;}
}
}
}
if(end){
d = -1;
break;
}
else{
for(int y=0;y<h;y++){
for(int x=0;x<w;x++){
if(f[y][x]=='?') f[y][x] = '!';
}
}
d++;
}
}
dist[i][j] = d;
// System.out.print(dist[i][j]+" ");
}
// System.out.println();
}
}
public int solve(){
boolean[] searched = new boolean[size];
Arrays.fill(searched, false);
searched[0] = true;
int ret = search(0, 0, 1, searched);
if(ret==Integer.MAX_VALUE) ret = -1;
return ret;
}
private int search(int loc, int len, int snum, boolean[] searched){
if(snum==size){
max = Math.min(max, len);
return len;
}
else{
if(len>=max) return max;
int ret = Integer.MAX_VALUE;
for(int i=0;i<size;i++){
if(!searched[i]&&dist[loc][i]>=0){
searched[i] = true;
ret = Math.min(ret, search(i, len + dist[loc][i], snum + 1, searched));
searched[i] = false;
}
}
return ret;
}
}
}
class Point{
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
2006-05-14T00:54:36+09:00
1147535676
-
2686 Traveling by Stagecoach
https://w.atwiki.jp/azounoman/pages/94.html
**2686 Traveling by Stagecoach
**解答例
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
int n = sc.nextInt();
int m = sc.nextInt();
int p = sc.nextInt();
int a = sc.nextInt()-1;
int b = sc.nextInt()-1;
if(n==0 && m==0 && p==0) break;
int[] t = new int[n];
for(int i=0;i<n;i++) t[i] = sc.nextInt();
double[][] path = new double[m][m];
for(int i=0;i<m;i++) Arrays.fill(path[i], Double.MAX_VALUE);
for(int i=0;i<p;i++){
int x = sc.nextInt()-1;
int y = sc.nextInt()-1;
double z = sc.nextDouble();
path[x][y] = z;
path[y][x] = z;
}
Solver sol = new Solver(n, m, a, b, t, path);
double ans = sol.solve();
if(ans==Double.MAX_VALUE) System.out.println("Impossible");
else System.out.printf("%.3f\r\n", sol.solve());
}
}
}
class Solver{
int n;
int m;
int a;
int b;
int[] t;
double[][] path;
public Solver(int n, int m, int a, int b, int[] t, double[][] path) {
this.n = n;
this.m = m;
this.a = a;
this.b = b;
this.t = t;
this.path = path;
}
public double solve(){
boolean searched[][] = new boolean[m][1<<n];
double x[][] = new double[m][1<<n];
for(int i=0;i<m;i++) Arrays.fill(searched[i], false);
for(int i=0;i<m;i++) Arrays.fill(x[i], Double.MAX_VALUE);
x[a][(1<<n)-1] = 0;
PriorityQueue<State> q = new PriorityQueue<State>();
q.add(new State(a, (1<<n)-1, 0.0));
while(!q.isEmpty()){
State s = q.poll();
if(searched[s.city][s.ticketcode]) continue;
searched[s.city][s.ticketcode] = true;
// end
if(s.city==b){
return s.x;
}
// next
TICKET: for(int i=0;i<n;i++){
if(!hasTicket(s.ticketcode, i)) continue TICKET;
int newcode = useTicket(s.ticketcode, i);
CITY: for(int j=0;j<m;j++){
if(path[s.city][j]==Double.MAX_VALUE) continue CITY;
if(searched[j][newcode]) continue CITY;
if(s.x + path[s.city][j]/t[i] < x[j][newcode]){
x[j][newcode] = s.x + path[s.city][j]/t[i];
q.add(new State(j, newcode, x[j][newcode]));
}
}
}
}
return Double.MAX_VALUE;
}
private static boolean hasTicket(int ticketcode, int i){
return (ticketcode>>i)%2==1;
}
private int useTicket(int ticketcode, int i){
return ticketcode - (1<<i);
}
}
class State implements Comparable<State>{
int city;
int ticketcode;
double x;
public State(int city, int ticketcode, double x) {
this.city = city;
this.ticketcode = ticketcode;
this.x = x;
}
public int compareTo(State s){
if(x>s.x) return 1;
else if(x<s.x) return -1;
else return 0;
}
}
2006-05-14T00:52:05+09:00
1147535525
-
2685 Numeral System
https://w.atwiki.jp/azounoman/pages/93.html
**2685 Numeral System
**解答例
import java.util.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
String s1 = sc.next();
String s2 = sc.next();
print_mcxi(mcxi_to_int(s1) + mcxi_to_int(s2));
}
}
private static int mcxi_to_int(String s){
int ret = 0;
int index = 0;
int prefix;
char unit;
while(index<s.length()){
char c = s.charAt(index);
if('2'<=c&&c<='9'){
prefix = c - '0';
index++;
c = s.charAt(index);
}
else{
prefix = 1;
}
unit = c;
switch(unit){
case 'm':
ret += 1000 * prefix;
break;
case 'c':
ret += 100 * prefix;
break;
case 'x':
ret += 10 * prefix;
break;
case 'i':
ret += prefix;
break;
default:
throw new RuntimeException();
}
index++;
}
return ret;
}
private static void print_mcxi(int x){
int pm = x/1000;
int pc = (x/100)%10;
int px = (x/10)%10;
int pi = x%10;
if(pm>=2){
System.out.printf("%dm",pm);
}
else if(pm==1){
System.out.printf("m");
}
if(pc>=2){
System.out.printf("%dc",pc);
}
else if(pc==1){
System.out.printf("c");
}
if(px>=2){
System.out.printf("%dx",px);
}
else if(px==1){
System.out.printf("x");
}
if(pi>=2){
System.out.printf("%di",pi);
}
else if(pi==1){
System.out.printf("i");
}
System.out.println();
}
}
2006-05-14T00:50:08+09:00
1147535408
-
2684 Polygonal Line Search
https://w.atwiki.jp/azounoman/pages/92.html
**2684 Polygonal Line Search
**解答例
import java.util.*;
public class Main {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int n;
while((n=sc.nextInt())>0){
int m0 = sc.nextInt();
PolyLine tmp = new PolyLine(m0, readPoints(m0));
PolyLine rtmp = tmp.reverse();
int[] tmpcode = tmp.code();
int[] rtmpcode = rtmp.code();
for(int i=1;i<=n;i++){
int m = sc.nextInt();
PolyLine pl = new PolyLine(m, readPoints(m));
if(m!=m0) continue;
int[] plcode = pl.code();
if(PolyLine.codeEquals(tmpcode, plcode) ||
PolyLine.codeEquals(rtmpcode, plcode)){
System.out.println(i);
}
}
System.out.println("+++++");
}
}
private static Point[] readPoints(int m){
Point[] ps = new Point[m];
for(int i=0;i<m;i++) ps[i] = new Point(sc.nextInt(), sc.nextInt());
return ps;
}
}
class PolyLine{
int m;
Point[] ps;
public PolyLine(int m, Point[] ps) {
this.m = m;
this.ps = ps;
}
public PolyLine reverse(){
Point[] rps = new Point[m];
for(int i=0;i<m;i++) rps[i] = ps[m-1-i];
return new PolyLine(m, rps);
}
public int[] code(){
int[] ret = new int[2*m-3];
for(int i=0;i<m-1;i++) ret[2*i] = Point.dist(ps[i], ps[i+1]);
for(int i=0;i<m-2;i++) ret[2*i+1] = Point.angleSign(ps[i], ps[i+1], ps[i+2]);
return ret;
}
public static boolean codeEquals(int[] c1, int[] c2){
if(c1.length!=c2.length) return false;
boolean flag = true;
for(int i=0;i<c1.length;i++) if(c1[i]!=c2[i]) flag = false;
return flag;
}
}
class Point{
int x;
int y;
public Point(int x, int y) {
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
}
public static int dist(Point p1, Point p2){
return Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y);
}
public static int angleSign(Point p1, Point p2, Point p3){
if((p2.x - p1.x)*(p3.y - p2.y) - (p2.y - p1.y)*(p3.x - p2.x) > 0) return 1;
else return -1;
}
}
2006-05-14T00:47:58+09:00
1147535278