Problem Description
Problem Analysis
Originally I used array indices to store the numbers
Then I realized Big Numbers couldn’t be stored
So I switched to storing them directly in an array
Then after sorting, I used Binary Search to determine whether a number is in the array
The specific process considers the relationship between the remaining money and the number
If the money is enough to buy the toy with this number
Then consider whether the money left after buying it can buy another toy
If not
Then you need to choose a toy with the largest possible value
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
| #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100020];
int b[100020];
int al;
int n,m;
bool findm(int m) {
int lt=0,rt=al-1;
while(lt<=rt) {
int t=(lt+rt)/2;
if(m==a[t])
return true;
else if(m>a[t]) {
lt=t+1;
} else
rt=t-1;
}
return false;
}
int findl(int k,int l) {
int i;
for(i=l; i>=0; i--) {
if(findm(i))
continue;
else
break;
}
return i;
}
int findn(int k,int l) {
int i;
for(i=k+1; i<=m; i++) {
if(findm(i))
continue;
else
break;
}
return i;
}
int main() {
while(scanf("%d %d",&n,&m)!=EOF) {
al=0;
memset(a,0,sizeof(a));
for(int i=0; i<n; i++) {
int t;
scanf("%d",&a[al++]);
}
sort(a,a+al);
int cnt=0;
int l=m;
for(int i=1; i<=l&&m>0; i++) {
if(findm(i))
continue;
else {
if(m>=i) {
int tn=findn(i,m);
if(tn==m||m-i>=tn) {
b[++cnt]=i;
m-=i;
} else {
int t=findl(i,m);
b[++cnt]=t;
break;
}
} else
break;
}
}
printf("%d\n",cnt);
for(int i=1; i<=cnt; i++) {
printf("%d",b[i]);
if(i==cnt)
printf("\n");
else
printf(" ");
}
}
return 0;
}
|
Problem link:【CodeForces】[659C]Tanya and Toys