本节内容再深入贪心算法和算法设计
其中会用到一些数学证明
辅助证明一些性质

然后在这些性质的基础上
进行贪心选择

构造法

LA4378
LA4378

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
const int maxn = 100000 + 10;

class Data {
public:
int val, id;
Data(int v = 0, int i = 0) : val(v), id(i) {}
};

bool cmp(const Data& lhs, const Data& rhs) {
return lhs.val > rhs.val;
}

Data data[maxn];
int ans[maxn];
int n;

void init() {
Set(ans, 0);
}

int main() {
freopen("input.txt", "r", stdin);
while (~scanf("%d", &n)) {
init();
llong sum = 0;
_for(i, 0, n) {
scanf("%d", &data[i].val);
data[i].id = i;
sum += data[i].val;
}

if(sum % 2) {
printf("No\n");
continue;
}
else sum /= 2;

sort(data, data + n, cmp);
_for(i, 0, n) {
if(data[i].val <= sum) {
sum -= data[i].val;
ans[data[i].id] = 1;
}
else ans[data[i].id] = -1;
}

printf("Yes\n");
_for(i, 0, n - 1) printf("%d ", ans[i]);
printf("%d\n", ans[n - 1]);
}
}

环形排列

UVA10570
UVA10570
UVA10570

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
const int maxn = 500 + 10;
int A[maxn], B[maxn], pos[maxn];
int N;

void init() {
Set(A, 0);
Set(B, 0);
Set(pos, 0);
}

void copy(int st, int step) {
_for(i, 0, N) {
B[i] = A[(st + step * i + N) % N];
pos[B[i]] = i;
}
}

int solve() {
int ans = 0;
_for(i, 0, N) {
int p = pos[i];
if(p == i) continue;
pos[B[i]] = p;
pos[i] = i;
swap(B[i], B[p]);
ans++;
}
return ans;
}

int main() {
freopen("input.txt", "r", stdin);
while (scanf("%d", &N) == 1 && N) {
init();

_for(i, 0, N) {
scanf("%d", &A[i]);
A[i]--;
}

int res = maxn;
_for(i, 0, N) {
copy(i, 1);
res = min(res, solve());
copy(i, -1);
res = min(res, solve());
}
printf("%d\n", res);
}
}

图上的贪心算法

LA4993
LA4993

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
const int maxn = 10000 + 10;

class Edge {
public:
int from, to;
Edge(int f = 0, int t = 0) : from(f), to(t) {}
};

vector<Edge> edges;
vector<int> G[maxn];
int n, m, k;

int color[maxn], vis[maxn];

void init() {
Set(color, 0);
Set(vis, 0);
_for(i, 0, maxn) G[i].clear();
edges.clear();
k = 0;
}

void initVis() {
Set(vis, 0);
}

void addEdge(int from, int to) {
edges.push_back(Edge(from, to));
int m = edges.size();
G[from].push_back(m - 1);
}

void dfs(int u) {
initVis();
_for(i, 0, G[u].size()) {
Edge cur = edges[G[u][i]];
if(color[cur.to]) vis[color[cur.to]] = 1;
}

_rep(i, 1, k) {
if(!vis[i]) {
color[u] = i;
break;
}
}

_for(i, 0, G[u].size()) {
Edge cur = edges[G[u][i]];
if(!color[cur.to]) dfs(cur.to);
}
}

int main() {
freopen("input.txt", "r", stdin);
int kase = 0;

while (scanf("%d%d", &n, &m) != EOF) {
init();
if(++kase > 1) printf("\n");

int u, v;
_for(i, 0, m) {
scanf("%d%d", &u, &v);
addEdge(u - 1, v - 1);
addEdge(v - 1, u - 1);
}
_for(i, 0, n) k = max(k, (int)G[i].size());

if(k % 2 == 0) k++;
cout << k << endl;

dfs(0);
_for(i, 0, n) cout << color[i] << endl;
}
}

枚举排列

处理小数点的方法

1
2
3
4
5
6
7
8
9
10
11
12
int readint() {
char buf[8];
scanf("%s", buf);

int a = 0, b = 0;
sscanf(buf, "%d", &a);

char* pp = strchr(buf, '.');
if(pp) sscanf(++pp, "%d", &b);

return a * 100 + b;
}

example

LA3664
LA3664

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
const int maxn = 16384 + 5;
vector<int> per[maxn];
vector<int> rk;
int n;

void init() {
_for(i, 0, maxn) per[i].clear();
rk.clear();
}

int readint() {
char buf[8];
scanf("%s", buf);

int a = 0, b = 0;
sscanf(buf, "%d", &a);

char* pp = strchr(buf, '.');
if(pp) sscanf(++pp, "%d", &b);

return a * 100 + b;
}

int read() {
int x;
scanf("%d", &x);
return x;
}

int solve() {
int lastScore = -1, lastID = -1;
_for(i, 0, rk.size()) {
int id = rk[i];
if(lastScore == -1) lastScore = per[id].front();
else {
bool found = false;
_for(j, 0, per[id].size()) {
if(per[id][j] < lastScore || (per[id][j] == lastScore && id > lastID)) {
lastScore = per[id][j];
found = true;
break;
}
}
if(!found) return -1;
}
lastID = id;
}
return lastScore;
}

int main() {
freopen("input.txt", "r", stdin);
for(int kase = 1; scanf("%d", &n) == 1 && n; kase++) {
init();

_for(i, 0, n) {
per[i].clear();

per[i].push_back(0);
_for(j, 0, 3) per[i].push_back(readint());

per[i].push_back(per[i][1] + per[i][2]);
per[i].push_back(per[i][1] + per[i][3]);
per[i].push_back(per[i][2] + per[i][3]);
per[i].push_back(per[i][1] + per[i][2] + per[i][3]);

sort(per[i].begin(), per[i].end(), greater<int>());
}

_for(i, 0, n) rk.push_back(read() - 1);

// then solve the problem
int ans = solve();
if(ans == -1) printf("Case %d: No solution\n", kase);
else printf("Case %d: %d.%02d\n", kase, ans / 100, ans % 100);
}
}