Noip simulation exercises 6

Noip simulation exercises 6

  • Out of 300, I 110. After 260 amendments.
  • Medium difficulty.

gambler

Description

  • There are N gambler hands each have their own chips. In gambling, they can borrow money from gamblers around, so they

    The chips may be negative, but the chips are necessarily integers. When the gambling, N gambler among the chips just another three

    The sum of chips gambler who is the winner. If more than one qualified gambler, select the largest man-made chips winner.

    5 gambler e.g., at the end of each gambling 2,3,5,7,12, they are the winners of the holder 12, since 12 = 2 + 3 + 7.

Input

  • Conduct a first integer n (. 1 <= n <= 1000) with n represents gambler. Next n lines of a

    Integers X (. 8 ^ 10 <= X <= 10 ^. 8) indicates the end of gambling chips in the hands of each gambler.

Output

  • Only at the end of a gambling integer number of chips in the hands of the winner. If there is no winner, the output "no

    solution”。

Sample Input

5

2

3

5

7

12

Sample output

12

answer:

  • Violence enumeration.
  • emmm ... .. my question burst of zero.
  • Beginning to write dp, read, read, found wrong, immediately turned into a record search. Confidently pay up, the result of the explosion. The reason is that there is a boundary situation is not taken into account.
  • 3 positive solutions is recirculated + pruning. Shocked.
  • Concrete is the first order. The first winner then re-enumeration money, money by a second re-enumeration 1 konjac, konjac money third re-enumeration of the user 2, is not necessary to enumerate those konjac money 3, because the front by enumeration Ruo's calculated, assuming counted out the money for the 4 x. Then if x is definitely explosive barrels sentenced space in the array. Less direct binary search chant.
  • Is written complexity O (n ^ 3), I had not. Pruning it to the point! When you enumerate money konjac in 1, assuming current konjac who have money 1 * 3 <money winner. Less direct break. The reason is very simple, three times konjac who most people are not, then how could the rest of the portfolio up to get?

  • Summary: small search data can be considered under. But like this, only three such data extremely small. + Pruning can be considered violent.
  • Do not want to get any problem so difficult.

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

int n;
int a[N];

int find(int x)
{
    int pos = lower_bound(a + 1, a + 1 + n, x) - a;
    return a[pos];
}

int main()
{
    cin >> n;
    if(n < 4) {cout << "no solution"; return 0;}
    for(int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    for(int i = n; i >= 4; i--)
        for(int j = i - 1; j >= 1; j--)
        {
            if(a[j] * 3 < a[i]) break;
            for(int k = j - 1; k >= 1; k--)
            {
                if(a[k] * 2 + a[j] < a[i]) break;
                int v1 = a[i], v2 = a[j], v3 = a[k], v4 = v1 - v2 - v3;
                if(find(v4) == v4) {cout << v1; return 0;}
            }
        }
    cout << "no solution";
    return 0;
}

Monkeys eat bananas

Description

  • Some researchers are studying monkeys IQ, they are a bunch of bananas hanging from the ceiling, and provides some boxes

    The child, if the monkey is very smart, it will overlay these boxes together, until I got the banana so far. Researchers altogether

    Providing the n kinds of boxes, but is not limited to the number of each box. Each box is rectangular with dimensions of (xi, yi, zi).

    When superimposing the box, placed in a box above the requirement of the bottom surface is smaller than the length and width must be placed in the bottom of a box

    The length and width of the top surface. (Box only one by one superimposed)

    Given the size of some of the box, they can be programmed to obtain the maximum height added together.

Input

  • First row n-(. 1 <= n- <= 30), indicates several boxes.

    Next n lines of three numbers xi, yi, zi, respectively, the size of the box.

    . 1 <= XI , Yi , zi and <= 104.

Output

  • Only a number that represents the maximum height they can be added together.

Sample Input

2

6 8 10

5 5 5

Sample output

21

answer:

  • dp。
  • A direct out. But just unpacking to see the range of data that question actually <= 30 scared, and thought it was like the pressure. The results of careful thought, in fact, the essence is the "missile intercept", then quickly cut off. (This problem because the data range is too small can also search through the ... ...

  • Because of a box can over and over, so a box is actually a "6" boxes. Suppose x, y, z. There are two kinds of doing high x, y do two kinds of high and, z has to do two kinds of high, a total of six kinds. N * 6 was then added into the box Species array. This array of LIS can do it again. But note that maintenance is a maximum, not the longest up / down sequence.

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

struct A {int x, y, z;} a[N];
int n, ans, cnt;
int dp[N];

bool cmp(A u, A v)
{
    if(u.x == v.x)
    {
        if(u.y == v.y) return u.z > v.z;
        else return u.y > v.y;
    }
    else return u.x > v.x;
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int x, y, z;
        cin >> x >> y >> z;
        a[++cnt].x = x, a[cnt].y = y, a[cnt].z = z;
        a[++cnt].x = y, a[cnt].y = x, a[cnt].z = z;
        a[++cnt].x = y, a[cnt].y = z, a[cnt].z = x;
        a[++cnt].x = z, a[cnt].y = y, a[cnt].z = x;
        a[++cnt].x = z, a[cnt].y = x, a[cnt].z = y;
        a[++cnt].x = x, a[cnt].y = z, a[cnt].z = y;
    }
    n *= 6;
    sort(a + 1, a + 1 + n, cmp);
    for(int i = 1; i <= n; i++) dp[i] = a[i].z;
        for(int i = 2; i <= n; i++)
            for(int j = 1; j < i; j++)
                if(a[j].x > a[i].x && a[j].y > a[i].y)
                    dp[i] = max(dp[i], dp[j] + a[i].z);
    for(int i = 1; i <= n; i++) ans = max(ans, dp[i]);
    cout << ans;
    return 0;   
}

Necklace Factory

topic:

  • There are pictures, transfer link

answer:

  • Balanced tree / tree line
  • This problem positive solution I tried to get the Gugu Gu,Fill in the future.
  • Violence first fight, was actually the first time since R staggered operation was wrong, a great pity.
  • This idea of ​​the violence problem is very simple, but many of the details!
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 500005
using namespace std;

int n, c, q;
int a[N], b[N];
bool vis[N];

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

void step1()
{
    int k = read() % n;
    for(int i = 1; i <= n; i++)
    {
        int j = (i + k) % n;
        if(!j) j = n;
        b[j] = a[i];
    }
    for(int i = 1; i <= n; i++) a[i] = b[i];
}

void step2()
{
    for(int i = 2; i <= (n + 1) / 2; i++)
        swap(a[i], a[n - i + 2]);
}

void step3()
{
    int u = read(), v = read();
    swap(a[u], a[v]);
}

int getCnt(int u, int v)
{
    int pos = u, cnt = 0;
    while(pos != v)
    {
        cnt++, pos++;
        if(pos == n + 1) pos = 1;
    }
    return cnt;
}

void step4()
{
    int u = read(), v = read(), w = read();
    int pos = u, cnt = 1 + getCnt(u, v);
    for(int i = 1; i <= cnt; i++)
    {
        a[pos] = w;
        pos++;
        if(pos == n + 1) pos = 1;
    }
}

void step5()
{
    int ans = 0, pos;
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++)
        if(!vis[i])
        {
            ans++, vis[i] = 1;
            pos = i;
            while(1)
            {
                pos++;
                if(pos == n + 1) pos = 1;
                if(vis[pos]) break;
                if(a[pos] == a[i]) vis[pos] = 1;
                else break;
            }
            pos = i;
            while(1)
            {
                pos--;
                if(!pos) pos = n;
                if(vis[pos]) break;
                if(a[pos] == a[i]) vis[pos] = 1;
                else break;
            }
        }
    printf("%d\n", ans);
}

void step6()
{
    int l = read(), r = read();
    int ans = 0, u = l, v = -1, cnt = 1 + getCnt(l, r);
    for(int i = 1; i < cnt; i++)
    {
        v = (u + 1) % n;
        if(!v) v = n;
        if(a[u] != a[v]) ans++;
        u++;
        if(u == n + 1) u = 1;
    }
    printf("%d\n", ans + 1);
}

int main()
{
    cin >> n >> c;
    for(int i = 1; i <= n; i++) a[i] = read();
    cin >> q;
    for(int i = 1; i <= q; i++)
    {
        char c[4]; scanf("%s", c);
        if(c[0] == 'R') step1();
        else if(c[0] == 'F') step2();
        else if(c[0] == 'S') step3();
        else if(c[0] == 'P') step4();
        else if(c[0] == 'C' && c[1] == 'S') step6();
        else if(c[0] == 'C') step5();
    }
    return 0;
}

Guess you like

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