Bestcoder

Solved Problems
Numbers
Game
Subtrees

Numbers

可能需要两个特判?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Oct 31, 2015 7:01:33 PM
* PrjName: Bc61-01
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int maxl=1001;
static int[] f=new int[maxl];
static Vector<Integer> v=new Vector<Integer>();
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
while(in.nextLine()!=null){
int n=in.nextInt();
// if (n<1) continue;
boolean has1=false,has2=false,has=false;;
Arrays.fill(f, 0);
for(int i=1;i<=n;i++){
int k=in.nextInt();
f[k]++;
if (f[k]>1&&k>0) has1=true;
if (f[k]>2) has2=true;
}
v.clear();
for(int i=0;i<maxl;i++)
if (f[i]>0)
v.add(i);
// for(Integer e:v.toArray(new Integer[0]))
// out.print(e+" ");
if (v.get(0)==0&&has1||has2){
out.println("YES");continue;
}
Integer[] a=v.toArray(new Integer[0]);
for(int i=0;i<a.length-1;i++)
if (!has)
for(int j=a.length-1;j>i;j--){
int k=Arrays.binarySearch(a, a[j]-a[i]);
if (k>i&&k<j){
has=true;break;
}

}
out.println(has?"YES":"NO");
}
out.flush();
out.close();
}
}

Game

各种分类讨论,但就是要写好最边缘的特判。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Oct 31, 2015 7:28:53 PM
* PrjName: Bc61-02
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
while(in.nextLine()!=null){
int n=in.nextInt();
int s=in.nextInt();
int t=in.nextInt();
if (s==t){
out.println(n>1?-1:0);continue;
}
if (t==1){
if (s==n)
out.println(0);
else if (s==2)
out.println(1);
else
out.println(2);
}
else if (t==n){
if (s==1)
out.println(0);
else if (s==n-1)
out.println(1);
else
out.println(2);
}
else{
if (s==t+1||s==t-1||s==1||s==n)
out.println(1);
else
out.println(2);
}
}
out.flush();
out.close();
}

}

Subtrees

有些查找行为显然拖慢时间以致TLE。。。
有一个重要优化是从小到大枚举子树大小而不是相反方向;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Nov 7, 2015 10:31:31 PM
* PrjName: hdu5524-3
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int maxn=64;
static long[] p2;
static HashSet<Long> st=new HashSet<Long>();
static PrintWriter out=new PrintWriter(System.out);
static void solve(long n){
if (n==0L) return;
st.add(n);
if (n==1L) return;
long f;
for(f=1L;;f=f<<1|1L)
if ((f>>1)<=n-1L-f&&(f<<1)>=n-1L-f)
break;
solve(f);
if ((f<<1|1)!=n)
solve(n-1-f);
}
static void init(){
p2=new long[maxn];
p2[0]=1L;
for(int i=1;i<maxn;i++){
p2[i]=p2[i-1]<<1;
p2[i-1]--;
}
}
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);

init();
while(in.nextLine()!=null){
long n=in.nextLong();
st.clear();solve(n);
out.println(st.size());
}
out.flush();
out.close();
}

}

BestCoder

Solved Problems
SDOI
Reorder the Books

SDOI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* 2015年10月10日 下午6:54:59
* PrjName:Bc59-01
* @ Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static Person[] ps;
static ArrayList<Person> li=new ArrayList<Person>();
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
int m=in.nextInt();
float x1=0,x2=0;
int female=0,h=0;
ps=new Person[n];
for(int i=0;i<n;i++){
ps[i]=new Person(in.next(), in.next(), in.nextInt(), in.nextInt());
female+=ps[i].sex;
if (ps[i].sex==1&&h==0) h=i;
x1=Math.max(x1, ps[i].s1);
x2=Math.max(x2, ps[i].s2);
}
li.clear();
for(int i=0;i<n;i++){
ps[i].s1*=300;ps[i].s1/=x1;
ps[i].s2*=300;ps[i].s2/=x2;
ps[i].s=ps[i].s1*(float)0.3+ps[i].s2*(float)0.7;
if (female>0){
if (ps[i].s>ps[h].s&&ps[i].sex==1) h=i;
}
else
if (ps[i].s>ps[h].s) h=i;
//li.add(ps[i]);
}
out.println("The member list of Shandong team is as follows:");
Person p0=new Person();
if (female>0){
// out.println(li.get(h).name);
p0=new Person(ps[h]);
ps[h].s=-1;
m--;
}
Arrays.sort(ps, new Person.comp());
li.clear();

for(int i=0;i<m;i++)
li.add(ps[i]);
if (female>0){
li.add(p0);m++;
}
ps=li.toArray(new Person[0]);
Arrays.sort(ps, new Person.comp());
for(int i=0;i<m;i++)
out.println(ps[i].name);

}
out.flush();
out.close();
}
}
class Person{
String name;
int sex;
float s1,s2,s;
Person(){}
Person(Person p){
name=new String(p.name);
sex=p.sex;
s1=p.s1;
s2=p.s2;
s=p.s;
}
Person(String nm,String sx,int _s1,int _s2){
name=new String(nm);
if (sx.charAt(0)=='m') sex=0;
else sex=1;
s1=_s1;s2=_s2;
}
void debug(PrintWriter out){
out.println(name+"\t"+sex+"\t"+s);
}
static class comp implements Comparator<Person> {
public int compare(Person p1,Person p2){
return -Float.compare(p1.s, p2.s);
}
}
}

Reorder the Books

思索再三,竟然连充分条件、必要条件都没找准,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/** Oct 13, 2015 8:55:41 PM
* PrjName:hdu5500
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int[] a;
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
a=new int[n+1];
for(int i=1;i<=n;i++)
a[in.nextInt()]=i;
int ans=0;
// if (a[n]!=n&&a[n]!=1) ans++;
int k=a[n];
for(int i=n;i>1;i--)
if (a[i-1]<k){
k=a[i-1];n--;
}
else
break;
// out.println(n);
for(int i=n-1;i>1;i--){
if (a[i]==1)
continue;
boolean need=false;
for(int j=1;j<i;j++)
if (a[j]>a[i]) need|=true;
for(int j=i+1;j<=n;j++)
if (a[j]<a[i]) need|=true;
if (need){
ans++;
for(int j=1;j<=n;j++)
if (a[j]<a[i])
a[j]++;
a[i]=1;
}
}
if (a[1]!=1) ans++;
out.println(ans);
}
out.flush();
out.close();
}
}

观察、归纳后可以优化到最简,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Main {
static int[] a;
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
a=new int[n+1];
for(int i=1;i<=n;i++)
a[in.nextInt()]=i;
int ans=0;
int k=a[n],h=0;
for(int i=n;i>1;i--){
if (a[i-1]>k){
ans++;
a[i-1]=h--;
}
k=a[i-1];
}
if (a[1]!=h+1) ans++;
out.println(ans);
}
out.flush();
out.close();
}

}

BestCoder

1001 Clarke and minecraft

玩过MC无压力

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/** Sep 19, 2015 7:12:36 PM
* PrjName:Bc56-01
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int maxn=110;
static int cnt;
static int[] w;
static HashMap<Integer, Integer> mp=new HashMap<Integer,Integer>();
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
//h=new int[maxn];
w=new int[maxn];
while(T-->0){
int n=in.nextInt();
cnt=0;
mp.clear();
Arrays.fill(w, 0);
for(int i=1;i<=n;i++){
int k=in.nextInt();
if (mp.containsKey(k))
w[mp.get(k)]+=in.nextInt();
else{
w[cnt]+=in.nextInt();
mp.put(k, cnt++);
}
}
int sum=0;
for(int i=0;i<cnt;i++)
sum+=(w[i]+63)/64;
out.println((sum+35)/36);
}
out.flush();
out.close();
}
}

1002 Clarke and problem

第一时间想到的是容斥,而后才逐渐醒悟过来是DP。。。
另外mod的遗漏又错了一发。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/** Sep 19, 2015 7:24:54 PM
* PrjName:Bc56-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int maxn=1010;
final static int high=1000000000;
final static int mod=1000000007;
//static int[] s=new int[maxn];
static int[][] f=new int[2][maxn];
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
int p=in.nextInt();
//Arrays.fill(s, 0, p, 0);
Arrays.fill(f[0], 0, p, 0);
f[0][0]=1;
int cnt=0;
//for(int j=0;j<p;j++)out.print(f[j]+" ");out.println();
for(int i=1;i<=n;i++){
int k=in.nextInt();
while(k<0) k+=p;
k%=p;
Arrays.fill(f[cnt^1], 0,p,0);
for(int j=0;j<p;j++){
f[cnt^1][(k+j)%p]+=f[cnt][j];
f[cnt^1][(k+j)%p]%=mod;
}
for(int j=0;j<p;j++){
f[cnt^1][j]+=f[cnt][j];
f[cnt^1][j]%=mod;
}
cnt^=1;
}
out.println(f[cnt][0]);

}
out.flush();
out.close();
}
}

1003 Clarke and puzzle

没有总结好Nim游戏的规律啊。。。超简单的代码

Bestcoder

1001 A problem of sorting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/** Sep 5, 2015 7:07:52 PM
* PrjName:Bc54-01
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
final static int maxn=116;
static String s;
static Vector<String>[] vec=new Vector[maxn];
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
for(int i=0;i<maxn;i++)
vec[i]=new Vector<String>();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine());
while(T-->0){
for(int i=0;i<maxn;i++)
vec[i].clear();
int n=Integer.parseInt(br.readLine());
for(int i=1;i<=n;i++){
s=new String(br.readLine());
int y=0,tmp=1,j;
for(j=s.length()-1;j>0;j--)
if (s.charAt(j)>='0'&&s.charAt(j)<='9'){
y+=tmp*(s.charAt(j)-'0');
tmp*=10;
}
else
break;
s=s.substring(0, j);
vec[y-1900].add(s);
}
for(int i=maxn-1;i>=0;i--)
for(int j=0;j<vec[i].size();j++)
out.println(vec[i].get(j));
}
out.flush();
out.close();
}
}

1002 The Factor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/** Sep 5, 2015 7:30:23 PM
* PrjName:Bc54-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
final static int maxn=109000;
static int[] a;
static int[] pri,phi,fstp;
static void get_prime(){
pri=new int[maxn];
fstp=new int[maxn];
phi=new int[maxn];
phi[1]=1;
for(int i=2;i<maxn;i++){
if (fstp[i]==0){
pri[++pri[0]]=i;
phi[i]=i-1;
}
for(int j=1;j<=pri[0]&&i*pri[j]<maxn;j++){
int k=i*pri[j];
fstp[k]=pri[j];
//if (fstp[i]==pri[j]){
if (i%pri[j]==0){
phi[k]=phi[i]*pri[j];
break;
}
else
phi[k]=phi[i]*(pri[j]-1);
}
}
}
static int get_fstp(int n){
if (n<maxn)
return fstp[n]>0?fstp[n]:n;
for(int i=2;i<maxn;i++)
if (n%i==0)
return fstp[i]>0?fstp[i]:i;
return 0;
}
static Vector<Integer> get_prime_factor(int n){
Vector<Integer> res=new Vector<Integer>();
while(n>1&&fstp[n]>0){
res.add(fstp[n]);
n/=fstp[n];
}
if (n>1) res.add(n);
return res;
}
static Vector<Integer> v=new Vector<Integer>();
static Integer[] f;
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
get_prime();
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
v.clear();
a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=in.nextInt();
if (a[i]==1) continue;
int tmp=get_fstp(a[i]);
v.add(tmp);
if (a[i]/tmp>1) v.add(get_fstp(a[i]/tmp));
}
/*v.sort(new Comparator<Integer>() {
public int compare(Integer o1,Integer o2){
return Integer.compare(o1, o2);
}
});*/
f=v.toArray(new Integer[0]);
Arrays.sort(f);
if (v.size()<2)
out.println(-1);
else
out.println(f[0]*f[1]);
}
out.flush();
out.close();
}
}

⬆️当时想太多了啊,何必把找到的因子全存进vector啊,白白吃了个RE……
(测试还发现HDU尚不支持Vector的sort方法,还未支持到Java7?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/** Sep 8, 2015 16:02:23 PM
* PrjName:Bc54-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
final static int maxn=45000;
static int[] a;
static int[] pri,fstp;
static void get_prime(){
pri=new int[maxn];
fstp=new int[maxn];
for(int i=2;i<maxn;i++){
if (fstp[i]==0){
pri[++pri[0]]=i;
}
for(int j=1;j<=pri[0]&&i*pri[j]<maxn;j++){
int k=i*pri[j];
fstp[k]=pri[j];
if (i%pri[j]==0)
break;
}
}
}
static int get_fstp(int n){
if (n<maxn)
return fstp[n]>0?fstp[n]:n;
for(int i=2;i<maxn;i++)
if (n%i==0)
return fstp[i]>0?fstp[i]:i;
return n;
}
static int max1,max2,sum;
static void init(){
max1=max2=0x3fffffff;sum=0;
}
static void update(int s){
if (s<max1){
max2=max1;max1=s;
}
else
max2=Math.min(max2, s);
sum++;
}
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
get_prime();
int T=in.nextInt();
while(T-->0){
init();
int n=in.nextInt();
a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=in.nextInt();
if (a[i]==1) continue;
int tmp=get_fstp(a[i]);
update(tmp);
if (a[i]/tmp>1) update(get_fstp(a[i]/tmp));
}
if (sum<2)
out.println(-1);
else
out.println(max1*(long)max2);
}
out.flush();
out.close();
}
}

1003 Geometric Progression

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/** Sep 5, 2015 8:05:33 PM
* PrjName:Bc54-03
* @author Semprathlon
*/
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
final static int maxn=101;
static BigInteger a[]=new BigInteger[maxn];
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
boolean zero=false,nonzero=false;
for(int i=1;i<=n;i++){
a[i]=new BigInteger(in.next());
if (a[i].equals(BigInteger.ZERO))
zero|=true;
else
nonzero|=true;
}
boolean ans=(zero&&nonzero)?false:true;
if (ans)
for(int i=2;i<n;i++)
if (!a[i].pow(2).equals(a[i-1].multiply(a[i+1]))){
ans=false;
break;
}
out.println(ans?"Yes":"No");
}
out.flush();
out.close();
}
}

不明不白地送了个WA,没有考虑好数列中值为0的项。
这题不是暑假多校才做过的么?

Bestcoder

迟到了半小时开打,不然rank还可以更好看点……

1001 Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/** Aug 29, 2015 7:39:29 PM
* PrjName:Bc53-01
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n=(int)in.nval;
in.nextToken();
int m=(int)in.nval;
boolean has=false;
//out.println(n+"-"+m);
for(int i=1;i<=m;i++){
in.nextToken();
int u=(int)in.nval;
in.nextToken();
int v=(int)in.nval;
if (u==1&&v==n||u==n&&v==1)
has=true;
}
if (has)
out.println(1+" "+n*(n-1)/2);
else
out.println(1+" "+1);
}
out.flush();
out.close();
}
}

如果点1与点n直接相连,那么就是距离最短了。

1002 Rikka with Tree

要同时满足不同与相似,就是要求结点数相等,各结点到树根1的距离分别相等(即深度分别相同),且存在父节点不同的结点。
可以说就是判断同一深度上的结点互换后是否能得到新的结构。
若有某一深度上的结点数多于一个,那么大概可以通过同层互换获得新的结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/** Aug 29, 2015 7:59:14 PM
* PrjName:Bc53-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int maxn=2010;
static Graph G=new Graph(maxn);
static int[] dep;
static HashSet<Integer> st=new HashSet<Integer>();
static void bfs(int st){
Queue<Integer> que=new LinkedList<Integer>();
que.add(st);
while(!que.isEmpty()){
int u=que.poll();
for(int i=G.h[u];i>-1;i=G.edge[i].next){
int v=G.edge[i].to;
if (dep[v]>0) continue;
dep[v]=dep[u]+1;
que.add(v);
}
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n=(int)in.nval;
G.clear();
dep=new int[n+1];
dep[1]=1;
for(int i=1;i<n;i++){
in.nextToken();
int u=(int)in.nval;
in.nextToken();
int v=(int)in.nval;
G.add(u, v);
G.add(v, u);
}
bfs(1);
boolean ans=true;
st.clear();
for(int i=1;i<=n;i++)
if (st.contains(dep[i])){
ans=false;
break;
}
else
st.add(dep[i]);
out.println(ans?"YES":"NO");
}
out.flush();
out.close();
}

}
class Edge{
int to,next;
Edge(int _u,int _v){
to=_u;next=_v;
}
}
class Graph{
int[] h;
int sz;
Edge[] edge;
Graph(int size){
sz=size;
h=new int[sz+1];
edge=new Edge[sz+1];
Arrays.fill(h, -1);
h[0]=0;
}
void clear(){
h=new int[sz+1];
edge=new Edge[sz+1];
Arrays.fill(h, -1);
h[0]=0;
}
void add(int u,int v){
edge[h[0]]=new Edge(v,h[u]);
h[u]=h[0]++;
}
}

然而错了。
适当地列举一些情况,发现若树的深度仅有2,那么它始终是特殊的。
可是加上这一判断仍不够。
进一步的举例发现,若树上非最底层有某一层结点多于一个,就能变换形成新结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/** Aug 29, 2015 7:59:14 PM
* PrjName:Bc53-02
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
final static int maxn=2010;
static Graph G=new Graph(maxn);
static int[] dep;
static HashSet<Integer> st=new HashSet<Integer>();
static void bfs(int st){
Queue<Integer> que=new LinkedList<Integer>();
que.add(st);
while(!que.isEmpty()){
int u=que.poll();
for(int i=G.h[u];i>-1;i=G.edge[i].next){
int v=G.edge[i].to;
if (dep[v]>0) continue;
dep[v]=dep[u]+1;
que.add(v);
}
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n=(int)in.nval;
G.clear();
dep=new int[n+1];
dep[1]=1;
for(int i=1;i<n;i++){
in.nextToken();
int u=(int)in.nval;
in.nextToken();
int v=(int)in.nval;
G.add(u, v);
G.add(v, u);
}
bfs(1);
boolean ans=true;
st.clear();
int maxd=0;
for(int i=1;i<=n;i++)
maxd=Math.max(dep[i], maxd);
for(int i=1;i<=n;i++)
if (st.contains(dep[i])){
if (dep[i]<maxd){
ans=false;
break;
}
}
else
st.add(dep[i]);
if (maxd<3)
ans=true;
out.println(ans?"YES":"NO");
}
out.flush();
out.close();
}

}
class Edge{
int to,next;
Edge(int _u,int _v){
to=_u;next=_v;
}
}
class Graph{
int[] h;
int sz;
Edge[] edge;
Graph(int size){
sz=size;
h=new int[sz+1];
edge=new Edge[sz+1];
Arrays.fill(h, -1);
h[0]=0;
}
void clear(){
h=new int[sz+1];
edge=new Edge[sz+1];
Arrays.fill(h, -1);
h[0]=0;
}
void add(int u,int v){
edge[h[0]]=new Edge(v,h[u]);
h[u]=h[0]++;
}
}

这与题解中所说的“一棵树是独特的当且仅当任意处于每一个深度的点数是1 1 1 1 … 1 1 x”相符(我个人将这种形态称作“呈扫帚状”)

BestCoder

1001 Victor and Machine

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)

  • Problem Description
    Victor has a machine. When the machine starts up, it will pop out a ball immediately. After that, the machine will pop out a ball every w seconds. However, the machine has some flaws, every time after x seconds of process the machine has to turn off for y seconds for maintenance work. At the second the machine will be shut down, it may pop out a ball. And while it’s off, the machine will pop out no ball before the machine restart.
    Now, at the 0 second, the machine opens for the first time. Victor wants to know when the n-th ball will be popped out. Could you tell him?

  • Input
    The input contains several test cases, at most 100 cases.
    Each line has four integers x, y, w and n. Their meanings are shown above。
    $ 1\leq x,y,w,n\leq 100 $ .

  • Output
    For each test case, you should output a line contains a number indicates the time when the n-th ball will be popped out.

  • Sample Input
    2 3 3 3
    98 76 54 32
    10 9 8 100

  • Sample Output
    10
    2664
    939

n-1的问题,自己刚开始出了点差错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/** Aug 22, 2015 7:03:37 PM
* PrjName:Bc52-01
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {

/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int x=(int)in.nval;
in.nextToken();
int y=(int)in.nval;
in.nextToken();
int w=(int)in.nval;
in.nextToken();
int n=(int)in.nval;
if (n==1){
out.println(0);continue;
}
if (w>x){
out.println((n-1)*(x+y));continue;
}
else if (w==x){
out.println((n-1)/2*(x+y)+(n-1)%2*x);continue;
}
else{
int tmp=(x+w)/w;
out.println((n-1)/tmp*(x+y)+(n-1)%tmp*w);continue;
}
}
out.flush();
out.close();
}

}

BestCoder

1001 Distribution money

WA过一发,因为忽视了金额的分布范围。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 2015年8月8日 下午7:02:20
* PrjName:Bc50-01
* @ Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int[] a;
static int maxn=10001;
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
StreamTokenizer in=new StreamTokenizer(new BufferedInputStream(System.in));
PrintWriter out=new PrintWriter(System.out);
a=new int[maxn];
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n=(int)in.nval;
Arrays.fill(a, 0);
for(int i=1;i<=n;i++){
in.nextToken();
int k=(int)in.nval;
a[k]++;
}
int ans=-1;
for(int i=0;i<maxn;i++)
if ((a[i]<<1)>n)
ans=i;
//if (n>1)
out.println(ans);
//else
//out.println(-1);
}
out.flush();
out.close();
}
}

1002 Run

明显搞复杂了,做了一回出题人眼中的“火星人”T_T
平面上的整点就是无法构成正三角形、正五边形、正六边形,没这点见识就只有瞎弄。。。
简化到这个地步,正方形的判断就应该仔细点了吧?四条边及两对角线的长度比较都写上,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Nov 19, 2015 7:32:18 PM
* PrjName: hdu5365
* @semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static Point[] pt;
static boolean check(Point p1,Point p2,Point p3,Point p4){
double[] v=new double[6];
v[0]=Point.dist(p1, p2);
v[1]=Point.dist(p2, p3);
v[2]=Point.dist(p3, p4);
v[3]=Point.dist(p1, p4);
v[4]=Point.dist(p1, p3);
v[5]=Point.dist(p2, p4);
Arrays.sort(v);
return v[0]==v[1]&&v[1]==v[2]&&v[2]==v[3]&&v[4]==v[5];
}
public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
while(in.nextLine()!=null){
int n=in.nextInt();
pt=new Point[n];
for(int i=0;i<n;i++)
pt[i]=new Point(in.nextInt(), in.nextInt());
int ans=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
for(int k=j+1;k<n;k++)
for(int l=k+1;l<n;l++){
Point p1=pt[i],p2=pt[j],p3=pt[k],p4=pt[l];
if (check(p1, p2, p3, p4))
ans++;
}
out.println(ans);
}
out.flush();
out.close();
}
}

1003 The mook jong

多么有教育意义的猜公式,猜不出就别偏执了。。。
f[i]=s[i-3]+1,s[i]=s[i-1]+f[i],…

Bestcoder

1001 Untitled

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 2015年8月1日 下午7:15:14
* PrjName:Bc49-01
* @ Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static ArrayList<Integer> v=new ArrayList<Integer>();
static int[] a,f;
static int lowbit(int x){
return x&(-x);
}
static int get(int x){
int res=0;
while(x>0){
res++;
x-=lowbit(x);
}
return res;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
v.clear();
int n=in.nextInt();
int m=in.nextInt();
for(int i=1;i<=n;i++)
v.add(in.nextInt());
Collections.sort(v);
f=new int[1<<n];
Arrays.fill(f, -1);
f[0]=m;
//out.println(n);
int ans=Integer.MAX_VALUE;
for(int i=0;i<(1<<n);i++)
if (f[i]>0)
for(int j=0;j<n;j++)
if (((1<<j)&i)==0){
int k=i|(1<<j);
f[k]=f[i]%v.get(j);
if (f[k]==0)
ans=Math.min(ans, get(k));
}
if (ans==Integer.MAX_VALUE) out.println(-1);
else out.println(ans);
}
out.flush();
out.close();
}
}

1002 Three Palindromes

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

  • Problem Description
    Can we divided a given string S into three nonempty palindromes?

  • Input
    First line contains a single integer $ T \leq 20 $ which denotes the number of test cases.
    For each test case , there is an single line contains a string S which only consist of lowercase English letters. $ 1\leq |s| \leq 20000 $

  • Output
    For each case, output the “Yes” or “No” in a single line.

  • Sample Input
    2
    abc
    abaadada

  • Sample Output
    Yes
    No

赛时的Hash做法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* 2015年8月1日 下午7:47:15
* PrjName:Bc49-02
* @ Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static long pri=29L;
static long lh(String s,long res,int p){
return res*pri+(long)(s.charAt(p)-'a');
}
static long rh(String s,long res,int p,long m){
return res+(long)(s.charAt(p)-'a')*m;
}
static BitSet v=new BitSet(20010);
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
String s=new String(in.next());
int len=s.length();
//s=s+"#"+(new StringBuffer(s).reverse().toString());
//out.println(s);
boolean ans=false;
long l1=0L,r1=0L,tmp=1L;
for(int i=0;i<len;i++){
if (ans) break;
l1=lh(s, l1, i);
r1=rh(s, r1, i, tmp);
//out.println(i+" "+l1+" "+r1);
tmp*=pri;
//if ((i&1)>0) continue;
if (l1==r1){
//out.println("f"+i);
long l2=0L,r2=0L,tmp2=1L;
v.clear();
for(int j=i+1;j<len;j++){
l2=lh(s,l2,j);
r2=rh(s,r2,j,tmp2);
tmp2*=pri;
//if (((j-i+1)&1)>0) continue;
if (l2==r2) v.set(j);
}

l2=0L;r2=0L;tmp2=1L;
for(int j=len-1;j>i;j--){
l2=lh(s,l2,j);
r2=rh(s,r2,j,tmp2);
tmp2*=pri;
//if (((len-j+1)&1)>0) continue;
if (l2==r2&&v.get(j-1)){
ans=true;break;
}
}
}
}
out.println(ans?"Yes":"No");
}
out.flush();
out.close();
}
}

首次尝试去hack别人不成,还被人黑了,立马TLE

题解介绍的多种方法中,个人认为二分+hash代码量较少,如何实现?

BestCoder 1st Anniversary T_T

1001 Souvenir

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 2015年7月25日 下午7:07:41
* PrjName:0725-01
* @ Semprathlon
*/
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
int m=in.nextInt();
int p=in.nextInt();
int q=in.nextInt();
int ans=0,has=0;
if (m*p>q)
ans+=n/m*q;
else ans+=n/m*m*p;
ans+=n%m*p;
if ((n+m)/m*q<ans) ans=(n+m)/m*q;
out.println(ans);
}
out.flush();
out.close();
}

}

1002 Hidden String

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
用Trie做查询时,往死胡同里钻而且根本停不下来,啊啊啊啊……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdio>
#include <cstring>
char s[200], con[] = "anniversary";

int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", s);
int len = strlen(s);
bool flag = false;
for(int i = 0; i <= 8; i++)
{
for(int j = i + 1; j <= 9; j++)
{
int k = 0;
while(k < len && strncmp(con, s + k, i + 1) != 0)
k ++;
if(k == len)
continue;
k += i + 1;
while(k < len && strncmp(con + i + 1, s + k, j - i) != 0)
k ++;
if(k == len)
continue;
k += j - i;
while(k < len && strncmp(con + j + 1, s + k, 10 - j) != 0)
k ++;
if(k != len)
{
flag = true;
break;
}
}
}
if(flag)
puts("YES");
else
puts("NO");
}
}

这里的枚举倒是简洁得出奇。

1003 Sequence

Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
下意识地做了贪心,果然被hack

Bestcoder

Protest一时爽,final test全爆零 QwQ

1001 wyh2000 and a string problem

1
out.println(str.matches(".*w.*y.*h.*")||str.matches(".*v{2,}.*y.*h.*")?"Yes":"No");

偷懒着用正则表达式,华丽地TLE……
后来就醉了一样的胡乱的写了个匹配也不太行……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.*;
import java.util.*;
import java.math.*;
import java.util.regex.*;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;
public class Main {
static boolean check(String str){
int res=0;
int i,len=str.length();
for(i=0;i<len-1;i++)
if (str.charAt(i)=='w'||str.charAt(i)=='v'&&str.charAt(i+1)=='v'){
res|=1;break;
}
for(;i<len;i++)
if (str.charAt(i)=='y'){
res|=2;break;
}
for(;i<len;i++)
if (str.charAt(i)=='h'){
res|=4;break;
}
return res==7;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputReader in = new InputReader(System.in) ;
PrintWriter out = new PrintWriter(System.out) ;
int T=in.nextInt();
while(T-->0){
String str=in.next();
out.println(check(str)?"Yes":"No");
out.flush();
}
out.close();
}
}

1002 wyh2000 and pupil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
const int maxn=200010;
const int inf=0x7fffffff;
const double eps=1e-3;

short col[maxn];
int s[2];

struct Edge
{
int to,next;
Edge(){}
Edge(int v,int w):to(v),next(w){}
} edge[maxn];
int head[maxn];

void addedge(int u,int v)
{
edge[head[0]]=Edge(v,head[u]);
head[u]=head[0]++;
}

void init()
{
fill(head,head+maxn,-1);
head[0]=1;
}

int bfs(int u)
{
queue<int> q;
q.push(u);
col[u]=0;
while(!q.empty())
{
int p=q.front();
s[col[p]]++;
q.pop();
for(int i=head[p];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if (col[v]>=0)
{
if (col[v]==col[p]) return -1;
}
else
{
col[v]=1^col[p];
q.push(v);
}

}
}
return 0;
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}

int sum=0;
fill(col+1,col+n+1,-1);
for(int i=1;i<=n;i++)
if (col[i]<0)
{
s[0]=s[1]=0;
if (bfs(i)<0)
{
sum=-1;break;
}
else sum+=max(s[0],s[1]);
}
if (n==sum) sum--;
if (sum<1||n-sum<1)
puts("Poor wyh");
else
printf("%d %d\n",sum,n-sum);
}
return 0;
}

事实证明这并不是二分图匹配;并没有什么典型的算法让两个集合中的一个最大……
用DFS或BFS都可实现填色。