In 2004 NOIP universal solution to a problem set rematch

The topics covered algorithm:

  • Unhappy Jinjin: Getting Started question;
  • Peanut picking: greedy;
  • FBI tree: recursion, DP and demand interval;
  • Martian: Analog.

Unhappy Jinjin

Topic links:
Simple enumeration.
Traversal again, find \ (a [i] + b [i] \) maximum that can coordinate.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int a[8], b[8], id;
int main() {
    for (int i = 1; i <= 7; i ++) {
        cin >> a[i] >> b[i];
        if (a[i] + b[i] > 8 && (!id || a[i]+b[i] > a[id]+b[id]))
            id = i;
    }
    cout << id << endl;
    return 0;
}

Peanut picking

Topic links: https://www.luogu.org/problem/P1086
greedy.
HERE condition is "number of assumed peanut plants in these different", so we can directly in descending order according to the number of peanut, but also need to record each element of his row number, column number and The number of peanuts.
For the sorted elements from the roadside to the second \ (0 \) plants each (assuming coordinates from \ (0 \) start) time is determined, it is the first \ (0 \) plants each line number, picking good section \ (n-1 \) after the plants were returned to roadside trees are time may be determined, that is, \ (1 \) ;
and from \ (I \) plants each of the \ (i + 1 \) trees time plants, there are two ways of transition:

  1. From \ (I \) plants each directly reach the first \ (i + 1 \) plants each and picking time is spent \ (| x_i-x_ {i + 1} | + | y_i + y_ {i + 1 } | +. 1 \) (here, \ (x_i \) represents the \ (I \) plants each line number, \ (y_i \) represents the \ (I \) plants each column number, \ (| a | \) represents \ (a \) absolute value);
  2. From \ (I \) plants each jump back side of the road, then first come from the street \ (i + 1 \) plants each, and picking time spent is \ (x_i + x_ {i + 1} + 1 \) .

And I should take the smaller of the two. \ (\ Rightarrow \) This is the essence of this question greedy.
Later, I found that I want more, this problem is to assume that the monkeys in the process of taking peanuts will not return to the road, some students thinking about the possibility of return to the road in the middle, because the subject did not say spend in mobile road time, so there may be more out of the way and then go pick peanuts.
Therefore, we consider only the first two conditions can be a ~

There is then a limited time \ (K \) , we only need to determine how many lines can be off the above-described embodiment within a limited time on it.

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 440;
struct Node {
    int x, y, z;
} a[maxn];
int n, m, k, cnt, dis, ans;
bool cmp(Node a, Node b) {
    return a.z > b.z;
}
int main() {
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= m; j ++) {
            a[cnt].x = i;
            a[cnt].y = j;
            cin >> a[cnt].z;
            if (a[cnt].z) cnt ++;
        }
    }
    sort(a, a+cnt, cmp);
    for (int i = 0; i < cnt; i ++) {
        if (!i) dis += a[i].x + 1;
        // else dis += min( abs(a[i].x-a[i-1].x) + abs(a[i].y-a[i-1].y), a[i-1].x + a[i].x ) + 1;
        else dis += abs(a[i].x - a[i-1].x) + abs(a[i].y - a[i-1].y) + 1;
        if (dis + a[i].x <= k) ans += a[i].z;
    }
    cout << ans << endl;
    return 0;
}

FBI tree

Topic links: https://www.luogu.org/problem/P1087
This question is to use recursive traversal range.
Can I use dynamic programming to implement the interval and then recursively, or sets of templates tree line.
Used herein describes only the use of dynamic programming + recursive, as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = (1<<10|1);
char ch[maxn+10];
int tree[maxn<<2], n, m, sum[maxn];
void solve(int L, int R, int n) {
    if (n) {
        solve(L, L+(1<<(n-1))-1, n-1);
        solve(L+(1<<(n-1)), R, n-1);
    }
    int tmp = sum[R] - sum[L-1];
    if (tmp == (1<<n)) putchar('I');
    else if (!tmp) putchar('B');
    else putchar('F');
}
int main() {
    scanf("%d%s", &n, ch+1);
    m = (1<<n);
    for (int i = 1; i <= m; i ++)
        sum[i] = sum[i-1] + (ch[i] == '1');
    solve(1, m, n);
    return 0;
}

Martian

Topic links: https://www.luogu.org/problem/P1088
This question is an analog, simulated a full array.
But STL provides a next_permutationfunction, I'll just make use of.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int n, m, a[maxn];
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++) cin >> a[i];
    while (m --) next_permutation(a, a+n);
    for (int i = 0; i < n; i ++) {
        if (i) putchar(' ');
        cout << a[i];
    }
    cout << endl;
    return 0;
}

Author: zifeiy

Guess you like

Origin www.cnblogs.com/codedecision/p/11717543.html