Noip simulation exercises 5

Noip simulation exercises 5

  • Out of 300, I 240. After 300 amendments.
  • Medium difficulty.

Space password

Description

  • Humans have been committed to exploring extraterrestrial civilization, the scientists constructed for this purpose a giant radio telescope
    to receive cosmic rays. One day spate mysterious waves from deep space, each wave can be regarded as a string of ternary
    number, i.e. composed of 0, 1, 2, the length of not more than 40, most particularly by radio wave analysis showed that a large number of : each string of electric
    waves of consecutive 0 has not. Scientists suspect the airwaves contain alien password, in order to decipher the mystery,
    and now you write a program, the statistics length to meet the requirements of the total number N of radio waves.
    For example: When N = 2, to meet the requirements of a radio wave with 8: 01, 02, 10, 11, 12, 20, 21, 22. 00 dissatisfaction
    meet the requirements, because there has been a continuous 0

Input

  • Password.in input file is given a positive integer of not more than 40 N, the wave length of the string.
    50% of the data 1≤N≤15.

Output

  • It gives the length of the file and not the number of password.out string of consecutive 0 N.

Sample Input

2

Sample output

8

answer:

  • dp。

  • Set DP (i, 0/1/2) indicates the number of programs is 0/1/2 at the i-th position, then the request is dp (n, 0) + dp (n, 1) + dp (n, 2). Transfer is very simple, see the code.

#include <iostream>
#include <cstdio>
#define N 55
using namespace std;

long long n;
long long f[N][4];

int main()
{
    cin >> n;
    f[1][0] = f[1][1] = f[1][2] = 1;
    for(int i = 2; i <= n; i++)
        f[i][0] = f[i - 1][1] + f[i - 1][2],
        f[i][1] = f[i - 1][0] + f[i - 1][1] + f[i - 1][2],
        f[i][2] = f[i - 1][0] + f[i - 1][1] + f[i - 1][2];
    cout << f[n][0] + f[n][1] + f[n][2];
    return 0;
}

Job scheduling program

topic:

  • There are pictures, transfer link

answer:

  • simulation.
  • This question is difficult topic in easy to understand, in fact, problem-solving approach in the title, read the topic is a simulation questions. The following is the translated version:

  • There are m sets of n machined workpieces, each workpiece has the m steps, each process completed on a different specified machine. Each process has specified for each piece of processing time. Now to give you an "order of arrangement," you order "to arrange the order of" analog to finally determine the shortest time to complete these tasks take.
  • What stunned, simulation ah!

#include <iostream>
#include <cstdio>
#define N 25
#define inf 0x7fffffff
using namespace std;

struct A {int obj, id, val, las;} a[N * N];
struct B {int id, val;} b[N][N];
int n, m, ans;
int g[N], t[N];
int form[N][405];

bool check(int id, int l, int r)
{
    for(int i = l; i <= r; i++)
        if(form[id][i]) return 0;
    return 1;
}

void cal(int obj, int id, int val, int las)
{
    if(!las)
    {
        for(int i = 1; ; i++)
            if(check(id, i, i + val - 1))
            {
                for(int j = i; j <= i + val - 1; j++)
                    form[id][j] = obj;
                t[id] = max(t[id], i + val - 1);
                break;
            }
        return;
    }
    int idd = a[las].id, pos;
    for(int i = t[idd]; i >= 1; i--)
        if(form[idd][i] == obj) {pos = i; break;}
    for(int i = pos + 1; ; i++)
        if(check(id, i, i + val - 1))
        {
            for(int j = i; j <= i + val - 1; j++)
                form[id][j] = obj;
            t[id] = max(t[id], i + val - 1);
            break;
        }
}

int main()
{
    cin >> m >> n;
    for(int i = 1; i <= m * n; i++) cin >> a[i].obj;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> b[i][j].id;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> b[i][j].val;
    for(int i = 1; i <= m * n; i++)
        a[i].id = b[a[i].obj][++g[a[i].obj]].id,
        a[i].val = b[a[i].obj][g[a[i].obj]].val;
    for(int i = 1; i <= m * n; i++)
    {
        int x = a[i].obj;
        for(int j = i - 1; j >= 1; j--)
            if(a[j].obj == x) {a[i].las = j; break;}
    }
    for(int i = 1; i <= m * n; i++)
        cal(a[i].obj, a[i].id, a[i].val, a[i].las);
    for(int i = 1; i <= m; i++) ans = max(ans, t[i]);
    cout << ans;
    return 0;
}

Water into the city

topic:

  • There are pictures, transfer link

answer:

  • bfs + greedy.
  1. bfs calculated for each point beyond the control of the left boundary, right boundary.

  2. Look at all indicating whether it reaches the arid region. Does not go to step 3, step 4 can enter.

  3. Sweep it again to see how many arid region can not be built facilities, output the answer. Go to step 5.

  4. Greedy run again interval coverage, output the answer. Go to step 5.

  5. End.

  • The question is, in those arid zone between the left and right edges of each point can control, how do you know that you can control it? In other words, how do you know it must be controlled for a range of it?
  • The big blog speaks good, recommended.
  • Finally add, my code locally AC, Luo Valley 90pts timeout a point. It is said to enhance memory of the data, thenI'm lazy I do not continue corrected.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 505
#define inf 0x7fffffff
using namespace std;

struct E {int l, r;} e[N];
struct Node {int x, y, h;};
int n, m, last, ans;
int a[N][N];
bool tag[N];
bool vis[N][N];
int dx[5] = {0, -1, 1, 0, 0};
int dy[5] = {0, 0, 0, -1, 1};

int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x *= f;
}

void bfs(int x, int y, int dfn)
{
    queue<Node> que;
    memset(vis, 0, sizeof(vis));
    Node tmp;
    tmp.x = x, tmp.y = y, tmp.h = a[x][y];
    que.push(tmp), vis[x][y] = 1;
    while(que.size())
    {
        Node now = que.front(); que.pop();
        for(int i = 1; i <= 4; i++)
        {
            tmp.x = now.x + dx[i], tmp.y = now.y + dy[i], tmp.h = a[tmp.x][tmp.y];
            if(now.h > tmp.h && !vis[tmp.x][tmp.y] && tmp.x >= 1 && tmp.x <= n && tmp.y >= 1 && tmp.y <= m)
            {
                vis[tmp.x][tmp.y] = 1;
                que.push(tmp);
            }
        }
    }
    for(int i = 1; i <= m; i++)
        if(vis[n][i]) {e[dfn].l = i; break;}
    for(int i = m; i >= 1; i--)
        if(vis[n][i]) {e[dfn].r = i; break;}
    for(int i = e[dfn].l; i <= e[dfn].r; i++) tag[i] = 1;
}

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            a[i][j] = read();
    for(int i = 1; i <= m; i++) bfs(1, i, i);
    for(int i = 1; i <= m; i++)
        if(!tag[i])
        {
            cout << 0 << endl;
            for(int j = 1; j <= m; j++)
                if(!tag[j]) ans++;
            cout << ans;
            return 0;
        }
    cout << 1 << endl;
    int l = 1;
    while(l <= m)
    {
        int r = 0;
        for(int i = 1; i <= m; i++)
            if(e[i].l <= l) r = max(r, e[i].r);
        ans++, l = r + 1;
    }
    cout << ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11346531.html