luogu P1199 [Three Games

First, it is clear that this is a problem greedy.

Many dalao greedy method has been written out, find the greatest generals generals times each large value.

What do we define an array \ (F [N] [2] \) , where \ (f [i] [0 ] \) for storing the first \ (I \) views of a large value generals, \ (F [ i] [1] \) for storing a first \ (I \) a maximum of generals.

Do it for the first \ (i \) line \ (j \) column force values we read incoming \ (x \) is both the first \ (i \) force value generals, but also the first \ (i + j \ ) force generals of value.

What you can do

if(x > f[i][1]) f[i][0] = f[i][1],f[i][1] = x;//如果x大于最大值,呢么当前的最大值就是该武将的次大值。
else if(x > f[i][0]) f[i][0] = x;//如果x只大于次大值,呢么x就是新的次大值

This way we can avoid this sort of complex and time-consuming process

AC coding

#include <bits/stdc++.h>
using namespace std;


const int N = 505;
int n,maxx = 0,f[N][2] = {};


inline int read()
{
    register int x = 0;
    register char ch = getchar();
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch >= '0' && ch <= '9')
    {
        x = (x<<3)+(x<<1) + ch-'0';
        ch = getchar();
    }
    return x;
}



int main()
{
    n = read();
    
    for(register int i = 1;i <= n;i++)
    {
        for(register int j = 1;j <= n-i;j++)
        {
            register int x = read();
            if(x > f[i][1]) f[i][0] = f[i][1],f[i][1] = x;
            else if(x > f[i][0]) f[i][0] = x;
            
            if(x > f[i+j][1]) f[i+j][0] = f[i+j][1],f[i+j][1] = x;
            else if(x > f[i+j][0]) f[i+j][0] = x;
        }
    }
    
    for(register int i = 1;i <= n;i++) maxx = max(maxx,f[i][0]);
    printf("1\n%d\n",maxx);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Mark-X/p/11404653.html