2013hpuacm第八周周赛题

来源:互联网 发布:不要网络的游戏有哪些 编辑:程序博客网 时间:2024/06/05 14:37

1001 To and Fro

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 52   Accepted Submission(s) : 33

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example, if the message is “There’s no place like home on a snowy night” and there are five columns, Mo would write down

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x


Note that Mo includes only letters and writes them all in lower case. In this example, Mo used the character ‘x’ to pad the message out to make a rectangle, although he could have used any letter.

Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So, the above would be encrypted as

toioynnkpheleaigshareconhtomesnlewx

Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.

Input

There will be multiple input sets. Input for each set will consist of two lines. The first line will contain an integer in the range 2. . . 20 indicating the number of columns used. The next line is a string of up to 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end of input.

Output

Each input set should generate one line of output, giving the original plaintext message, with no spaces.

Sample Input

5toioynnkpheleaigshareconhtomesnlewx3ttyohhieneesiaabss0

Sample Output

theresnoplacelikehomeonasnowynightxthisistheeasyoneab

Source

East Central North America 2004

思路:蛇形填数,然后按列输出


import java.io.InputStreamReader;import java.util.Scanner;public class Main{//1001public static void main(String[] args) {Scanner input=new Scanner(new InputStreamReader(System.in));int n;while((n=input.nextInt())!=0){String s=input.next();int len=s.length()/n;char a[][]=new char[len][n];int e=0;for(int i=0;i<len;i+=2){for(int j=0;j<n;j++){a[i][j]=s.charAt(e++);}if(i+1<len)for(int j=n-1;j>=0;j--)a[i+1][j]=s.charAt(e++);}for(int i=0;i<n;i++){System.out.print(a[0][i]);for(int j=1;j<len;j++){System.out.print(a[j][i]);}}System.out.println();}}}

1002 大菲波数

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 95   Accepted Submission(s) : 39

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

Fibonacci数列,定义如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。

Input

输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。

Output

输出为N行,每行为对应的f(Pi)。

Sample Input

512345

Sample Output

11235

Source

2007省赛集训队练习赛(2)

思路:大数,一个循环摆平,水题

import java.io.InputStreamReader;import java.math.BigInteger;import java.util.Scanner;public class Main{    public static void main(String[] args) {        Scanner input=new Scanner(new InputStreamReader(System.in));        int N=input.nextInt();        while(N-->0){            int n=input.nextInt();            BigInteger a=BigInteger.ONE;            BigInteger b=BigInteger.ONE;            BigInteger c=BigInteger.ONE;            for(int i=3;i<=n;i++){                c=a.add(b);                a=b;                b=c;            }            System.out.println(c);        }    }}


1003 Eddy's picture

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?

Input

The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point. 

Input contains multiple test cases. Process to the end of file.

Output

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points. 

Sample Input

31.0 1.02.0 2.02.0 4.0

Sample Output

3.41

Author

eddy

思路:最小生成树+并查集,

import java.util.Arrays;import java.util.Comparator;import java.util.Scanner;public class Main{static Point f[],map[];static int rand[],p[],n;public static void main(String[] args) {Scanner input=new Scanner(System.in);while(input.hasNext()){n=input.nextInt();f=new Point[n];rand=new int[n];p=new int[n];map=new Point[6000];//其实开到n*n就可以了,不过还是开大点吧Init();for(int i=0;i<n;i++){float a=input.nextFloat();float b=input.nextFloat();f[i]=new Point(a,b);}int e=0;//求所有可联通的点for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){//j=i+1,不要求重哦,如j,i;i,jdouble t=Math.sqrt(Math.pow(f[i].a-f[j].a, 2)+Math.pow(f[i].b-f[j].b, 2));map[e++]=new Point(i,j,t);//存起来}}Arrays.sort(map,0,e,new Comparator<Point>(){//从小到大排序,最小生成树@Overridepublic int compare(Point a0, Point a1) {if(a0.s<a1.s)return -1;else if(a0.s==a1.s)return 0;return 1;}});//测试/*for(int i=0;i<e;i++)System.out.println(map[i].s+" ");*///double sum=0;for(int i=0;i<e;i++){//并查集,查看这个点是否被连通if(heBing(map[i].x,map[i].y)){sum+=map[i].s;}}System.out.println(String.format("%.2f",sum));}}private static boolean heBing(int x, int y) {//合并集合int X=find(x);int Y=find(y);if(X==Y)return false;if(rand[X]<rand[Y]){//优化,小树合并到大树中p[X]=Y;}else{p[Y]=X;if(rand[X]==rand[Y])//如果深度相等,+1rand[X]++;}return true;}private static int find(int x) {//查找根节点while(x!=p[x])x=p[x];return x;}private static void Init() {//初始化Arrays.fill(rand, 1);//集合初始化为1,因为就自己一个深度是1for(int i=0;i<n;i++)p[i]=i;}}class Point{//int x,y;float a,b;double s;Point(float x,float y){//点坐标this.a=x;this.b=y;}Point(int x,int y,double s){//存可连通的两个点,和距离this.x=x;this.y=y;this.s=s;}}

1004 搬寝室

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 36   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太多了,于是xhd决定随便搬2*k件过去就行了.但还是会很累,因为2*k也不小是一个不大于n的整数.幸运的是xhd根据多年的搬东西的经验发现每搬一次的疲劳度是和左右手的物品的重量差的平方成正比(这里补充一句,xhd每次搬两件东西,左手一件右手一件).例如xhd左手拿重量为3的物品,右手拿重量为6的物品,则他搬完这次的疲劳度为(6-3)^2 = 9.现在可怜的xhd希望知道搬完这2*k件物品后的最佳状态是怎样的(也就是最低的疲劳度),请告诉他吧.

Input

每组输入数据有两行,第一行有两个数n,k(2<=2*k<=n<2000).第二行有n个整数分别表示n件物品的重量(重量是一个小于2^15的正整数).

Output

对应每组输入数据,输出数据只有一个表示他的最少的疲劳度,每个一行.

Sample Input

2 11 3

Sample Output

4

Author

xhd

Source

ACM暑期集训队练习赛(二)

思路:最小生成树+并查集,java超内存,大家可以用C试一下

import java.io.InputStreamReader;import java.util.Arrays;import java.util.Comparator;import java.util.HashMap;import java.util.Scanner;public class Main {//超内存    public static void main(String[] args) {        Scanner input=new Scanner(new InputStreamReader(System.in));        while(input.hasNext()){            int n=input.nextInt();            int k=input.nextInt();            int a[]=new int[n];            for(int i=0;i<n;i++){                a[i]=input.nextInt();            }            Point f[]=new Point[n*n];            int e=0;            for(int i=0;i<n;i++){                for(int j=i+1;j<n;j++){                    f[e++]=new Point(i,j,Math.abs(a[i]-a[j]));                }            }            Arrays.sort(f,0,e,new Comparator<Point>(){//从小到大排序,最小生成树                @Override                public int compare(Point a0, Point a1) {                    if(a0.v<a1.v)                        return -1;                    else if(a0.v==a1.v)                        return 0;                    return 1;                }            });            HashMap<String, Boolean> map1=new HashMap<String, Boolean>();            long sum=0;            for(int i=0;i<e;i++){                if(map1.get(""+f[i].x+"+"+f[i].y)==null){                    sum+=f[i].v*f[i].v;                    map1.put(""+f[i].x+"+"+f[i].y, true);                }                if(map1.size()>=k)                    break;            }            System.out.println(sum);        }    }}class Point{    int x;    int y;    int v;    public Point(int x, int y, int v) {        this.x = x;        this.y = y;        this.v = v;    }}

1005 Scales

Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

Give you a scale, a goods weigts m kilograms. And then give you some stones weighting 1, 3, 9, 27, ..., 3^k. Of course, the number of different weights' is only ONE.

Now put the goods on the left of scale. And then you should put some stones on two sides of scale to make the scale balanced.

Input

An integer m, stands for the weights of the goods (0 ≤ m ≤ 100 000 000)

Output

You should output two lines.

In the first line, the first integer N1 is the number of stones putting on the left of scale, and then N1 integers follow(in the ascend order), indicating the weight of stones putting on the left, seperated by one space. In the second line, please use the same way as the first line's to output the N2, which is the number of stones putting on the right side. Then following N2 integers, which are in ascending order, indicate the stones' weight putting on the right side.

Sample Input

4230

Sample Output

3 3 9 271 8102 3 27

Source

2009 Multi-University Training Contest 6 - Host by WHU


思路:可以用动态规划试一下,不过我没做出来


1006 小希的迷宫

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 6

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。 

Input

输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。 
整个文件以两个-1结尾。

Output

对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出"Yes",否则输出"No"。

Sample Input

6 8  5 3  5 2  6 45 6  0 08 1  7 3  6 2  8 9  7 57 4  7 8  7 6  0 03 8  6 8  6 45 3  5 6  5 2  0 0-1 -1

Sample Output

YesYesNo

Author

Gardon

Source

HDU 2006-4 Programming Contest

思路:用删边问题,或者并查集都可以解决,水题

import java.io.InputStreamReader;import java.util.Scanner;public class Main{    static int s[];    static boolean ok=true;    public static void main(String[] args) {        Scanner input=new Scanner(new InputStreamReader(System.in));        while(true){            int a=input.nextInt();            int b=input.nextInt();            if(a==0&&b==0){                System.out.println("Yes");                continue;            }            if(a==-1&&b==-1)                break;            s=new int[100001];            boolean d[]=new boolean[100001];            for(int i=1;i<=100000;i++)                s[i]=i;            ok=true;            int max=a;            int min=a;            while(!(a==0&&b==0)){                if(a>max)                    max=a;                if(b>max)                    max=b;                if(a<min)                    min=a;                if(b<min)                    min=b;                d[a]=true;                d[b]=true;                if(ok)                    hebing(a,b);                a=input.nextInt();                b=input.nextInt();            }            int sum=0;            for(int i=min;i<=max;i++)                if(s[i]==i&&d[i]){                //    System.out.print(s[i]+" ");                    sum++;                }            if(sum==1&&ok)                System.out.println("Yes");            else                System.out.println("No");        //    System.out.println(sum+"****"+ok);        }    }    private static void hebing(int a, int b) {        int X=find(a);        int Y=find(b);        if(X==Y){            ok=false;        }        else            s[X]=Y;    }    private static int find(int a) {        while(s[a]!=a)            a=s[a];        return a;    }}

1007 Coder

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 0

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

  In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap and reliable computers. Many companies now employ a single coder to write an algorithm that will replace many other employees. An added benefit to the employer is that the coder will also become redundant once their work is done. 1
  You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should be able to complete a task to replace these employees who do nothing all day but eating: make the digest sum.
  By saying “digest sum” we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code should give response to following operations:
  1. add x – add the element x to the set;
  2. del x – remove the element x from the set;
  3. sum – find the digest sum of the set. The digest sum should be understood by

  where the set S is written as {a1, a2, ... , ak} satisfying a1 < a2 < a3 < ... < ak 
  Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm

Input

  There’re several test cases.
  In each test case, the first line contains one integer N ( 1 <= N <= 105 ), the number of operations to process.
  Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
  You may assume that 1 <= x <= 109.
  Please see the sample for detailed format.
  For any “add x” it is guaranteed that x is not currently in the set just before this operation.
  For any “del x” it is guaranteed that x must currently be in the set just before this operation.
  Please process until EOF (End Of File).

Output

  For each operation “sum” please print one line containing exactly one integer denoting the digest sum of the current set. Print 0 if the set is empty.

Sample Input

9add 1add 2add 3add 4add 5sumadd 6del 3sum6add 1add 3add 5add 7add 9sum

Sample Output

345

Hint

C++ maybe run faster than G++ in this problem.

Source

2012 ACM/ICPC Asia Regional Chengdu Online

思路:链表,不知道怎么错了

import java.io.InputStreamReader;import java.util.LinkedList;import java.util.Scanner;public class Main{    public static void main(String[] args) {        Scanner input=new Scanner(new InputStreamReader(System.in));        while(input.hasNext()){            int n=input.nextInt();            LinkedList<Integer> list=new LinkedList<Integer>();            while(n-->0){                String s=input.next();                                if(s.equals("add")){                    int a=input.nextInt();                    list.add(a);                }                else if(s.equals("sum")){                    long sum=0;                    for(int i=2;i<list.size();i+=5)                        sum+=list.get(i);                    System.out.println(sum);                }                else{                    int a=input.nextInt();                    list.remove(a-1);                }            }        }    }}


原创粉丝点击