Professional Documents
Culture Documents
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
Part
.
1. C
. Ubuntu Linux && Code::Blocks
7
8
12
15
18
2.
20
20
21
21
3.
22
22
23
.
4.
37
39
67
5.
.
6.
107
107
254
254
280
Part
Part
1. C
2.
3.
Part
Part
C/C++ 1
.
, .
C/C++
,
.
,
.
C/C++ ,
, (IDE) ,
.
C/C++ ,
.
,
C/C++ .
()
(Ubuntu Software
Center) .
codeblocks .
codeblocks
install .
Part
()
NewProject ,
Category Console .
Consoleapplication
GO .
()
C++
Next .
, (Desktop
)
Next .
Finish GCC
,
.
Sources
main.cpp
C++
.
10
1
2
3
4
5
6
7
8
9
10
Part
#include<stdio.h>
intn;
intmain()
{
scanf("%d",&n);
printf("%d\n",n);
return0;
}
Build Buildandrun .
11
()
sudoaptgetupdate
sudoaptgetupgrade
sudoaptgetinstallbuildessential
gccv
makev
/
Build Buildandrun
.
12
Part
orwelldevc++
.
13
()
.
(
.)
1
2
3
4
5
6
7
#include<stdio.h>
intmain()
{
printf("hello");
return0;
}
.
14
Part
15
()
Xcode
.
Xcode
CreateanewXcodeproject
.
OSX Application
CommandLineTool .
Next .
.
C++ .
Next .
16
Part
.
(Desktop )
main.cpp .
1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
intn;
intmain()
{
scanf("%d",&n);
printf("%d\n",n);
return0;
}
17
()
(
)
/ (
).
.
.
, . .
-
1
2
3
4
5
6
7
8
9
10
18
#include<stdio.h>
intn;
intmain()
{
scanf("%d",&n);
printf("%d\n",n);
return0;
}
7:
8:
Part
- freopen( ) ()
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
intn;
intmain()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
scanf("%d",&n);
printf("%d\n",n);
return0;
}
7: (
) in.txt
8: (
) out.txt
9:
10:
- fopen( )
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
intn;
intmain()
{
FILE*in=fopen("in.txt","r");
FILE*out=fopen("out.txt","w");
fscanf(in,"%d",&n);
fprintf(out,"%d\n",n);
return0;
}
7: in.txt
8: out.txt
9:
10:
19
()
. , ,
.
.
(computational problem) ,
.
, n , n .
.
.
. .
.
[-1]
?
?
?
, .
?
,
?
(decision problem),
(search problem), (couting problem), (optimization problem),
20
Part
(function problem) ,
.
NP complete 1) ,
.
.
Yes, No
. [-1] , .
,
,
. .
.
. [-1] ,
. .
1) ,
, SAT
.
21
()
.
,
.
, .
.
, , .
S A .
[]
A
(1) .
(2)
(3) .
[]
A
(1) ,
(2)
(3)
(4)
22
Part
[ ( C )]
void A(int S[], int n)
{
int s = 0;
for(int id=1; id<=n; id++)
s = s + S[id];
printf("%d\n",s);
}
C .
.
, .
1
. A .
.
.
.
Online
Judge .
CPU time IO
time .
,
.
23
()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
5:
n
3 <= n <=
100,000 .
intn,S[100000];
9:
intmain()
{
srand(time(NULL));
scanf("%d",&n);
for(inti=0;i<n;i++)
S[i]=rand();
return0;
}
12: rand()
(
15, 31
)
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
24
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intn,S[100000];
intmain()
{
srand(time(NULL));
scanf("%d",&n);
for(inti=0;i<n;i++)
S[i]=rand();
for(inti=0;i<n;i++)
printf("%d",S[i]);
return0;
}
Part
[]
.
(selection sort) .
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
#include<stdio.h>
#include<stdlib.h>
#include<ctime>
intn,S[100000];
voidprint_array()
{
for(inti=0;i<n;i++)
printf("%d",S[i]);
printf("\n");
}
voidswap(inta,intb)
{
intt=S[a];
S[a]=S[b];
S[b]=t;
}
voidselection_sort(void)
{
for(inti=0;i<n1;i++)
for(intj=i+1;j<n;j++)
if(S[i]>S[j])
swap(i,j);
}
intmain()
25
()
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
srand(time(NULL));
scanf("%d",&n);
for(inti=0;i<n;i++)
S[i]=rand();
//print_array();
intstart=clock();
selection_sort();
printf("result=%.3lf(sec)\n",(double)(clock()start)/CLOCKS_PER_SEC);
//print_array();
return0;
}
. , selection_sort
. print_array
. .
[]
[n=1,000]
[n=10,000]
26
Part
[n=100,000]
10 100
. .
(quick sort)
. std::sort( ) .
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
4: std::sort
6: 100
intn,S[1000000];
voidprint_array()
{
for(inti=0;i<n;i++)
printf("%d",S[i]);
printf("\n");
}
intmain()
{
srand(time(NULL));
scanf("%d",&n);
for(inti=0;i<n;i++)
S[i]=rand();
//print_array();
intstart=clock();
std::sort(S,S+n);
27
()
26
27
28
29
30
printf("result=%.3lf(sec)\n", (double)(clock()-start)/CLOCKS_PER_SEC);
//print_array();
}
[n=100,000]
[n=1,000,000]
100 0.2 ,
.
C++ std::sort()
. ,
.
std::sort() lg , , , std::vector,
std::list, std::set, std::map
. .
std::sort( , , [ ]);
28
Part
[ ]
bool compare(int a, int b) //
{
return a < b;
//
[ ]
bool compare(int a, int b) //
{
return a > b;
//
[x, y POINT x ]
bool compare(POINT a, POINT b) //POINT
{
return a.x < b.x;
//x
[x, y POINT 1 x, 2 y ]
bool compare(POINT a, POINT b)
{
// POINT
// x
4 . compare
std::sort() .
29
()
[S n-1 compare ]
std::sort(S, S+n, compare);
std::sort()
.
,
STL(Standard Template Library) . STL
sort, list, vector, set, map .
.
.
1. pivot() .
2. pivot , pivot .
3. pivot ,
1 .
. 1 pivot()
,
pivot .
pivot ,
. .
.
pivot
pivot .
.
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Part
voidswap(inta,intb)
{
intt=S[a];
S[a]=S[b];
S[b]=t;
}
voidquick_sort(ints,inte)
{
if(s<e)
{
intp=s,l=s+1,r=e;
while(l<=r)
{
while(l<=e&&S[l]<=S[p])l++;
while(r>=s+1&&S[r]>=S[p])r;
if(l<r)swap(l,r);
}
swap(p,r);
quick_sort(s,r1);
quick_sort(r+1,e);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
4: std::sort
6: 100
intn,S[1000000];
voidprint_array()
{
for(inti=0;i<n;i++)
printf("%d",S[i]);
printf("\n");
}
31
()
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
32
voidswap(inta,intb)
{
intt=S[a];
S[a]=S[b];
S[b]=t;
}
voidquick_sort(ints,inte)
{
if(s<e)
{
intp=s,l=s+1,r=e;
while(l<=r)
{
while(l<=e&&S[l]<=S[p])l++;
while(r>=s+1&&S[r]>=S[p])r;
if(l<r)swap(l,r);
}
swap(p,r);
quick_sort(s,r1);
quick_sort(r+1,e);
}
}
intmain()
{
srand(time(NULL));
scanf("%d",&n);
for(inti=0;i<n;i++)
S[i]=rand();
//print_array();
intstart=clock();
quick_sort(0,n1);
printf("result=%.3lf(sec)\n",(double)(clock()start)/CLOCKS_PER_SEC);
//print_array();
return0;
}
Part
[n=100,000]
[n=1,000,000]
33
Part
4.
5.
6.
Part
Part
. ,
.
.
.
.
,
.
.
.
.
,
,
.
37
()
. ,
.
.
. ,
.
,
.
Windows
.
.
.
C
.
.
38
Part
,
.
.
.
.
, . 2, 3
.
. 1
1 2
.
.
,
.
.
.
.
`
.
39
()
,
.
log .
( ,
, .
.)
.
.
40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Part
#include<stdio.h>
intS[100],n,k;
intfind(ints,inte)
{
while(s<=e)
{
intm=(s+e)/2;
if(S[m]==k)returnm;
if(S[m]>k)e=m1;
elses=m+1;
}
return1;
}
intmain()
{
scanf("%d%d",&n,&k);
for(inti=0;i<n;i++)
scanf("%d",&S[i]);
printf("%d\n",find(0,n1));
return0;
}
41
()
1
(S)
9 ,
.
, 9
3, 29, 38, 12, 57, 74, 40, 85, 61
, 85, 8 .
.
100 .
, .
85
29
38
12
57
74
40
85
61
: (2007 )
42
Part
1
. 9
.
.
. ans ,
index .
ans .
100 , 0 .
ans
ans , index .
, ans index .
.
29
38
12
57
74
40
85
61
index
index=?ans
43
()
[1 ]
A
29
38
12
57
74
40
85
61
index
index=0ans
[2 ]
A
29
38
12
57
74
40
85
61
index
index=1ans
29
[3 ]
A
29
38
12
57
74
40
85
61
index
index=2ans
38
[4 ]
A
29
38
12
57
74
40
85
61
index
index=3ans
38
44
Part
[9 ]
A
29
38
12
57
74
40
85
61
index
index=8ans
85
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#defineMAXN9
intmain()
{
inti,ans,index,A[MAXN+1];
ans=0;
index=0;
for(i=1;i<MAXN+1;i++)
scanf("%d",&A[i]);
for(i=1;i<MAXN+1;i++)
if(ans<A[i])
{
ans=A[i];
index=i;
}
printf("%d\n%d\n",ans,index);
return0;
}
1 .
45
()
, main
.
. ,
, .
.
1
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#defineMAXN9
intans,index,A[MAXN+1];
voidsolve(void)
{
for(inti=1;i<10;i++)
{
scanf("%d",&A[i]);
if(ans<A[i])ans=A[i],index=i;
}
}
intmain()
{
solve();
printf("%d\n%d\n",ans,index);
return0;
}
. ans,
index , .
.
.
46
Part
, 0 .
solve ,
. , ,
, .
for i .
i
.
.
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#defineMAXN9
intans,A[MAXN+1];
voidsolve(void)
{
for(inti=1;i<10;i++)
{
scanf("%d",A+i);
if(A[ans]<A[i])ans=i;
}
}
intmain()
{
solve();
printf("%d\n%d\n",A[ans],ans);
return0;
}
47
()
48
Part
2
3
3 3
.
3 .
** 3 ?
.
3 , "" .
(n 10 .).
1 , 3 6 9
X .
12X45X7
49
()
[ 1]
.
.
3 .
.
n % 3 == 0
.
n / 3 * 3 == n
3 3 .
1 10 3 .
n
n/3
n/3*3==n
False
False
True
False
False
True
False
False
True
10
False
.
, 1 1 3 3
50
Part
X .
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
intn;
voidsolve(void)
{
for(inti=1;i<=n;i++)
{
if(i%3==0)printf("X");
elseprintf("%d",i);
}
}
intmain()
{
scanf("%d",&n);
solve();
return0;
}
n .
, solve for i 1
. 0 1 ,
.
.
solve
. main solve
.
51
1
2
3
4
5
6
7
8
52
()
voidsolve(void)
{
for(inti=1;i<=n;i++)
{
if(i/3*3==i)printf("X");
elseprintf("%d",i);
}
}
5:
Part
3
linear structure search
n .
, , .
n .
n .
.
(, 2 <= n <= 1,000,000 , 100,000,000 .)
. -1 .
8
1235791115
11
3
257
53
()
. [s, e] m
.
A , A[m] == k A[m] > k, A[m] < k
. .
11
15
index
11
[1 ]
A
11
15
index
11
[2 ]
A
11
15
index
11
54
Part
[3 ]
A
11
15
index
11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
intn,k,A[1000001];
intsolve(ints,inte)
{
intm;
while(es>=0)
{
m=(s+e)/2;
if(A[m]==k)
returnm+1;
if(A[m]<k)s=m+1;
elsee=m1;
}
return1;
}
intmain()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d",A+i);
scanf("%d",&k);
printf("%d\n",solve(0,n1));
return0;
}
55
()
.
.
56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
intn,k,A[1000001];
intsolve(ints,inte)
{
if(s>e)
return1;
intm=(s+e)/2;
if(A[m]==k)
returnm+1;
if(A[m]<k)
returnsolve(m+1,e);
else
returnsolve(s,m1);
}
intmain()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d",A+i);
scanf("%d",&k);
printf("%d\n",solve(0,n1));
return0;
}
Part
4
lower bound
n k
.
, ,
.
n .
n .
k .
(, 2 <= n <= 1,000,000 , 100,000,000 .)
. k n+1
.
5
13577
7
8
1235791115
6
5
12345
7
5
22222
57
()
lower bound
. lower bound .
, lower bound
.
lower bound
.
[s, e] , m , A[m-1] <k A[m]>=
k m . m 2 .
A[m] == k .
k n+1 ,
[1, n] [1, n+1] .
11
15
index
[1 ]
58
11
15
index
Part
[2 ]
A
11
15
index
[3 ]
A
11
15
index
[4 ]
A
11
15
index
< 4 k
. >
59
()
lower bound .
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
intn,k,A[1000001];
intsolve(ints,inte)
{
intm;
while(es>0)
{
m=(s+e)/2;
if(A[m]<k)s=m+1;
elsee=m;
}
returne+1;
}
intmain()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d",A+i);
scanf("%d",&k);
printf("%d\n",solve(0,n));
return0;
}
60
Part
std::lower_bound( ) .
S n-1 k low bound
std::lower_bound( ) .
std::lower_bound( S, S+n, k, [compare] );
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
#include<algorithm>
intn,k,A[1000001];
intmain()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d",A+i);
scanf("%d",&k);
printf("%d\n",std::lower_bound(A,A+n,k)A+1);
}
12 .
std::lower_bound( A, A+n, k ) A[0]~A[n-1]
, k lower bound .
A k A ,
0 1 .
std::lower_bound(A, A+n, k)-A+1 .
61
()
5
upper bound
n k
.
, ,
.
n, n .
k .
(, 2 <= n <= 1,000,000, 100,000,000 .)
. k n+1
.
5
13557
5
8
1277771115
7
5
12345
7
5
22222
1
62
Part
upper bound
. upper bound lower bound
. upper bound k .
upper bound lower bound
.
upper bound lower bound .
k ?
.
upper bound [s, e] , m
, A[m-1] <= k A[m] > k m . m 2
. A[m] == k
.
k n+1 ,
[1, n] [1, n+1] .
upper bound .
11
15
index
63
()
[1 ]
A
11
15
index
[2 ]
A
11
15
index
[3 ]
A
11
15
index
64
Part
[4 ]
A
11
15
index
< 6
. >
upper bound lower bound .
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
intn,k,A[1000001];
intsolve(ints,inte)
{
intm;
while(es>0)
{
m=(s+e)/2;
if(A[m]<=k)s=m+1;
elsee=m;
}
returne+1;
}
intmain()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d",A+i);
scanf("%d",&k);
printf("%d\n",solve(0,n));
}
65
()
bound
.
6 e-s > 1
.
upper bound lower bound STL .
S n-1 k upper bound
std::upper_bound()
.
std::upper_bound( S, S+n, k, [compare] );
1
2
3
4
5
6
7
8
9
10
11
12
13
66
#include<stdio.h>
#include<algorithm>
intn,k,A[1000001];
intmain()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d",A+i);
scanf("%d",&k);
printf("%d\n",std::upper_bound(A,A+n,k)A+1);
}
Part
12 .
std::upper_bound( A, A+n, k ) A[0]~A[n-1]
, k upper bound .
lower bound . std::upper_bound(A,
A+n, k)-A+1 .
.
, .
.
.
.
(depth first search, dfs)
(breadth firth search, bfs) ,
.
-
.
(vertex) (edge) .
67
()
,
15
,
, ,
7 () .
,
, .
, .
- (path) (cycle)
s t , s t
.
s s .
s t ,
s t .
- (degree)
.
68
Part
-
(adjacency matrix) (adjacency list)
.
,
, 2 .
, 2 .
7 11
69
()
[-2]
7 11
1 2 47
1 3 69
n m
.
m
.
2 4 57
2 5 124
3 4 37
3 5 59
3 6 86
4 6 27
4 7 94
5 7 21
6 7 40
-
[-2] 2 .
2 .
2 .
, . , 3 4
1, 37 . 1 ,
.
2 . 100
.
70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
intn,m,G[101][101];
intmain()
{
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++)
{
inta,b,w;
scanf("%d%d%d",&a,&b,&w);
G[a][b]=G[b][a]=w;
}
}
Part
10: a, b
, w
12:
1,
G[a][b]
.
-
. , 0
.
0 .
0
.
0 .
[-10]
71
()
[-11]
.
1 1-2 47 1-3
69 .
std::vector()
. , .
, ,
.
.
- (dfs)
(cycle) . [-13] .
,
.
72
Part
10 (vertex) 9 (edge)
,
, .
1~3
3 .
.
.
(backtrack) .
. (stack) (recursion)
.
73
()
4 ~ 6
4, 5, 6 .
. .
7~9
74
Part
,
.
. (backtracking)
.
1
2
3
4
5
6
7
8
9
10
11
boolvisited[101];
voiddfs(intk)
{
for(inti=0;i<G[i].size();i++)
if(!visited[G[k][i].to])
{
visited[G[k][i].to]=true;
dfs(G[k][i]);
}
return;
}
1:
4: k
5:
7:
8:
10:
backtrack
.
.
.
.
1
2
3
4
5
6
7
8
9
10
11
boolvisited[101];
voiddfs(intk)
{
for(inti=1;i<=n;i++)
if(G[k][i]&&!visited[G[k][i]])
{
visited[G[k][i]]=true;
dfs(G[k][i]);
}
return;
}
1:
4:
5: k
,
7:
8:
10:
backtrack
75
()
.
,
.
76
Part
1
(S)
.
.
.
0 1 . 0 1
. 1
.
[ 1]
[ 2]
[ 2] [ 1] .
,
.
, n .(n 30 )
n n 0 1 .
.
.
7
0110100
0110101
1110101
0000111
0100000
0111110
0111000
3
9
8
7
77
()
, .
1 , , , 1
.
.
7
0 1 1 0 1 0 0
0 1 1 0 1 0 1
1 1 1 0 1 0 1
0 0 0 0 1 1 1
0 1 0 0 0 0 0
0 1 1 1 1 1 0
0 1 1 1 0 0 0
(0, 0) (a, b)
1
.
. ,
std::sort()
.
,
, flood fill . .
78
Part
.
runtime error .
release 10 .
,
.
.
[ =2]
2 1
2 .
(0,0) .
(0,0) 0 .
Size
Size
index
(0,1) , 1
dfs ,,,
2
.
7 Size[2]
, 1
Size
index
79
80
()
(0,2) , 1
(0,1) 2 ,
[ =3]
Size
index
[ =3]
(0,3),(0,4) ,(0,5)
dfs floodfill
,,, 3
.
Size[3] 8
Size
index
, 1
[ =4]
dfs
4 .
(0,6) (4,0) 1
.(4,1) 1
Size[4]
, 5 .
Size
index
Part
[ =5]
1
.
,
7,8,9 .
Size
index
std::sort()
, .
1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
#include<algorithm>
intn,A[101][101],cnt,Size[101];
intmain()
{
input();
solve();
output();
}
main . , , ,
.
A (0 , 1 ), Size
, dx, dy 4 x, y
, cnt .
81
()
1
2
3
4
5
6
7
8
boolsafe(inta,intb)
{
return(0<=a&&a<n)&&(0<=b&&b<n);
}
boolcmp(inta,intb)
{
returna>b;
}
safe ,
false .
cmp
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
82
voiddfs(inta,intb,intc)
{
A[a][b]=c;
if(safe(a+1,b)&&A[a+1][b]==1)
dfs(a+1,b,c);
if(safe(a1,b)&&A[a1][b]==1)
dfs(a1,b,c);
if(safe(a,b+1)&&A[a][b+1]==1)
dfs(a,b+1,c);
if(safe(a,b1)&&A[a][b1]==1)
dfs(a,b1,c);
}
voidsolve()
{
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
if(A[i][j]==1)
{
cnt++;
dfs(i,j,cnt+1);
}
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
if(A[i][j])
Size[A[i][j]2]++;
std::sort(Size,Size+cnt,cmp);
}
Part
dfs 4 dx, dy .
1
2
3
4
5
6
7
8
9
intdx[4]={1,0,1,0},dy[4]={0,1,0,1};
voiddfs(inta,intb,intc)
{
A[a][b]=c;
for(inti=0;i<4;i++)
if(safe(a+dx[i],b+dy[i])&&A[a+dx[i]][b+dy[i]]==1)
dfs(a+dx[i],b+dy[i],c);
}
1: 4
.
.
4 2 .
1 2
.
22~25 Size .
. 26 Size
.
1
2
3
4
5
6
7
8
9
10
11
12
13
voidinput()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
scanf("%d",&A[i][j]);
}
voidoutput()
{
printf("%d\n",cnt);
for(inti=0;i<cnt;i++)
printf("%d\n",Size[i]);
}
83
()
input.
.
scanf("%1d",&A[i][j]);
, 1 .
, ,
.
scanf(%2d%2d%2d-%1d%d, &year, &mon, &day, &gender, &etc);
.
output() ,
.
84
Part
2
n-queen
n-queen problem .
n*n n queen
.
n 4 queen
.
queen n
.
n .( 3 <= n <= 9 )
85
()
.
. (8
.)
. 4*4 .
1 1
.
86
Part
1. , .
2. .
3. n .
4. .
5. .
.
.
, .
n k k
.
.
2
. 2 .
.
+ . n 4 +
2 8.
+
.
87
()
. n 4 3
3 . n
n+(-) , .
.
[ ]
4*4
.
.
col
/
col:,/: 1,\: 2
\
1
[1]
1 1 ,
col 1 col[1]
col
1 inc[1+1]
\
1
88
(n=4)
3
dec[4(11)]
Part
[2 ]
2 1 .
col[1]
.
col
\
1
[2 ]
Q
Q
2 2 .
col[2] OK!
col
inc[2+2]
OK!
dec[4(2+2)+1]
!
[3 ]
Q
Q
Q
3 1,2,3,4
3 .
col
!!!
\
1
89
()
[]
col
\
1
[2 ]
2 3 , 2
4 !!
col
\
1
[3 ]
Q
Q
3 1
Q
Q
col
Q
4
\
1
90
,3 2 .
, 2
1 2
.
7
Part
[1 ]
1 1
, ,1 2
.
col
\
1
[ ]
Q
Q
2 4,3 1,4 3
Q
col
.
8
[ ]
Q
Q
1 3,2 1,3 4
,4 2
Q
col
.
8
91
()
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
#include<stdio.h>
intn,ans,col[10],inc[20],dec[20];
voidsolve(intr)
{
if(r>n)
{
ans++;
return;
}
for(inti=1;i<=n;i++)
if(!col[i]&&!inc[r+i]&&!dec[n+(ri)+1])
{
col[i]=inc[r+i]=dec[n+(ri)+1]=1;
solve(r+1);
col[i]=inc[r+i]=dec[n+(ri)+1]=0;
}
}
9:
10:
12: r
15:
17:
(
)
intmain()
{
scanf("%d",&n);
solve(1);
printf("%d",ans);
}
.
, col, inc, dec 3 , 2
.
. 17 .
92
Part
.
- (bfs)
1
. 73 10 9
.
1
1 ~ 4
1 4 1 1
. .
5
10
5 ~ 10
93
()
. 1
(queue) (FIFO)
1 . STL
std::queue() .
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<queue>
boolvisited[101];
voidbfs(intk)
{
std::queue<int>Q;
Q.push(k),visited[k]=1;
while(!Q.empty())
{
intcurrent=Q.front();Q.pop();
for(inti=0;i<G[current].size();i++)
if(!visited[G[current][i]])
{
visited[G[current][i]]=1;
Q.push(G[current][i]);
}
}
}
1: std::queue
2:
5: Queue
6:
Queue
7: Queue
9: Queue
10:
11:
,
13: Queue
,
.
. .
(standard library) , C++
.
94
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<queue>
boolvisited[101];
voidbfs(intk)
{
std::queue<int>Q;
Q.push(k),visited[k]=1;
while(!Q.empty())
{
intcurrent=Q.front();Q.pop();
for(inti=1;i<=n;i++)
if(G[current][i]&&!visited[G[current][i]])
{
visited[G[current][i]]=1;
Q.push(G[current][i]);
}
}
}
Part
1: std::queue()
2:
5: Queue
6:
Queue
7: Queue
9: Queue
10:
11:
,
13: Queue
.
,
.
95
()
3
(L)
.
.
.
0 1 . 0 1
. 1
.
[ 1]
[ 2]
[ 2] [ 1] .
,
.
, n . n 30
n n 0 1 .
.
.
96
7
0110100
0110101
1110101
0000111
0100000
0111110
0111000
3
9
8
7
Part
.
runtime error .
.
, .
flood fill .
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<algorithm>
#include<queue>
structVERTEX{inta,b;};
intn,A[101][101],cnt,Size[101];
intdx[4]={1,0,1,0},dy[4]={0,1,0,1};
intmain()
{
input();
solve();
output();
return0;
}
main() . VERTEX
queue . queue
.
1
2
3
4
5
6
7
8
boolsafe(inta,intb)
{
return(0<=a&&a<n)&&(0<=b&&b<n);
}
boolcmp(inta,intb)
{
returna>b;
}
97
()
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
voidbfs(inta,intb,intc)
{
std::queue<VERTEX>Q;
Q.push((VERTEX){a,b}),A[a][b]=c;
while(!Q.empty())
{
VERTEXcurr=Q.front();Q.pop();
for(inti=0;i<4;i++)
if(safe(curr.a+dx[i],curr.b+dy[i])&&
A[curr.a+dx[i]][curr.b+dy[i]]==1)
{
A[curr.a+dx[i]][curr.b+dy[i]]=c;
Q.push((VERTEX){curr.a+dx[i],curr.b+dy[i]});
}
}
}
voidsolve()
{
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
if(A[i][j]==1)
{
cnt++;
bfs(i,j,cnt+1);
}
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
if(A[i][j])
Size[A[i][j]2]++;
std::sort(Size,Size+cnt,cmp);
}
98
Part
bfs .
, 4
. , .
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
voidinput()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
scanf("%d",&A[i][j]);
}
voidoutput()
{
printf("%d\n",cnt);
for(inti=0;i<cnt;i++)
printf("%d\n",Size[i]);
}
input.
scanf("%1d",&A[i][j])
.
, .
99
()
4
h*w .
, ".", "#" ,
"S" "G" .
, S G
.
h w .
(, h, w 5 100 .)
h w .
".", "#", "S", "G" . S
G
.
, -1 .
55
#S###
#...#
#.#.#
#....
###G#
100
Part
,
.
.
S G 6 .
.
.
Q ueue
#S###
#...#
#.#.#
#....
###G#
S (0,1)
#S###
#1..#
#.#.#
#....
###G#
.
.
0,1
(0, 1)
1 .
(1,1) (1,1)
.
Q ueue
1,1
#S###
#12.#
#2#.#
#....
###G#
.
(1,1).
(1,1) +
1 , .
(1,2) (2,1) .
Q ueue
1,2
2,1
101
Q ueue
Queue
Q ueue
Q ueue
102
2,1
1,3
3,1
2,3
()
#S###
#123#
#2#.#
#....
###G#
(1,2)
#S###
#123#
#2#.#
#3...
###G#
(2, 1) ,
#S###
#123#
#2#4#
#3...
###G#
(1, 3) ,
#S###
#123#
#2#4#
#34..
###G#
(3, 1) ,
.
(1,3) 3
.
1,3
(3,
1) 3
.
3,1
(2,3)
4
2,3
3,2
(3,
2) 4 ,
Q ueue
Q ueue
Q ueue
3,2
Part
#S###
#123#
#2#4#
#345.
###G#
(2, 3) ,
#S###
#123#
#2#4#
#345.
###G#
(3, 2) ,
#S###
#123#
#2#4#
#3456
###6#
(3, 3) ,
#S###
#123#
#2#4#
#3456
###6#
(3,4)
(3,
3) 5 ,
3,3
3,3
3,4
(3,
4) (4,3) .
4,3
, (4, 3)
.
(4, 3) ,
.
Q ueue
,
6 .
103
()
,
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<queue>
structVERTEX{inta,b;};
inth,w,Sa,Sb,Ga,Gb,visited[101][101];
intdx[4]={1,0,1,0},dy[4]={0,1,0,1};
charM[101][101];
boolsafe(inta,intb)
{
return(0<=a&&a<h)&&(0<=b&&b<w);
}
intmain()
{
input();
printf("%d\n",solve());
}
h, w , Sa, Sb ,
Ga, Gb , visited
, dx, dy 4
.
VERTEX . M
.
safe
.
104
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
Part
voidinput(void)
{
scanf("%d%d",&h,&w);
for(inti=0;i<h;i++)
{
scanf("%s",M[i]);
for(intj=0;j<w;j++)
if(M[i][j]=='S')Sa=i,Sb=j;
elseif(M[i][j]=='G')Ga=i,Gb=j,M[i][j]='.';
}
}
intsolve(void)
{
std::queue<VERTEX>Q;
Q.push((VERTEX){Sa,Sb}),visited[Sa][Sb]=0;
while(!Q.empty())
{
VERTEXcur=Q.front();Q.pop();
if(cur.a==Ga&&cur.b==Gb)break;
for(inti=0;i<4;i++)
{
inta=cur.a+dx[i],b=cur.b+dy[i];
if(safe(a,b)&&!visited[a][b]&&M[a][b]=='.')
{
visited[a][b]=visited[cur.a][cur.b]+1;
Q.push((VERTEX){a,b});
}
}
}
returnvisited[Ga][Gb];
}
. G
. .
, .
105
()
28 .
28 .
1
2
3
4
5
6
7
8
if(safe(a,b)&&!visited[a][b]&&M[a][b]=='.')
{
VERTEXtemp;
temp.a=a;
temp.b=b;
visited[a][b]=visited[cur.a][cur.b]+1;
Q.push(temp);
}
{ }
. .
.
1
2
3
4
5
6
if(safe(a,b)&&!visited[a][b]&&M[a][b]=='.')
{
VERTEXtemp=(VERTEX){a,b};
visited[a][b]=visited[cur.a][cur.b]+1;
Q.push(temp);
}
VERTEX 28 .
1
2
3
4
5
if(safe(a,b)&&!visited[a][b]&&M[a][b]=='.')
{
visited[a][b]=visited[cur.a][cur.b]+1;
Q.push((VERTEX){a,b});
}
13 31 .
, .
, .
visited
106
Part
. 1, 0 ,
0, .
.
.
.
.
,
.
.
,
.
.
. 1
2 .
. ,
.
, .
107
()
1
1
n n .
10 1, 2, 5, 10 18 10
.
n .
(, 1 <= n <= 100,000)
n .
108
10
18
Part
.
.
. .
.
.
n % x == 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
intn;
intsolve()
{
intans=0;
for(inti=1;i<=n;i++)
if(n%i==0)
ans+=i;
returnans;
}
intmain()
{
scanf("%d",&n);
printf("%d\n",solve());
}
, 10
. .
109
()
2
(L)
< 1> 99 81 ,
.
, 81 90,
5 7 .
.
100 .
,
.
.
32385341774255265
10739428852147263
874218785345188453
342864851216753655
21774535287590761
258765152811372874
65277541789786439
47477045236534144
871382383112292980
90
57
: (2007 )
110
Part
2 . 2
,
, .
5 4 2
5 4 2
11 16
12 17
10 11 12
13 18
13 14 15 16
14 19
17 18 19 20
10 15 20
[2 ]
[2 ]
1
2
3
4
5
6
for(introw=0;row<5;row++)
{
for(intcol=0;col<4;col++)
printf("[%d,%d]",row,col);
puts("");
}
1
2
3
4
5
6
for(intcol=0;col<4;col++)
{
for(introw=0;row<5;row++)
printf("[%d,%d]\n",row,col);
puts("");
}
111
()
[2 ]
.
ans 0 .
ans 0
. ans 0
.
. int
0x7fffffff(2,147,483,647), 0x80000000(-2,147,483,648).
, , 16 .
(overflow) . .
1
2
intmax=0x7fffffff;
max=max+1;
max , 1
max . 2
.
987654321 2
.
.
112
Part
, . .
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
#include<stdio.h>
intA[10][10],ans,mi,mj;
voidinput()
{
for(inti=0;i<9;i++)
for(intj=0;j<9;j++)
scanf("%d",&A[i][j]);
}
intsolve()
{
for(inti=0;i<9;i++)
for(intj=0;j<9;j++)
if(ans<A[i][j])
{
ans=A[i][j];
mi=i+1;
mj=j+1;
}
}
intmain()
{
input();
solve();
printf("%d\n%d%d\n",ans,mi,mj);
return0;
}
.
, , ans, mi, mj
mi, mj .
113
()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
intA[10][10],mi,mj;
voidinput_solve()
{
for(inti=0;i<9;i++)
for(intj=0;j<9;j++)
{
scanf("%d",&A[i][j]);
if(A[mi][mj]<A[i][j])
mi=i,mj=j;
}
}
intmain()
{
input_solve();
printf("%d\n%d%d\n",A[mi][mj],mi+1,mj+1);
return0;
}
114
Part
3
(S)
.
. , . ,
9m ,
1m, 4m
2m, 3m, 4m
3m 3 .
n .
: (2002 )
115
()
,
. ,
.
1
, , .
3 .
. ,
.
, .
.
3 .
1
2
3
4
5
6
7
8
9
intcount=0;
for(inta=1;a<=n;a++)
for(intb=a;b<=n;b++)
for(intc=b;c<=n;c++)
{
if(count%5==0)puts("");
count++;
printf("[%d%d%d]\t",a,b,c);
}
1 5 .
, .
. , 5 count
116
Part
.
[1
[1
[1
[2
[2
[3
[3
1
2
3
2
3
3
5
1]
2]
4]
2]
4]
3]
5]
[1
[1
[1
[2
[2
[3
[4
1
2
3
2
3
3
4
2]
3]
5]
3]
5]
4]
4]
[1
[1
[1
[2
[2
[3
[4
1
2
4
2
4
3
4
3]
4]
4]
4]
4]
5]
5]
[1
[1
[1
[2
[2
[3
[4
1
2
4
2
4
4
5
4]
5]
5]
5]
5]
4]
5]
[1
[1
[1
[2
[2
[3
[5
1
3
5
3
5
4
5
5]
3]
5]
3]
5]
5]
5]
.
. .
. .
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
intn;
intsolve()
{
intcnt=0;
scanf("%d",&n);
for(inta=1;a<=n;a++)
for(intb=a;b<=n;b++)
for(intc=b;c<=n;c++)
if(a+b+c==n&&a+b>c)
cnt++;
returncnt;
}
intmain()
{
printf("%d\n",solve());
}
117
()
9~13 3 .
.
12 .
.
.
1
2
3
4
5
6
7
8
9
118
#include<stdio.h>
intcnt;
voidsolve(intn,inta,intb,intc)
{
if(a+b+c==n)
{
if(a<=b&&b<=c&&a+b>c)
cnt++;
6:
12: a 1
13: b 1
14: c 1
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Part
return;
}
solve(n,a+1,b,c);
solve(n,a,b+1,c);
solve(n,a,b,c+1);
}
intmain(void)
{
intn;
scanf("%d",&n);
solve(n,1,1,1);
printf("%d\n",cnt);
}
. 1
n 5 1, 2, 2 , b 1 1, 2, 2
c 1 1, 2, 2 . ,
.
a, b, c chk[a][b][c]
1 . .
.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
intcnt,chk[21][21][21];
voidsolve(intn,inta,intb,intc)
{
if(a+b+c==n)
{
if(a<=b&&b<=c&&a+b>c&&chk[a][b][c]==0)
{
cnt++;
chk[a][b][c]=1;
}
3: chk
7:
9:
12:
16: a 1
17: b 1
18: c 1
119
()
14
15
16
17
18
19
20
21
22
23
24
25
26
27
return;
}
solve(n,a+1,b,c);
solve(n,a,b+1,c);
solve(n,a,b,c+1);
}
intmain(void)
{
intn;
scanf("%d",&n);
solve(n,1,1,1);
printf("%d\n",cnt);
}
. 10,000
.
.
120
Part
4
(S)
.
3 , 1
.
1 1*6
.
1 0 2 0 4 3
1*3 , (1 0 2), (0 2 0), (2
0 4), (0 4 3) 4 .
0 + 4 + 3 = 7.
7 .
.
,
, .
N . ( N <= 100 )
W . ( W <= N )
W .
7 . 0 .
6
3
102043
121
()
.
.
,
.
.
.
7 .
0 1
, .
. .
1
2
3
4
5
6
122
for(inti=0;i<NW+1;i++)
{
for(intj=0;j<W;j++)
printf("%d",i+j);
puts("");
}
Part
, .
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
1
2
3
4
5
6
for(inti=0;i<N;i++)
{
for(intj=0;j<W;j++)
printf("%d",i+j);
puts("");
}
.
.
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
7 8 9 10 11
123
()
0
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
intdata[101],N,W,ans=0;
intmain()
{
scanf("%d%d",&N,&W);
for(inti=0;i<N;i++)
scanf("%d",data+i);
for(inti=0;i<N+W1;i++)
{
intsum=0;
for(intj=0;j<W;j++)
sum+=data[i+j];
if(sum>ans)ans=sum;
}
printf("%d",ans);
}
1~N W
.
N 100,000
.
124
Part
5
(L)
.
3 , 2
.
1 1*6
.
1 0 2 0 4 3
1*3 , (1 0 2), (0 2 0), (2
0 4), (0 4 3) 4 .
0 + 4 + 3 = 7.
7 .
.
, ,
.
N, M . ( N, M <= 100 )
W, H . ( W <= N, H <= M )
N*M .
7 . 0 .
26
13
102043
340201
125
()
[ 4] (S) 2 .
2 .
,
.
(1,1)~(N, M) .
(N=2, M=6, W=1, H=3) , .
7 .
. .
126
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
Part
#include<stdio.h>
intdata[100][100];
intN,M,H,W;
intres=0;
intmain()
{
scanf("%d%d",&N,&M);
scanf("%d%d",&H,&W);
for(inti=0;i<N;i++)
for(intj=0;j<M;j++)
scanf("%d",&data[i][j]);
for(inti=0;i<NH+1;i++)
{
for(intj=0;j<MW+1;j++)
{
intsum=0;
for(inta=0;a<H;a++)
for(intb=0;b<W;b++)
sum+=data[i+a][j+b];
if(sum>res)res=sum;
}
}
printf("%d",res);
}
N*M H*W
.
N*M 300,000 .
1 2 2 4
.
.
127
()
.
, 19 .
. , .
, .
. ,
,
.
,
.
19 19 , 1,
2, 0 , .
1, 2,
0 .
(
, )
.
: (2003 )
128
32
Part
.
. .
.
2 2
.
. .
.
1. (0, 0) (18, 18) .
2. .
3. 2 .
4. .
2 .
8 .
.
, .
8
.
129
()
(0, 0) , 8 .
, . 8
?
.
. . , ,
, , 4 .
4 . .
.
130
Part
6 ()
1
.
.
. .
.
online judge
.
.
131
()
1
2
#include<stdio.h>
inta[19+2][19+2];
132
4
5
intmain()
{
inti,j;
7
8
for(i=1;i<=19;i++)
for(j=1;j<=19;j++)
scanf("%d",&a[i][j]);
10
11
for(i=1;i<=19;i++)
for(j=1;j<=19;j++)
12
if(a[i][j]!=0)
13
14
{
if(a[i][j1]!=a[i][j]&&search1(a[i][j],i,j,1)==1)
15
16
17
printf("%d\n%d%d",a[i][j],i,j);
return0;
18
19
20
if(a[i1][j1]!=a[i][j]&&search2(a[i][j],i,j,1)==1)
{
21
printf("%d\n%d%d",a[i][j],i,j);
22
23
return0;
}
24
if(a[i1][j]!=a[i][j]&&search3(a[i][j],i,j,1)==1)
25
26
{
printf("%d\n%d%d",a[i][j],i,j);
27
return0;
28
29
}
if(a[i+1][j1]!=a[i][j]&&search4(a[i][j],i,j,1)==1)
30
31
32
printf("%d\n%d%d",a[i][j],i,j);
return0;
33
34
35
}
printf("0");
36
return0;
37
2: 1919,
0
12:
35:
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
intsearch1(intcolor,inti,intj,intcnt)
{
for(;color==a[i][j+1];j++)
cnt++;
returncnt==5?1:0;
}
intsearch2(intcolor,inti,intj,intcnt)
{
for(;color==a[i+1][j+1];i++,j++)
cnt++;
returncnt==5?1:0;
}
Part
1:
7:
14:
21:
intsearch3(intcolor,inti,intj,intcnt)
{
for(;color==a[i+1][j];i++)
cnt++;
returncnt==5?1:0;
}
intsearch4(intcolor,inti,intj,intcnt)
{
for(;color==a[i1][j+1];i,j++)
cnt++;
returncnt==5?1:0;
}
133
()
7
(S)
GSHS A .
n, m
GSHS 1 A n
.
, n 10 , m 30 ,
200
.
.(, a->b
, .)
: 1357, : 69+59+21=149
n m .
m . ( ,
.)
. -1 .
711
1247
1369
2457
25124
3437
3559
3686
4627
4794
5721
6740
134
149
Part
.
,
.
.
. .
.
2,3 2
.
=
2 47,
4,5 4
.
=
4 104 ,3,6,
7 3 .
=
135
()
3 , 5
.
=
5 ,7
=
. 221
221
=221
267 .
.
=221
136
Part
171 .
=171
198 .
.
=171
302 .
.
=171
329 .
.
=171
137
()
192 .
.
=171
308 .
.
=171
149 .
.
=149
149 .
.
1
2
3
4
5
6
138
#include<stdio.h>
intn,m,G[11][11],sol=0x7fffffff,chk[11];
voidsolve(intV,intW)
{
if(V==n)
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
Part
{
if(W<sol)sol=W;
return;
}
for(inti=1;i<=n;i++)
if(!chk[i]&&G[V][i])
{
chk[i]=1;
solve(i,W+G[V][i]);
chk[i]=0;
}
}
intmain(void)
{
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++)
{
ints,e,w;
scanf("%d%d%d",&s,&e,&w);
G[s][e]=G[e][s]=w;
}
solve(1,0);
printf("%d\n",sol==0x7fffffff?1:sol);
return0;
}
.
solve(a, b) a b , chk
. 14
chk , 16
.
6 ,
.
.
139
()
.
,
.
1) 1
2) 1
3) 5
4) 5
5) 10
6) 10
6 .
.
. , 7 34 ,
7 17 27 32 33 34
5 .
140
734
Part
. .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
inta,b;
intres=40;
voidf(inttemp,intcnt)
{
if(cnt>res)return;
if(temp==b)
{
if(cnt<res)res=cnt;
return;
}
f(temp+10,cnt+1);f(temp+5,cnt+1);f(temp+1,cnt+1);
f(temp10,cnt+1);f(temp5,cnt+1);f(temp1,cnt+1);
}
intmain()
{
scanf("%d%d",&a,&b);
f(a,0);
printf("%d",res);
return0;
}
res 40 0~40
40 .
f .
.
f(temp, cnt) = temp , cnt
141
()
. 6
res . res 40
.
.
,
, .
, , .
14~15
.
14
15
16
17
18
19
if(temp<b){
f(temp+10,cnt+1);f(temp+5,cnt+1);f(temp+1,cnt+1);
}
else{
f(temp10,cnt+1);f(temp5,cnt+1);f(temp1,cnt+1);
}
142
Part
.
res , res
.
. res
,
.
,
.
.
1
2
3
4
5
6
7
#include<stdio.h>
#include<queue>
usingnamespacestd;
structELE{intv,cnt;};
queue<ELE>Q;
143
()
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
intmain()
{
inta,b,i;
ELEtemp;
scanf("%d%d",&a,&b);
Q.push({a,0});
while(!Q.empty())
{
temp=Q.front(),Q.pop();
if(temp.v==b)
break;
if(temp.v<b)
{
Q.push({temp.v+10,temp.cnt+1});
Q.push({temp.v+5,temp.cnt+1});
Q.push({temp.v+1,temp.cnt+1});
}
else
{
Q.push({temp.v10,temp.cnt+1});
Q.push({temp.v5,temp.cnt+1});
Q.push({temp.v1,temp.cnt+1});
}
}
printf("%d",temp.cnt);
}
.
b ,
.
2 STL queue . 5
ELE Q , 13~31
. 13, 21~23, 27~29 {, }
.
7 34 .
144
Part
34 .
3
cnt .
145
()
9
. (prime number)
1 1 .
. , ,
.
.
.
7193 . 7193 , 7193 3
719 . 719 9 71
. 71 1 7 . 7193
.
1. n
.
2. .
23
29
31
37
53
59
71
73
79
9
146
Part
n
. n ,
.
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
#include<stdio.h>
intn,cnt;
intisprime(intx)
{
if(x<2)return0;
for(inti=2;i*i<=x;i++)
if(x%i==0)
return0;
return1;
}
voidf(intnum,intlen)
{
if(len==n)
{
if(num==0)return;
intflag=1;
inttemp=num;
while(temp)
{
if(!isprime(temp))
return;
temp/=10;
}
cnt++;
printf("%d\n",num);
return;
}
else
{
for(inti=1;i<=9;i++)
147
()
34
35
36
37
38
39
40
41
42
43
f(num*10+i,len+1);
}
}
intmain()
{
scanf("%d",&n);
f(0,0);
printf("%d",cnt);
}
4~11 isprime(x) x 1 , 0
.
33~34 1~9 . 0
. 0
.
.
f(num, len) = num len
f(0, 0) , .
148
Part
n , 4~11
. n/10 .
,
. cnt
1, .
, n
,
. n 6
x
.
. 0
.
1. 2, 3, 5, 7 . 2, 3, 5, 7
. ( )
2. (2 ),
5 (5 ). 1, 3, 7, 9 . 10
. ()
3. 1, 3, 7, 9
.
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
intn,cnt;
intisprime(intx)
{
for(inti=2;i*i<=x;i++)
if(x%i==0)
return0;
return1;
}
26:
1, 3,
7, 9
39:
2, 3, 5, 7
149
()
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
voidf(intnum,intlen)
{
if(len==n)
{
if(isprime(num))
{
cnt++;
printf("%d\n",num);
}
return;
}
else
{
if(isprime(num))
{
f(num*10+1,len+1);
f(num*10+3,len+1);
f(num*10+7,len+1);
f(num*10+9,len+1);
}
}
}
intmain()
{
scanf("%d",&n);
f(2,1);f(3,1);f(5,1);f(7,1);
printf("%d",cnt);
}
n 1, 2, 3 .
< N >
n = 1 ,
n = 2 ,
150
Part
n = 3 ,
151
()
10
minimum sum(S)
n*n . (1<=n<=10)
.
(, n , 100 .)
n .
n . n+1 n .
3
153
247
535
152
Part
1 .
.
1 1 1 . 1 1
2 {2, 4, 7} 1 {2} .
.
153
()
1-4-5 10. 1
1, 2 2 3 1 2 . 10
.
.
.
7 .
154
Part
2 11
11 .
.
3, 2, 3 8
. .
155
()
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
156
#include<stdio.h>
intm[11][11];
intcol_check[11];
intn,min_sol=0x7fffffff;
voidinput(void)
{
scanf("%d",&n);
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
scanf("%d",&m[i][j]);
}
voidsolve(introw,intscore)
{
if(row==n)
{
if(score<min_sol)
min_sol=score;
return;
}
for(inti=0;i<n;i++)
{
if(col_check[i]==0)
{
col_check[i]=1;
solve(row+1,score+m[row][i]);
col_check[i]=0;
}
}
return;
}
intmain()
{
input();
solve(0,0);
printf("%d",min_sol);
return0;
}
Part
11
(S)
(App) .
''
.
.
.
.
.
.
.
.
.
n ,A1, .. , An . Ai mi
. , Ai
, ( ) ci .
B , M
. , A1,..,An
M .
ci M
.
157
()
(S) ()
n M ,
n .
n A1,, An
m1, , mn ,
n c1,, cn .
[ ]
1 n 100
1 M 10,000,000
1 m1, , mn 10,000,000
0 c1,, cn 100
M
.
560
3010203540
30354
: (2013 )
158
Part
M .
( 17) .
Wi
mi
Vi
ci
W
, M
.
.
(i)
(mi)
(ci)
30
10
20
35
40
60 60
1, 2, 3 .(30+10+20=60, 3+0+3=6)
. f(1, 0) ,
f(n, M) .
159
()
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
#include<stdio.h>
#defineMAXV999999
8: i,
r
intM,n,i,m[101],c[101];
intmin(inta,intb){returna<b?a:b;}
intf(inti,intr)
{
if(i==0)
{
if(r<=0)return0;
elsereturnMAXV;
}
elseif(r<0)
returnf(i1,r);
else
returnmin(f(i1,r),f(i1,rm[i])+c[i]);
}
intmain()
{
scanf("%d%d",&n,&M);
for(i=1;i<=n;i++)scanf("%d",&m[i]);
for(i=1;i<=n;i++)scanf("%d",&c[i]);
printf("%d",f(n,M));
return0;
}
f .
f(i, r) = 1~i , r
.
.
.
160
Part
12
n*m(5 n, m 100) [ 1]
. , n , m .
.
( )
.
[ 1] ( ) C
.
[ 1]
[ 2]
. C
. , [ 3]
C .
[ 2]
[ 3]
161
()
()
.
.
n, m(5 n, m 100)
. n 1
, 0 . , 0 1
.
89
000000000
000110000
000110110
001111110
001111100
001101100
000000000
000000000
: (2000 )
162
Part
flood fill
. .
,
.
.
, ,
.
C
.
.
. 1
.
, .
163
()
flood fill .
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
164
#include<stdio.h>
inta1[101][101],a2[101][101];
intn,m;
voidcopy()
{
inti,j;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
a1[i][j]=a2[i][j];
}
voidfill1(intx,inty)
{
if(x<1||y<1||x>n||y>m)return;
if(a1[x][y]==0)
{
a1[x][y]=2;
fill1(x+1,y);
fill1(x1,y);
fill1(x,y+1);
fill1(x,y1);
}
}
intcheck(intx,inty)
{
intt=0;
if(a1[x+1][y]==2)t++;
if(a1[x1][y]==2)t++;
if(a1[x][y+1]==2)t++;
if(a1[x][y1]==2)t++;
returnt;
}
intmain()
{
inti,j,hour=0,count;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
scanf("%d",&a1[i][j]);
a2[i][j]=a1[i][j];
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Part
}
while(1)
{
fill1(1,1);
count=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
if(a1[i][j]==1&&check(i,j)>=2)
{
a2[i][j]=0;
count++;
}
}
if(count==0)
{
printf("%d",hour);
break;
}
hour++;
copy();
}
return0;
}
.
, .
.
165
()
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
voidfill1(intx,inty)
{
if(x<1||y<1||x>n||y>m)return;
if(a1[x][y]==0)
{
a1[x][y]=2;
fill1(x+1,y);
fill1(x1,y);
fill1(x,y+1);
fill1(x,y1);
}
}
voidfill2(intx,inty)
{
if(x<1||y<1||x>n||y>m)return;
if(a2[x][y]==0)
{
a2[x][y]=2;
fill2(x+1,y);
fill2(x1,y);
fill2(x,y+1);
fill2(x,y1);
}
}
main() .
1
2
3
4
5
6
7
8
9
10
11
166
intmain()
{
inti,j,hour=0,count;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
scanf("%d",&a1[i][j]);
a2[i][j]=a1[i][j];
}
fill1(1,1);
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
Part
fill2(1,1);
while(1)
{
count=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
if(a1[i][j]==1&&check(i,j)>=2)
{
a2[i][j]=0;
count++;
}
}
if(count==0)
{
printf("%d",hour);break;
}
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(a1[i][j]==1&&a2[i][j]==0)
fill2(i,j);
hour++;
copy();
}
return0;
}
.
, .
167
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
168
()
voidfill3(intx,inty)
{
if(x<1||y<1||x>n||y>m)return;
if(a1[x][y]==3||a1[x][y]==0)
{
a1[x][y]=2;
fill3(x+1,y);
fill3(x1,y);
fill3(x,y+1);
fill3(x,y1);
}
}
intmain()
{
inti,j,hour=0,count;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
scanf("%d",&a1[i][j]);
a2[i][j]=a1[i][j];
}
fill3(1,1);
while(1)
{
count=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
if(a1[i][j]==1&&check(i,j)>=2)
{
a2[i][j]=0;
count++;
}
}
if(count==0)
{
printf("%d",hour);break;
}
41
42
43
44
45
46
47
48
49
Part
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(a1[i][j]==1&&a2[i][j]==0)
fill2(i,j);
hour++;
copy();
}
return0;
}
.
,
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<cstdio>
intdx[4]={1,0,1,0},dy[4]={0,1,0,1},h,w,S[110][110],
res;
boolinside(inta,intb)
{
return((0<=a&&a<h)&&(0<=b&&b<w));
}
booldone(void)
{
intcnt=0;
for(inti=0;i<h;i++)for(intj=0;j<w;j++)
if(S[i][j]==1||S[i][j]>2)S[i][j]=0;
elseif(S[i][j]==2||S[i][j]==1)
S[i][j]=1,cnt++;
returncnt==0;
}
intsolve(inta,intb)
{
S[a][b]=1;
for(inti=0;i<4;i++)
if(inside(a+dx[i],b+dy[i]))
{
if(S[a+dx[i]][b+dy[i]]==0)
169
24
25
26
27
28
29
30
31
32
33
34
35
36
37
170
()
solve(a+dx[i],b+dy[i]);
elseif(S[a+dx[i]][b+dy[i]]>0)
S[a+dx[i]][b+dy[i]]++;
}
}
intmain()
{
scanf("%d%d",&h,&w);
for(inti=0;i<h;i++)for(intj=0;j<w;j++)
scanf("%d",&S[i][j]);
for(res=0;!done();res++)solve(0,0);
printf("%d",res);
return0;
}
Part
13
(bicoloring)
,
, 4 .
100 1976
.
.
,
.
. 0 n-1 n m , 2
.
n(1 n 200) m .
m
.
.
3
3
01
12
20
9
8
01
02
03
04
05
06
07
08
IMPOSSIBLE
OK
171
()
.
.
, .
.
.
. 1 ,
2 .
1. 0 1 0 .
2. 1 2 .
3. OK, IMPOSSIBLE.
1 .
3 , 3
.
172
Part
1 .
0
!!
.
.
2 .
2 .
0
!!
2 .
1
!!!!
IMPOSSIBLE .
173
()
2 . 2
.
.
9 8
. (
2 .
.)
1
.
1 0
!!!!
...
2
.
174
Part
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
#include<stdio.h>
intn,m,G[200][200],visited[200];
voidsolve(intv,intc)
{
visited[v]=c;
intcan=1;
for(inti=0;i<n;i++)
if(G[v][i]&&visited[i]==c)can=0;
if(!can)
{
visited[v]=0;
return;
}
for(inti=0;i<n;i++)
{
if(!visited[i]&&G[v][i])
{
solve(i,1);
solve(i,2);
}
}
}
intmain()
{
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++)
{
ints,e;
scanf("%d%d",&s,&e);
G[s][e]=G[e][s]=1;
}
solve(0,1);
for(inti=0;i<n;i++)
if(visited[i]==0)
{
175
()
39
40
41
42
43
44
puts("IMPOSSIBLE");
return0;
}
printf("OK");
return0;
}
.
.
.
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
176
#include<stdio.h>
#include<vector>
intn,m,visited[200];
std::vector<int>G[200];
voidsolve(intv,intc)
{
visited[v]=c;
intcan=1;
for(inti=0;i<G[v].size();i++)
if(visited[G[v][i]]==c)can=0;
if(!can)
{
visited[v]=0;
return;
}
for(inti=0;i<G[v].size();i++)
{
if(!visited[G[v][i]])
{
solve(G[v][i],1);
solve(G[v][i],2);
}
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Part
}
}
intmain()
{
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++)
{
ints,e;
scanf("%d%d",&s,&e);
G[s].push_back(e);
G[e].push_back(s);
}
solve(0,1);
for(inti=0;i<n;i++)
if(visited[i]==0)
{
puts("IMPOSSIBLE");
return0;
}
printf("OK");
return0;
}
177
()
14
maximum sum(S)
n .
.
n i j
(, 1 < i <= j <= n ). 6
.
6 -7 3 -1 5 2
3 6
9.
n , n
.
(, 2 <= n <= 100 , -1000 1000 .)
6
673152
178
Part
100 .
.
.
1. s ( ).
2. e ( ).
3. [s, e] . .
4. , .
. ,
.
1
2
3
4
5
6
7
8
9
10
int count=1;
for(int s=0; s<n; s++)
{
for(int e=s; e<n; e++)
{
printf("[%d~%d] ", s, e);
if(count%7==0) puts("");
count++;
}
}
.
. .
[0~0] [0~1] [0~2] [0~3] [0~4] [0~5] [1~1]
[1~2] [1~3] [1~4] [1~5] [2~2] [2~3] [2~4]
[2~5] [3~3] [3~4] [3~5] [4~4] [4~5] [5~5]
. .
179
()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
intn,A[110],ans;
intmain()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d",A+i);
for(ints=0;s<n;s++)
{
for(inte=s,sum;e<n;e++)
{
sum=0;
for(intk=s;k<=e;k++)
sum+=A[k];
ans=ans<sum?sum:ans;
}
}
printf("%d\n",ans);
return0;
}
,
.
180
Part
15
n .
1 2 .
n ,
.
3 1, 1, 1 1, 2 , 2, 1
3 .
n ( n 20 ).
181
()
. 1 2
, . ,
n .
2 .
5 .
5 , 8 .
.
182
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Part
#include<stdio.h>
intn,ans;
voidsolve(intv)
{
if(v>n)return;
if(v==n)
{
ans++;
return;
}
solve(v+1);
solve(v+2);
}
intmain()
{
scanf("%d",&n);
solve(0);
printf("%d\n",ans);
}
7 .
.
183
()
16
(S)
. .
.
m .
( 10 <= m <= 10,000 )
n .
( 1 <= n <= 10 )
.
( 10 <= <= m )
730
5
10501005001250
184
Part
.
.
.
2 .
, 1
.
.
4 10, 50, 100, 500 .
0 ,
, .
.
120, 30, 50, 60, 100
. 1 .
185
()
30 . 120
.
30
50 60 . 100
.
186
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
Part
#include<stdio.h>
intm,n,coin[10],ans=987654321;
voidsolve(intmon,intd)
{
if(mon>m)return;
if(mon==m)
{
if(d<ans)ans=d;
return;
}
for(inti=0;i<n;i++)
solve(mon+coin[i],d+1);
}
intmain()
{
scanf("%d%d",&m,&n);
for(inti=0;i<n;i++)
scanf("%d",coin+i);
solve(0,0);
printf("%d\n",ans);
return0;
}
solve() .
solve(mon, d) = d mon
10,000
10 1,000 .
,
.
, ,
.
187
()
. 0
.
. 10, 50, 100, 500.
188
Part
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
#include<stdio.h>
intn,m,coin[10],ans=987654321;
voidsolve(intmon,intk,intcnt)
{
if(k==n||mon>m)return;
if(mon==m)
{
if(ans>cnt)ans=cnt;
return;
}
for(inti=0;mon+coin[k]*i<=m;i++)
solve(mon+coin[k]*i,k+1,cnt+i);
}
intmain()
{
scanf("%d%d",&m,&n);
for(inti=0;i<n;i++)
scanf("%d",coin+i);
solve(0,0,0);
printf("%d\n",ans);
return0;
}
solve() .
solve(mon, k, cnt )= k cnt mon
.
.
, .
189
()
17
.
.
.
(B) 40(:), (n) 6 .
6 7, 13, 17, 19, 29, 31. 40
. 40 ,
7 + 13 + 17 = 37
7 + 31 = 38
7 + 13 + 19 = 39
...
40 39.
.
190
40
6
71317192931
39
Part
.
B .
.
13
17
19
29
31
f i , .
, .
f , .
f(i, sum) = i , i-1 sum
1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
intB,n,act[23],res;
voidf(inti,intsum)
{
if(i==n+1)
{
if(sum<=B&&sum>res)
res=sum;
return;
5: i , i-1
13: i
14:
191
()
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
}
f(i+1,sum+act[i]);
f(i+1,sum);
}
intmain()
{
inti;
scanf("%d%d",&B,&n);
for(i=1;i<=n;i++)
scanf("%d",&act[i]);
f(1,0);
printf("%d",res);
return0;
}
f(1, 0) .
9 B sum .
f(5, 39) .
192
Part
18
0/1 (S)
W .
( wi, vi) ,
.
, .
W .
45
23
12
33
22
193
()
.
. .
(i)
(wi)
(vi)
5, 5
. 1, 2, 4 W=5 ,
3 + 2 + 2 = 7 . .
f .
f(i, r) = i~n , r
or
max
194
Part
n 1 1 , n
. i
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
intW,n,i,j,w[102],v[102];
intmax(inta,intb){returna>b?a:b;}
7: f( i,
r)
intf(inti,intr)
{
if(i==n+1)
return0;
elseif(r<w[i])
returnf(i+1,r);
else
returnmax(f(i+1,r),f(i+1,rw[i])+v[i]);
}
intmain()
{
scanf("%d%d",&n,&W);
for(i=1;i<=n;i++)
scanf("%d%d",&w[i],&v[i]);
printf("%d",f(1,W));
return0;
}
195
()
22 f(1, W) 1 , W
. f i ,
i+1 .
9~10 .
14 i ,
, i+1
.
196
Part
19
(S)
1kg .
1g, 3g, 9g, 27g, 81g, 243g, 729g 7 .
, , 25g .
1. n (1n1,000).
2. n .
1. 0 .
2.
.
3. .
25
2530127
40
40013927
197
()
. 8
. .
8 3
.
.
1
2
3
4
5
6
7
8
9
10
11
for(inti=0;i<7;i++)
{
if(chk[i]==0)
{
chk[i]=1;
solve(n,sum+scale[i]);
chk[i]=2;
solve(n+scale[i],sum);
chk[i]=0;
}
}
chk
.
, 1 , 0
, .
.
198
Part
chk[k] = 0(k )
chk[k] = 1(k )
chk[k] = 2(k )
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
#include<stdio.h>
#include<stdlib.h>
intn,scale[8]={1,3,9,27,81,243,729},chk[8],end;
voidsolve(intn,intsum)
{
if(end)return;
if(sum==n)
{
for(intc=2;c>0;c)
{
for(inti=0;i<7;i++)
if(chk[i]==c)
printf("%d",scale[i]);
if(c==2)
printf("0");
}
end=1;
}
for(inti=0;i<7;i++)
{
if(chk[i]==0)
{
chk[i]=1,solve(n,sum+scale[i]);
chk[i]=2,solve(n+scale[i],sum);
chk[i]=0;
}
}
}
intmain()
{
scanf("%d",&n);
printf("%d",n);
solve(n,0);
return0;
}
199
()
20
(S)
H*W (0,0) (H, W)
.
.
(1) .
(2) .
(3) (0,0) (H, W) .
( .)
.
, 34 5 .
(0,0) (H, W)
.
200
Part
(S) ()
1. H W .
2. H , W .
[ ]
1 <= H, W <= 10
(0,0) (H, W) .
34
201
()
.
2.
, [1]
. ( )
.
(, )
[1] .
.
1
2
3
4
5
6
voidsolve(inth,intw)
{
solve(h+1,w);
if((double)H/W<=(double)h/(w+1))
solve(h,w+1);
}
202
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Part
#include<stdio.h>
intH,W,ans;
voidsolve(inth,intw)
{
if(h>H||w>W)return;
if(h==H&&w==W)
{
ans++;
return;
}
solve(h+1,w);
if((double)H/W<=(double)h/(w+1))
solve(h,w+1);
}
intmain()
{
scanf("%d%d",&H,&W);
solve(0,0);
printf("%d\n",ans);
return0;
}
203
()
21
(S)
. , . 3
.
3
.
.
(1) .
(2) .
(3) 3
.
(4) --
.
(5) 3 , -- .
3
. .
1: d .
d = ( ) - ( )
2: d
.
204
Part
(S) ()
, 6 ,
6, 4, 4, 4, 6, 9
12, 12, 9 1
12-9=3 .
( 13, 10, 10 13-10=3 3 ,
2 .)
3
.
1. n (3n15).
2. n .
3. 0 100
1. 3 .
2. , , 3 .
6
644469
3
2101
9
111461111
12129
1021
665
205
()
15 ,
.
15 3
.
n , 1 , 2 , 3
, , .
.
.
1
2
3
4
5
6
7
8
9
206
voidsolve(intno,inta,intb,intc)
{
if(no<n)
{
solve(no+1,a,b,c+gift[no]);
solve(no+1,a,b+gift[no],c);
solve(no+1,a+gift[no],b,c);
}
}
Part
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
#include<stdio.h>
#include<algorithm>
intgift[30],chk[30],A,B,C,n,S;
intcomp(inta,intb)
{
returna>b;
}
voidsolve(intno,inta,intb,intc)
{
if(no<n)
{
solve(no+1,a,b,c+gift[no]);
solve(no+1,a,b+gift[no],c);
solve(no+1,a+gift[no],b,c);
}
elseif(a>=b&&b>=c)
{
A=a,B=b,C=c;
}
}
intmain()
{
inti;
scanf("%d",&n);
for(i=0;i<n;S+=gift[i++])
scanf("%d",&gift[i]);
std::sort(gift,gift+n,comp);
solve(0,0,0,0);
printf("%d%d%d\n",A,B,C);
return0;
}
n 20 .
207
()
22
(S)
A B
.
.
, < 1> A 1 B 8
, A 3 B 9 , A 4
B 1
.
.
,
.
208
Part
(S) ()
. 100
.
A B
.
500 ,
.
8
18
39
22
41
64
1010
97
76
: (2007 )
209
()
A, B .
, . 100,000
500,000 . , .
.
A 1, 3, 4
, 3 .
210
Part
, ?
,
A ,
.
211
()
3 2 , 2 3 .
.
,
, .
212
Part
A , B
.
, A , B
?
.
213
()
A , B
,
.
A , B
, ,
.
214
Part
,
(longest increasing subsequence, LIS) .
LIS ,
.
LIS
.
1. k LIS solve(k) .
2. solve(k) solve(k-1), solve(k-2), solve(k-3), ,
solve(3), solve(2), solve(1) 1 .
215
()
3. , k >
,
4. 1 ,
.
k LIS , (LIS
) .
216
Part
k LIS .
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
#include<stdio.h>
#include<algorithm>
structw{inta;intb;}d[100001];
intn;
boolcmp(wx,wy){returnx.a<y.a;}
intmax(intp,intq){returnp>q?p:q;}
intsolve(intk)
{
inti,count=1;
for(i=k1;i>=1;i)
if(d[k].b>d[i].b)count=max(count,solve(i)+1);
returncount;
}
intmain()
{
inti,t,m=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d%d",&d[i].a,&d[i].b);
std::sort(d+1,d+n+1,cmp);
for(i=1;i<=n;i++)
{
t=solve(i);
if(m<t)m=t;
}
printf("%d\n",nm);
return0;
}
LIS .
1. LIS .
2. 1~n LIS .
, , , , , , ,
217
()
LIS .
218
Part
LIS .
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
#include<stdio.h>
#include<algorithm>
structw{inta;intb;}d[100001];
intn;
boolcmp(wx,wy){returnx.a<y.a;}
intmax(intp,intq){returnp>q?p:q;}
intsolve(intk)
{
inti,count=1;
for(i=k+1;i<=n;i++)
if(d[k].b<d[i].b)count=max(count,solve(i)+1);
returncount;
}
intmain()
{
inti,t,m=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d%d",&d[i].a,&d[i].b);
std::sort(d+1,d+n+1,cmp);
for(i=1;i<=n;i++)
{
t=solve(i);
if(m<t)m=t;
}
printf("%d\n",nm);
return0;
}
219
()
23
(S)
n n
.
1 n
1 n
.
1.
( , ) . n 6
.
1 2 .
1 (1, 1) 2 (n, n) .
,
( .).
.
.
,
.
220
Part
(S) ()
n=6 , 3
(3, 5), (5, 5), (2, 3) .
(3, 5) 2 (5, 5) 2 , (2, 3)
1 4 + 2 + 3 = 9 ,
.
,
.
n(3n1,000)
.
w(1w15) .
(w+2) .
.
.
.
6
3
35
55
23
: (2003 )
221
()
.
.
,
.
.
,
,
.
. 2
2 ,
. .
solve( a, b, d ) =
max(a, b) d , 1 a , 2
b
.
max
222
Part
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
#include<stdio.h>
intE[1010][2],n,m,ans=987654321;
intmin(inta,intb)
{
returna>b?b:a;
}
intabs(inta)
{
returna>0?a:a;
}
intdis(inta,intb)
{
returnabs(E[a][0]E[b][0])+abs(E[a][1]E[b][1]);
}
intmain()
{
scanf("%d%d",&n,&m);
E[0][0]=E[0][1]=1;
E[1][0]=E[1][1]=n;
for(inti=2;i<m+2;i++)
scanf("%d%d",&E[i][0],&E[i][1]);
solve(0,1,0);
printf("%d",ans);
return0;
}
223
()
E . 20, 21 0, 1
.
m+2 . min , abs
.
1
2
3
4
5
6
7
8
9
10
11
voidsolve(inta,intb,intd)
{
intnext=(a>b?a:b)+1;
if(next>=m+2)
{
if(d<ans)ans=d;
return;
}
solve(next,b,d+dis(a,next));
solve(a,next,d+dis(b,next));
}
next a, b
max(a, b) + 1 , 2 .
,
.
.
.
1
2
3
4
5
6
7
8
9
10
11
12
224
#include<stdio.h>
intdis[1002][1002];
intmain(void)
{
:
for(i=0;i<m+2;i++)
for(j=0;j<m+2;j++)
dist[i][j]=abs(a[i][0]a[j][0])+abs(a[i][1]a[j][1]);
:
}
Part
. .
solve .
1
2
3
4
5
6
7
8
9
10
11
voidsolve(inta,intb,intd)
{
intnext=(a>b?a:b)+1;
if(next>=m+2)
{
if(d<ans)ans=d;
return;
}
solve(next,b,d+dis[a][next]);
solve(a,next,d+dis[b][next]);
}
.
.
225
()
24
1, 2, 3 .
, .
.
.
33
32121323
123123213
.
2
32
32123
1232123
n n
. , 1213121 2123212
1213121.
n . n 1 80 .
1, 2, 3 n
. 1, 2, 3
.
1213121
: (1997 )
226
Part
.
1 1, 2, 3 .
.
1. .
2. .
3. n .
4. 1,2,3 .
. 4
.
1 , 1
11
12 , 1
121 , 3
1211
1212
1213 , 4 !
. 80
, X
,
.
227
()
n .
.
, 1
, 2 , 3 , ... ,
228
Part
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
#include<stdio.h>
ints[81];
intn,end=0;
boolsame(inta,intb)
{
inti;
for(i=a;i<b;i++)
if(s[i]!=s[i+ba])
break;
return(a==b?false:i==b);
}
intgood(intm)
{
for(inti=1;i<=m/2;i++)
for(intj=1;j<=mi;j++)
if(same(j,j+i))
return0;
return1;
}
intmain()
{
scanf("%d",&n);
solve(1);
return0;
}
229
()
solve
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
voidsolve(intk)
{
if(end)return;
if(k>n)
{
end=1;
for(inti=1;i<=n;i++)
printf("%d",s[i]);
return;
}
for(inti=1;i<=3;i++)
{
s[k]=i;
if(good(k))solve(k+1);
s[k]=0;
}
}
.
, .
.
1. .
2. .
3. .
230
Part
same good
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
ints[81];
intn,end=0;
boolsame(inta,intb)
{
inti;
for(i=a;i<b;i++)
if(s[i]!=s[i+ba])
break;
return(a==b?false:i==b);
}
intgood(intm)
{
inti,j;
for(i=m1,j=m;i>0;i=2,j=1)
if(same(i,j))
return0;
return1;
}
231
()
25
(S)
S T
. , .
, .
.
k
. .
S, T 7 .
k = 2, S-a-b-T S-p-q-T
.
, S-p-q-T q-T
.
k
.
.
232
Part
(S) ()
1) . km
(). 1 10km
.
2) . , g = (2, 1) h = (37,
43) d(g, h) sqrt{ (2-37)^2 + (1-43)^2 }= 54.671 50 < d(g, h)
60 6 .
3) S (0, 0) T (10000,10000)
.
4) n 3 <= n <= 10 (x, y)
0 < x, y < 10,000 . 0 <= k <= 1000.
n k . n
() "x y" .
S T k
.
101
101000
201000
301000
401000
50005000
100060
100070
100080
100090
70007000
708
: (2005 )
233
()
(0,0) (10000,10000) ,
, , ,
.
1, (5000,5000) 1
, 708.
(5000,5000)
708 .
,
.
k , 1 , 2.. k-1, k
.
. ,
234
Part
. , .
, ,
( ) ,
k
.
p, c,
d , .
, .
, k ? ,
.
.
0 , .
v[ ] , (x, y)
235
()
a[ ] .
.
, () ,
<math.h> sqrt(square root) .
10 (1 10 ..)
( ) ceil( ) .
1.4.. 1.1 1.9 1 2
(ceil) ceil( )
. floor( ) .
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
236
#include<stdio.h>
#include<math.h>
intn,k,v[11],dmin=200000000;
structair{intx;inty;}a[11];
intmax(inta,intb){returna>b?a:b;}
intdist(intp1,intp2)
{
return
(a[p1].xa[p2].x)*(a[p1].xa[p2].x)+(a[p1].ya[p2].y)*(a[p1].ya[p2].y);
}
voidf(intp,intc,intd)
{
if(c==k)
{
d=max(d,dist(p,n+1));
if(d<dmin)dmin=d;
return;
}
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
for(inti=0;i<=n+1;i++)
if(!v[i])
{
v[i]=1;
f(i,c+1,max(d,dist(p,i)));
v[i]=0;
}
}
Part
intmain()
{
scanf("%d%d",&n,&k);
a[0].x=0,a[0].y=0;
for(inti=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
a[n+1].x=10000,a[n+1].y=10000;
v[0]=1;
f(0,0,0);
printf("%.f\n",ceil(sqrt(dmin)/10));
return0;
}
237
()
26
(S)
.
. < >
< >. 1 6 .
< > <
> . ,
. {R, I, N, G, S} .
1
< > <
> .
, .
.
(1) () () ,
.
(2) < > < > .
, .
(3) , .
1 RGS
3(
.) .
238
Part
(S) ()
.
RGS (1)
, (2),
(3) .
,
.
, 1 3 3 .
(R, I, N, G, S ) .
2, 10. < >
< > . 5 ,
20 .
. 0 .
2^31 - 1 .
RGS
RINGSR
GRGGNS
RINGS
SGNIRSGNIR
GNIRSGNIRS
GG
GGGGRRRR
IIIIGGGG
16
: (2004 )
239
()
,
.
- .
- .
- .
- .
- .
RGS 3.
.
,
240
Part
R , G
. G
.
,
,
, .
,
.
, ,
, , .
,
. .
241
()
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
242
#include<stdio.h>
charr[11];
charb1[21];
charb2[21];
intf(ints,intn,intrp)
{
inti,c=0;
if(r[rp]=='\0')return1;
if(s==1)
{
for(i=n;b1[i]!='\0';i++)
if(b1[i]==r[rp])
c+=f(2,i+1,rp+1);
}
if(s==2)
{
for(i=n;b2[i]!='\0';i++)
if(b2[i]==r[rp])
c+=f(1,i+1,rp+1);
}
returnc;
}
intmain()
{
scanf("%s%s%s",r,b1,b2);
printf("%d\n",f(1,0,0)+f(2,0,0));
return0;
}
Part
27
(S)
.
. .
.
. ,
km .
, 15km
.
.
.
, .
.
.
, 7km 3,
4km 10km
. .
.
.
.
12km
.
243
()
(S) ()
.
,
1000 .
n . n .
( 3 <= n <= 20 )
n .
.
, .
. p .
p 0, p . p
.
d ,
. g
.
1 , 0 .
1.
1,000
.
8
071
341
681
861
1220
1321
1421
1571
: (2008 )
244
Part
,
.
, ,
.
,
.
,
.
8 , 6
2km 14km .
, 8 3, 6, 12, 13, 14 .
0 7 3, 6 , 6
8 8, 12, 13, 14
.
,
.
, () .
245
()
.
A, B
, A, B
.
A B ,
A , B A 1
. A B
.
A, B .
<= , A, B
.
A, B , A
, A B 1
246
Part
.
i A, B 1
, 3 .
, . 3
- A i
- B i
- A, B i
A a, B b, k , f(a,b,k) A
a, B b k .
.
A <= B
, ,
( ) .
<= .
, ,
.
247
()
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
248
#include<stdio.h>
intn,m=1000;
intp[500],d[500],g[500];
boolca(inta,intk)
{
returnp[k]<=p[a]+d[a];
}
boolcb(intb,intk)
{
return(p[k]<=p[b]+d[k])&&g[k];
}
intf(inta,intb,intk)
{
intc=0;
if(k==n1)
{
if(ca(a,k)&&cb(b,k))c=1;
elsec=0;
}
else
{
if(ca(a,k))c+=f(k,b,k+1)%m;
if(cb(b,k))c+=f(a,k,k+1)%m;
c+=f(a,b,k+1)%m;
}
returnc;
}
intmain()
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d%d%d",&p[i],&d[i],&g[i]);
printf("%d\n",f(0,0,1));
return0;
}
Part
28
.
. .
( 27 73) ( 12, 50, 81)
.
.
.
.
. ,
( 15) ( 8)
15+8 = 23.
.
.
p f
. 1 p 11 1 f 10 pf.
p .
f .
. 1,000,000 .
. 2^31 - 1 .
32
125081
2773
23
: (2005 )
249
()
,
.
, 6.
p f , f
, 3 2 3 ,
1 2 6 .
1, 2 , 1-1, 2-2 .
250
Part
251
()
.
p, f, h ,
.
f(0,0,0) , f(1,1,15) 1 1
15 .
252
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
intp,f,pp[100001],fp[100000],pv[100001],mh=0x7fffffff;
intmin(inta,intb)
{
returna>b?b:a;
}
intabs(inta)
{
returna>0?a:a;
}
voidg(intpt,intft,inth)
{
if(ft==f)
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
Part
{
mh=min(mh,h);
return;
}
for(inti=1;i<=p;i++)
{
if(pv[i]==0)
{
pv[i]=1;
h+=abs(pp[i]fp[ft+1]);
g(i,ft+1,h);
h=abs(pp[i]fp[ft+1]);
pv[i]=0;
}
}
}
intmain()
{
inti;
scanf("%d%d",&p,&f);
for(i=1;i<=p;i++)
scanf("%d",&pp[i]);
for(i=1;i<=f;i++)
scanf("%d",&fp[i]);
g(0,0,0);
printf("%d\n",mh);
return0;
}
253
()
.
.
.
.
.
.
, .
.
, ,
. .
.
.
.
,
.
.
.
254
Part
.
.
, ,
.
,
.
(greedy)
, (mathematical greedy)
.
,
.
.
. [
] .
[ ]
.
(, .)
.
. .
.
255
()
3 3.
8
2 2. [ ]
8 2 .
. 20
? .
256
Part
.
,
. ,
.
.
257
()
1
n .
1 n n .
n 10
10 1, 2, 5, 10 1 + 2 + 5 + 10 18
.
n .
(, 1 <= n <= 10,000,000,000(100))
n .
258
10
18
Part
.
100,000 , 100
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
intn;
intsolve()
{
intans=0;
for(inti=1;i<=n;i++)
if(n%i==0)
ans+=i;
returnans;
}
intmain()
{
scanf("%d",&n);
printf("%d\n",solve());
return0;
}
,
. .
100 .
.
. .
.
8 .
259
()
7
8
9
for(inti=2;i<n;i++)
if(n%i==0)
ans+=i;
.
.
.
,
7
8
9
for(inti=2;i<=n/2;i++)
if(n%i==0)
ans+=i;
.
2 . 7
7
8
9
for(inti=2,bound=n/2;i<=bound;i++)
if(n%i==0)
ans+=i;
.
.
. .
260
Part
. , .
. 10 4 .
10
1 10 10 2 5 10.
c , .
, . 10
, 1 2 5 10 . ,
. 16 .
16
. 4 4 16 .
2
.
100 2 10 .
261
()
10 .
. 1 100 100 .
.
for( i = 1 ; i <= sqrt(n) ; i++ )
sqrt math.h
include , sqrt
. .
(
) ( )
.
for( i = 1; i*i <= n; i++ )
.
.
262
Part
.
100 int . 64bit
long long int . .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
longlongintn;
longlongintsolve()
{
longlonginti,ans=0;
for(i=1;i*i<n;i++)
if(n%i==0)
ans+=(i+n/i);
if(i*i==n)
ans+=i;
returnans;
}
intmain()
{
scanf("%lld",&n);
printf("%lld\n",solve());
return0;
}
263
()
2
(S)
n .
n .
n 5
2, 3, 5, 7, 11, 13, 5 11
.
n .
( , 1 <= n <= 100,000 )
n .
264
11
77
389
Part
2 .
.
1
2
3
4
5
6
7
boolisPrime(intk)
{
intcnt=0;
for(inti=1;i<=k;i++)
if(k%i==0)cnt++;
returncnt==2;
}
.
. .
?
,
, 2 .
isPrime .
1
2
3
4
5
6
7
8
9
10
boolisPrime(intk)
{
intcnt=0;
for(inti=1;i<=k;i++)
{
if(k%i==0)cnt++;
if(cnt>2)break;
}
returncnt==2;
}
. .
.
265
()
1
2
3
4
5
6
7
boolisPrime(intk)
{
intcnt=0;
for(inti=1;i<=k&&cnt<=2;i++)
if(k%i==0)cnt++;
returncnt==2;
}
4 .
. ?
.
.
.
.
1
2
3
4
5
6
7
boolisPrime(intk)
{
intcnt=0;
for(inti=2;i<k;i++)
if(k%i==0)returnfalse;
returntrue;
}
. .
266
Part
, .
.
.
.
.
1
2
3
4
5
6
7
boolisPrime(intk)
{
intcnt=0;
for(inti=2;i*i<=k;i++)
if(k%i==0)returnfalse;
returntrue;
}
. ,
. .
.
.
. 2 , 2 .
1. .
2. 1 .
3. 1 .
4. , .
267
()
3
(L)
(prime number) 1 .
.
, 23 , 32 .
.
, 113 113, 131, 311 ( ),
113, 131, 311.
1. n
.
2. .
3. .
4. 0 .
268
25
131
113131311
1003
13311033001
1234
1423214323414231
Part
.
, , ,
. . 124
.
.
.
1. .
2. .
3. .
4. .
5. .
6. -1 .
269
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
270
()
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
inta[6];
intallnum[2000],alli;
intcompare(constvoid*a,constvoid*b)
{
return*(int*)a*(int*)b;
}
intchk(intn)
{
inti;
if(n==1)
return1;
for(i=2;i<n;i++)
if(n%i==0)return1;
return0;
}
voidswap(int*a,int*b)
{
inttemp;
temp=*a;*a=*b;*b=temp;
}
voidP(inti,intlast)
{
intnum=0,j,k;
if(i==last)
{
for(k=0;k<=last;k++)
num+=a[k]*(int)pow((double)10,(double)k);
allnum[alli++]=num;
}
else
for(j=i;j<=last;j++)
{
swap(&a[i],&a[j]);
P(i+1,last);
swap(&a[i],&a[j]);
}
}
18: 0:
21:
26:
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
intmain()
{
intn,i,last,prev=1;
scanf("%d",&n);
for(i=0;i<6;i++)
if(n!=0)
{
a[i]=n%10;
n/=10;
last=i;
}
Part
5:
8:
1
10:
(
)
13:
17:
25:
-1
P(0,last);
qsort(allnum,alli,sizeof(int),compare);
for(i=0;i<alli;i++)
{
if(chk(allnum[i])==0)
if(prev!=allnum[i])
{
printf("%d",allnum[i]);
prev=allnum[i];
}
}
if(prev==1)
printf("0");
return0;
}
271
()
4
(L)
1kg .
1g, 3g, 9g, 27g, 81g, 243g, 729g 7 .
, , 25g .
1. n (1n1,000).
2. n .
1. 0 .
2.
.
3. .
272
25
2530127
40
40013927
Part
.
.
. .
?
7
. .
.
1g, 3g, 9g, 27g, 81g, 243g, 729g
. 1 3
. 3 (
3 . , , , ...).
3 . 5 .
729 243
81
27
<5 3 >
< 3 >
1g 2 .
. 1g 2 . 1g
? 1g .
.
273
()
1g 3 3g . 1g
. 3g 2 .
3 .
+
5 +1
729 243
81
27
3
2
1
0
<5+1 3 >
. 3g .
3g 3 9g . 3g
.
+
5+1+3
729 243
81
27
=
<5+1+3 3 >
274
9
1
3
0
1
0
Part
. 3
. 3 .
3 2
0 .
2 0
. 2 ,
1 .
.
3 2 .
3 3
. .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
intw[7]={1,3,9,27,81,243,729};
intoutput[9];
intthree[7];
inti,j,n,idx;
voidto3(intnum)
{
for(inti=0;num!=0;i++,num=num/3)
three[i]=num%3;
}
intchk2(void)
{
for(inti=0;i<7;i++)
if(three[i]==2)
{
n=n+w[i];
output[idx++]=w[i];
return2;
3:
4:
5: 3
(0
)
6: n: ,
idx: output
11: 3
14: 3
2
17: 2
19: n
20:
21: 2
2
275
()
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
276
}
return0;
}
intmain(void)
{
scanf("%d",&n);
output[idx++]=n;
do
to3(n);
while(chk2()==2);
idx++;
for(i=0;i<7;i++)
if(three[i]==1)
output[idx++]=w[i];
for(i=0;i<idx;i++)
printf("%d",output[i]);
return0;
}
23: 2
0
28:
29:
32: 3
33: 2
35:
0
0
37: 1
(
)
38: output
Part
5
(L)
.
. , . ,
9m ,
1m, 4m ,
2m, 3m, 4m ,
3m 3 .
n .
: (2002 )
277
()
.
100 50,000 .
. .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
intn;
intsolve()
{
intcnt=0;
scanf("%d",&n);
for(inta=1;a<=n;a++)
for(intb=a;b<=n;b++)
for(intc=b;c<=n;c++)
if(a+b+c==n&&a+b>c)
cnt++;
returncnt;
}
intmain()
{
printf("%d\n",solve());
}
.
.
. .
278
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Part
#include<stdio.h>
intmain(void)
{
intn,a,b,c,count=0;
scanf("%d",&n);
for(a=1;a<=n;a++)
for(b=1;b<=n;b++)
{
c=n(a+b);
if(a+b>c&&(a<=b&&b<=c))count++;
}
printf("%d\n",count);
return0;
}
10 .
.
.
,
.
.
.
279
()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
intmain(void)
{
intn,a,b,c,count=0;
scanf("%d",&n);
for(c=n/3;c<=n/2;c++)
for(a=1;a<=n/3;a++)
{
b=n(a+c);
if(a+b>c&&(a<=b&&b<=c))count++;
}
printf("%d\n",count);
return0;
}
.
. .
,
.
,
. ,
, .
, .
(branch & bound) .
,
.
.
280
Part
. 2 3 , 3
3
, 9 .
.
11 6
.
.
3 .
281
()
(bounding)
(cutting) .
. 3
.
branch & bound .
.
.
.
282
Part
1
(L)
GSHS A .
n, m
GSHS 1 S n
.
.
: 1357, : 69+59+21=149
n m .
m ( ,
.).
. -1 .
711
1247
1369
2457
25124
3437
3559
3686
4627
4794
5721
6740
149
283
()
.
.
.
.
,
. .
,
221.
=221
171 .
221
=171
171 !!
.
.
284
Part
. .
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
#include<stdio.h>
intn,m,G[11][11],sol=0x7fffffff,chk[11];
voidsolve(intV,intW)
{
if(W>sol)return;
if(V==n)
{
if(W<sol)sol=W;
return;
}
for(inti=1;i<=n;i++)
if(!chk[i]&&G[V][i])
{
chk[i]=1;
solve(i,W+G[V][i]);
chk[i]=0;
}
}
intmain(void)
{
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++)
{
ints,e,w;
scanf("%d%d%d",&s,&e,&w);
G[s][e]=G[e][s]=w;
}
solve(1,0);
printf("%d\n",sol==0x7fffffff?1:sol);
return0;
}
285
()
counter
, 3
.
.
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
286
#include<stdio.h>
intn,m,G[11][11],sol=0x7fffffff,chk[11];
intcounter;
voidsolve(intV,intW)
{
if(W>sol)return;
counter++;
if(V==n)
{
if(W<ol)sol=W;
return;
}
for(inti=1;i<=n;i++)
if(!chk[i]&&G[V][i])
{
chk[i]=1;
solve(i,W+G[V][i]);
chk[i]=0;
}
}
intmain(void)
{
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++)
{
ints,e,w;
scanf("%d%d%d",&s,&e,&w);
G[s][e]=G[e][s]=w;
}
solve(1,0);
printf("%d\n",sol==0x7fffffff?1:sol);
printf("[ %d]\n",counter);
return0;
}
Part
3.
1
5 7
1 2 2
1 3 10
1 4 7
2 5 4
2 3 6
4 5 3
3 5 4
5 8
1 2 2
1 3 1
1 4 3
2 5 2
2 3 1
4 5 3
3 5 2
1 5 14
3
7 11
1 2 47
1 3 69
2 4 57
2 5 124
3 4 37
3 5 59
3 6 86
4 6 27
4 7 94
5 7 21
6 7 40
3 .
36
42
73
10
19
25
72.3%
54.77%
65.76%
.
2 .
.
. ?
.
.
.
287
()
.
.
.
.
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
288
#include<stdio.h>
intn,m,G[1001][1001],sol,chk[1001],greedy_chk[1001];
voidgreedy_ans(intV)
{
intW=0,t;
greedy_chk[V]=1;
while(V!=n)
{
intmin=0x7fffffff;
for(inti=1;i<=n;i++)
if(!greedy_chk[i]&&G[V][i]&&G[V][i]<min)
{
greedy_chk[i]=1;
min=G[V][i];
t=i;
}
sol+=G[V][t];
V=t;
}
}
voidsolve(intV,intW)
{
if(W>sol)return;
if(V==n)
{
if(W<sol)sol=W;
return;
}
for(inti=1;i<=n;i++)
if(!chk[i]&&G[V][i])
{
25: bounding
(cutting)
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Part
chk[i]=1;
solve(i,W+G[V][i]);
chk[i]=0;
}
}
intmain(void)
{
scanf("%d%d",&n,&m);
for(inti=0;i<m;i++)
{
ints,e,w;
scanf("%d%d%d",&s,&e,&w);
G[s][e]=G[e][s]=w;
}
greedy_ans(1);
solve(1,0);
printf("%d\n",sol==0x7fffffff?1:sol);
return0;
}
greedy_ans
. ,
. .
.
36
42
73
10
19
25
16
289
()
72.3%
54.77%
65.76%
89.9%
81.96%
78.09%
4 .
,
.
,
.
290
Part
2
minimum sum(M)
n*n . (1<=n<=10)
.
(, n . 100 .)
n .
n . n+1 n .
3
125
243
543
291
()
.
.
.
.
.
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
292
#include<stdio.h>
intm[11][11];
intcol_check[11];
intn,min_sol=0x7fffffff;
voidinput(void)
{
scanf("%d",&n);
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
scanf("%d",&m[i][j]);
}
voidsolve(introw,intscore)
{
if(score>min_sol)return;
if(row==n)
{
if(score<min_sol)
min_sol=score;
return;
}
for(inti=0;i<n;i++)
{
16: bounding
(cutting)
25
26
27
28
29
30
31
32
33
Part
if(col_check[i]==0)
{
col_check[i]=1;
solve(row+1,score+m[row][i]);
col_check[i]=0;
}
}
return;
}
.
.
1. 1 .
2. .
3. 2 .
4. .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
intm[11][11];
intcol_check[11],greedy_chk[11];
intn,min_sol=0;
voidinput(void)
{
scanf("%d",&n);
for(inti=0;i<n;i++)
for(intj=0;j<n;j++)
scanf("%d",&m[i][j]);
}
voidgreedy_ans(introw)
{
for(inti=row;i<n;i++)
{
intmin=0x7fffffff,k;
for(intj=0;j<n;j++)
293
()
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
if(!greedy_chk[j]&&min>m[i][j])
{
min=m[i][j];
k=j;
}
min_sol+=min;
greedy_chk[k]=1;
}
}
intmain()
{
input();
greedy_ans(0);
solve(0,0);
printf("%d",min_sol);
return0;
}
.
2 .
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
294
#include<stdio.h>
intm[11][11];
intcol_check[11],greedy_chk[11];
intn,min_sol=987654321;
intcounter;
voidsolve(introw,intscore)
{
if(score>min_sol)return;
counter++;
if(row==n)
{
if(score<min_sol)
min_sol=score;
return;
}
9: bounding
(cutting)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Part
for(inti=0;i<n;i++)
{
if(col_check[i]==0)
{
col_check[i]=1;
solve(row+1,score+m[row][i]);
col_check[i]=0;
}
}
return;
}
intmain()
{
input();
greedy_ans(0);
solve(0,0);
printf("%d\n",min_sol);
printf(" :%d\n",counter);
return0;
}
.
.
3 , 1,
2 .
1
3
12762
527737
136716
2
5
9361925694
1832171064
2098853282
145667778
5211942657
3
7
88512488945060
1455123128491
26448197338230
3711299169248
8751493289256
4149296484177
9432431615251
295
()
3 .
16
326
13700
10
97
486
10
79
330
37.5%
70.25%
99.06%
37.5%
75.77%
99.08%
.
.
296
Part
3
(M)
. .
.
m .
( 10 <= m <= 10,000 )
n .
( 1 <= n <= 10 )
.
( 10 <= <= m )
730
5
10501005001250
297
()
.
.
. .
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
#include<stdio.h>
intm,n,coin[10],ans=987654321;
voidsolve(intmon,intd)
{
if(d>ans)return;
if(mon>m)return;
if(mon==m)
{
if(d<ans)ans=d;
return;
}
for(inti=0;i<n;i++)
solve(mon+coin[i],d+1);
}
intmain()
{
scanf("%d%d",&m,&n);
for(inti=0;i<n;i++)
scanf("%d",coin+i);
solve(0,0);
printf("%d\n",ans);
return0;
}
. .
298
Part
1. .
2. , 1 .
3. .
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
#include<stdio.h>
intm,n,coin[10],ans=987654321;
voidgreedy_ans(intmon)
{
ans=0;
while(mon)
{
for(inti=n1;i>=0;i)
{
ans+=mon/coin[i];
mon%=coin[i];
}
}
}
voidsolve(intmon,intd)
{
if(d>ans)return;
if(mon>m)return;
if(mon==m)
{
if(d<ans)ans=d;
return;
}
for(inti=0;i<n;i++)
solve(mon+coin[i],d+1);
}
intmain()
{
scanf("%d%d",&m,&n);
for(inti=0;i<n;i++)
299
()
35
36
37
38
39
40
scanf("%d",coin+i);
greedy_ans(m);
solve(0,0);
printf("%d\n",ans);
return0;
}
1, 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
300
#include<stdio.h>
intm,n,coin[10],ans=987654321;
intcounter;
voidgreedy_ans(intmon)
{
ans=0;
while(mon)
{
for(inti=n1;i>=0;i)
{
ans+=mon/coin[i];
mon%=coin[i];
}
}
}
voidsolve(intmon,intd)
{
if(d>ans)return;
if(mon>m)return;
counter++;
if(mon==m)
{
if(d<ans)ans=d;
return;
}
for(inti=0;i<n;i++)
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Part
solve(mon+coin[i],d+1);
}
intmain()
{
scanf("%d%d",&m,&n);
for(inti=0;i<n;i++)
scanf("%d",coin+i);
greedy_ans(m);
solve(0,0);
printf("%d\n",ans);
printf(" %d\n",counter);
return0;
}
3 .
1
180
4
1050100500
380
7
10501005001000
500010000
570
7
10501005001000
500010000
3 .
409
185,398
62,005,020
212
7,121
52,800
111
2,193
151
48.16%
96.15%
99.94%
72.86%
98.81%
99.99%
301
()
4
(M)
n n
.
1 n
1 n
.
1.
( , ) . n 6
.
1 2 .
1 (1, 1) 2 (n, n) .
,
( .).
.
.
,
.
302
Part
(M) ()
n=6 , 3
(3, 5), (5, 5), (2, 3) .
(3, 5) 2 (5, 5) 2 , (2, 3)
1 4 + 2 + 3 = 9 ,
.
,
.
n(3n1,000)
.
w(1w15) .
(w+2) .
.
.
.
6
3
35
55
23
: (2003 )
303
()
,
. .
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
304
#include<stdio.h>
intE[1010][2],n,m,ans=987654321;
intmin(inta,intb)
{
returna>b?b:a;
}
intabs(inta)
{
returna>0?a:a;
}
intdis(inta,intb)
{
returnabs(E[a][0]E[b][0])+abs(E[a][1]E[b][1]);
}
voidsolve(inta,intb,intd)
{
intnext=(a>b?a:b)+1;
if(d>ans)return;
if(next>m+2)
{
if(d<ans)ans=d;
return;
}
solve(next,b,d+dis(a,next));
solve(a,next,d+dis(b,next));
}
intmain()
21 : bounding
(cutting)
32
33
34
35
36
37
38
39
40
41
Part
{
scanf("%d%d",&n,&m);
E[0][0]=E[0][1]=1;
E[1][0]=E[1][1]=n;
for(inti=2;i<m+2;i++)
scanf("%d%d",&E[i][0],&E[i][1]);
solve(0,1,0);
printf("%d",ans);
return0;
}
. .
1. dl .
2. 1 .
3. .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
33 : bounding
(cutting)
intE[1010][2],n,m,ans=987654321;
intmin(inta,intb)
{
returna>b?b:a;
}
intabs(inta)
{
returna>0?a:a;
}
intdis(inta,intb)
{
returnabs(E[a][0]E[b][0])+abs(E[a][1]E[b][1]);
}
voidgreedy_ans(inta,intb)
{
305
()
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
ans=0;
for(inti=2;i<m+2;i++)
{
if(dis(i,a)>dis(i,b))
ans+=dis(i,b),b=i;
else
ans+=dis(i,a),a=i;
}
}
voidsolve(inta,intb,intd)
{
intnext=(a>b?a:b)+1;
if(d>ans)return;
if(next>=m+2)
{
if(d<ans)ans=d;
return;
}
solve(next,b,d+dis(a,next));
solve(a,next,d+dis(b,next));
}
intmain()
{
scanf("%d%d",&n,&m);
E[0][0]=E[0][1]=1;
E[1][0]=E[1][1]=n;
for(inti=2;i<m+2;i++)
scanf("%d%d",&E[i][0],&E[i][1]);
greedy_ans(0,1);
solve(0,1,0);
printf("%d",ans);
return0;
}
counter .
306
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
#include<stdio.h>
Part
34 : bounding
(cutting)
intE[1010][2],n,m,ans=987654321;
intcounter;
intmin(inta,intb)
{
returna>b?b:a;
}
intabs(inta)
{
returna>0?a:a;
}
intdis(inta,intb)
{
returnabs(E[a][0]E[b][0])+abs(E[a][1]E[b][1]);
}
voidgreedy_ans(inta,intb)
{
ans=0;
for(inti=2;i<m+2;i++)
{
if(dis(i,a)>dis(i,b))
ans+=dis(i,b),b=i;
else
ans+=dis(i,a),a=i;
}
}
voidsolve(inta,intb,intd)
{
intnext=(a>b?a:b)+1;
if(d>ans)return;
counter++;
if(next>=m+2)
{
if(d<ans)ans=d;
return;
}
307
()
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
solve(next,b,d+dis(a,next));
solve(a,next,d+dis(b,next));
}
intmain()
{
scanf("%d%d",&n,&m);
E[0][0]=E[0][1]=1;
E[1][0]=E[1][1]=n;
for(inti=2;i<m+2;i++)
scanf("%d%d",&E[i][0],&E[i][1]);
greedy_ans(0,1);
solve(0,1,0);
printf("%d\n",ans);
printf(" %d.\n",counter);
return0;
}
3 .
1
11
4
52
108
63
710
11
6
22
47
33
66
62
35
11
11
93
93
39
39
33
33
33
59
59
103
103
3 .
308
31
127
4095
12
28
137
16
28
98
Part
61.29%
77.95%
98.49%
48.38%
77.95%
98.92%
. 1
.
. .
309
()
5
(M)
. , . 3
.
3
.
.
(1) .
(2) .
(3) 3
.
(4) --
.
(5) 3 , --
.
3
. .
1: d .
d = ( ) - ( )
2: d
.
310
Part
(M) ()
, 6 ,
6, 4, 4, 4, 6, 9
12, 12, 9 1
12-9=3 .
( 13, 10, 10 13-10=3 3 ,
2 .)
3
.
1. n (3n15).
2. n .
3. 0 100
1. 3 .
2. , , 3 .
6
644469
12129
3
2101
1021
9
111461111
665
311
()
.
.
, ,
.
. , , .
, ,
,
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
312
#include<stdio.h>
#include<algorithm>
intgift[30],chk[30],DIF=0x7fffffff,A,B,C,n,S;
intcomp(inta,intb)
{
returna>b;
}
voidsolve(intno,inta,intb,intc)
{
if(c>S/3||b>S/2)return;
if(no<n)
{
solve(no+1,a,b,c+gift[no]);
solve(no+1,a,b+gift[no],c);
solve(no+1,a+gift[no],b,c);
}
elseif((a>=b&&b>=c)&&ac&&ac<DIF)
{
13: bounding
(cutting)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Part
DIF=ac,A=a,B=b,C=c;
}
}
intmain()
{
inti;
scanf("%d",&n);
for(i=0;i<n;S+=gift[i++])
scanf("%d",&gift[i]);
std::sort(gift,gift+n,comp);
solve(0,0,0,0);
printf("%d%d%d\n",A,B,C);
return0;
}
.
.
+ <
+ <
1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
#include<algorithm>
13: bounding1
(cutting)
intgift[30],chk[30],DIF=0x7fffffff,A,B,C,n,S;
15: bounding2
(cutting)
intcomp(inta,intb)
{
returna>b;
}
voidsolve(intno,inta,intb,intc)
313
()
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
{
if(c>S/3||b>S/2)return;
intrest=S(a+b+c);
if(a+rest<b||b+rest<c)return;
if(no<n)
{
solve(no+1,a,b,c+gift[no]);
solve(no+1,a,b+gift[no],c);
solve(no+1,a+gift[no],b,c);
}
elseif((a>=b&&b>=c)&&ac&&ac<DIF)
{
DIF=ac,A=a,B=b,C=c;
}
}
intmain()
{
inti;
scanf("%d",&n);
for(i=0;i<n;S+=gift[i++])
scanf("%d",&gift[i]);
std::sort(gift,gift+n,comp);
solve(0,0,0,0);
printf("%d%d%d\n",A,B,C);
return0;
}
.
DIF , rest ,
.
314
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
#include<stdio.h>
#include<algorithm>
intgift[30],chk[30],DIF=0x7fffffff,A,B,C,n,S;
Part
13: bounding1
(cutting)
15: bounding2
16: bounding3
intcomp(inta,intb)
{
returna>b;
}
voidsolve(intno,inta,intb,intc)
{
if(c>S/3||b>S/2)return;
intrest=S(a+b+c);
if(a+rest<b||b+rest<c)return;
if(a(c+rest)>DIF)return
if(no<n)
{
solve(no+1,a,b,c+gift[no]);
solve(no+1,a,b+gift[no],c);
solve(no+1,a+gift[no],b,c);
}
elseif((a>=b&&b>=c)&&ac&&ac<DIF)
{
DIF=ac,A=a,B=b,C=c;
}
}
intmain()
{
inti;
scanf("%d",&n);
for(i=0;i<n;S+=gift[i++])
scanf("%d",&gift[i]);
std::sort(gift,gift+n,comp);
solve(0,0,0,0);
printf("%d%d%d\n",A,B,C);
return0;
}
315
()
counter
.
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
316
#include<stdio.h>
#include<algorithm>
intgift[30],chk[30],DIF=0x7fffffff,A,B,C,n,S;
intcounter;
intcomp(inta,intb)
{
returna>b;
}
voidsolve(intno,inta,intb,intc)
{
if(c>S/3||b>S/2)return;
intrest=S(a+b+c);
if(a+rest<b||b+rest<c)return;
if(a(c+rest)>DIF)return;
counter++;
if(no<n)
{
solve(no+1,a,b,c+gift[no]);
solve(no+1,a,b+gift[no],c);
solve(no+1,a+gift[no],b,c);
}
elseif((a>=b&&b>=c)&&ac&&ac<DIF)
{
DIF=ac,A=a,B=b,C=c;
}
}
intmain()
{
inti;
scanf("%d",&n);
for(i=0;i<n;S+=gift[i++])
scanf("%d",&gift[i]);
std::sort(gift,gift+n,comp);
14: bounding1
(cutting)
16: bounding2
17: bounding3
38
39
40
41
42
Part
solve(0,0,0,0);
printf("%d%d%d\n",A,B,C);
printf(" %d.\n",counter);
return0;
}
3
.
1
7
20147630685
10
7815748278
853404453
11
9412183879
213842569394
3 .
3,208
88,537
265,720
988
34,524
108,378
572
16,037
49,483
15
698
2,299
69.2%
61.0%
59.21%
82.16%
81.88%
81.37%
99.53%
99.21%
99.13%
. .
.
317