【25.47%】【codeforces 733D】Kostya the Sculptor

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya’s idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere.

Zahar has n stones which are rectangular parallelepipeds. The edges sizes of the i-th of them are ai, bi and ci. He can take no more than two stones and present them to Kostya.

If Zahar takes two stones, he should glue them together on one of the faces in order to get a new piece of rectangular parallelepiped of marble. Thus, it is possible to glue a pair of stones together if and only if two faces on which they are glued together match as rectangles. In such gluing it is allowed to rotate and flip the stones in any way.

Help Zahar choose such a present so that Kostya can carve a sphere of the maximum possible volume and present it to Zahar.

Input
The first line contains the integer n (1 ≤ n ≤ 105).

n lines follow, in the i-th of which there are three integers ai, bi and ci (1 ≤ ai, bi, ci ≤ 109) — the lengths of edges of the i-th stone. Note, that two stones may have exactly the same sizes, but they still will be considered two different stones.

Output
In the first line print k (1 ≤ k ≤ 2) the number of stones which Zahar has chosen. In the second line print k distinct integers from 1 to n — the numbers of stones which Zahar needs to choose. Consider that stones are numbered from 1 to n in the order as they are given in the input data.

You can print the stones in arbitrary order. If there are several answers print any of them.

Examples
input
6
5 5 5
3 2 4
1 4 1
2 1 3
3 2 4
3 3 4
output
1
1
input
7
10 7 8
5 10 3
4 2 6
5 5 5
10 2 8
4 2 1
7 7 7
output
2
1 5
Note
In the first example we can connect the pairs of stones:

2 and 4, the size of the parallelepiped: 3 × 2 × 5, the radius of the inscribed sphere 1
2 and 5, the size of the parallelepiped: 3 × 2 × 8 or 6 × 2 × 4 or 3 × 4 × 4, the radius of the inscribed sphere 1, or 1, or 1.5 respectively.
2 and 6, the size of the parallelepiped: 3 × 5 × 4, the radius of the inscribed sphere 1.5
4 and 5, the size of the parallelepiped: 3 × 2 × 5, the radius of the inscribed sphere 1
5 and 6, the size of the parallelepiped: 3 × 4 × 5, the radius of the inscribed sphere 1.5
Or take only one stone:

1 the size of the parallelepiped: 5 × 5 × 5, the radius of the inscribed sphere 2.5
2 the size of the parallelepiped: 3 × 2 × 4, the radius of the inscribed sphere 1
3 the size of the parallelepiped: 1 × 4 × 1, the radius of the inscribed sphere 0.5
4 the size of the parallelepiped: 2 × 1 × 3, the radius of the inscribed sphere 0.5
5 the size of the parallelepiped: 3 × 2 × 4, the radius of the inscribed sphere 1
6 the size of the parallelepiped: 3 × 3 × 4, the radius of the inscribed sphere 1.5
It is most profitable to take only the first stone.

【题解】

用一个map来搞;

map<pair<int,int>,int> dic;


dic 保留底面为(int,int)的最大高(只有最大的高才有用),和这个高的长方体的id;
每次输入的a[0..2];
sort(a+0,a+3);
然后根据dic[a[0],a[1]]和dic[a[0],a[2]]以及dic[a[1],a[2]]的情况进行更新;
第一轮更新可以获得每个底面的最大高;
第二轮再更新一次就能获得最优解了;
然后只有一个长方体的情况第一轮更新就能得到了;

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long

using namespace std;

const int MAXN = 1e5*3+100;
const int MAXSIZE = 1e5+100;

const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);

map< pair<int,int>,int> dic;

struct abc
{
    int max,id;
};

int n;
LL a[3];
LL d[MAXSIZE][3];
int ans2 = 0,l,r,ans1 =0,id1,cnt = 0;
abc kk[MAXN];

void input_LL(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void input_int(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}
//map<pair<int,int>,int> dic;
//dic 保留底面为(int,int)的最大高;

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    input_int(n);
    for (int i = 1;i <= n;i++)
    {
        for (int j = 0;j <= 2;j++)
            input_LL(d[i][j]);
        sort(d[i]+0,d[i]+3);
        for (int j = 0;j <= 2;j++)
            a[j] = d[i][j];
        LL temp1 = min(a[0],a[1]);
        temp1 = min(temp1,a[2]);
        if (temp1 > ans1)
        {
            ans1 = temp1;
            id1 = i;
        }
        pair <int,int> temp;
        temp = make_pair(a[0],a[1]);
        if (!dic[temp])
        {
            dic[temp] = ++cnt;
            kk[cnt].max = a[2];
            kk[cnt].id = i;
        }
        else
        {
            int tt = dic[temp];
            if (kk[tt].id!=i)
            {
                LL temp = min(a[0],a[1]);
                temp = min(temp,kk[tt].max+a[2]);
                if (temp > ans2)
                    ans2 = temp,l = kk[tt].id,r = i;
                if (kk[tt].max<a[2])
                {
                    kk[tt].max = a[2];
                    kk[tt].id = i;
                }
            }
        }

        temp = make_pair(a[0],a[2]);
        if (!dic[temp])
        {
            dic[temp] = ++cnt;
            kk[cnt].max = a[1];
            kk[cnt].id = i;
        }
        else
        {
            int tt = dic[temp];
            if (kk[tt].id!=i)
            {
                LL temp = min(a[0],a[2]);
                temp = min(temp,kk[tt].max+a[1]);
                if (temp > ans2)
                    ans2 = temp,l = kk[tt].id,r = i;
                if (kk[tt].max<a[1])
                {
                    kk[tt].max = a[1];
                    kk[tt].id = i;
                }
            }
        }

        temp = make_pair(a[1],a[2]);
        if (!dic[temp])
        {
            dic[temp] = ++cnt;
            kk[cnt].max = a[0];
            kk[cnt].id = i;
        }
        else
        {
            int tt = dic[temp];
            if (kk[tt].id!=i)
            {
                LL temp = min(a[1],a[2]);
                temp = min(temp,kk[tt].max+a[0]);
                if (temp > ans2)
                    ans2 = temp,l = kk[tt].id,r = i;
                if (kk[tt].max<a[0])
                {
                    kk[tt].max = a[0];
                    kk[tt].id = i;
                }
            }
        }
    }
    for (int i = 1;i <= n;i++)
    {
        for (int j = 0;j <= 2;j++)
            a[j] = d[i][j];
        pair <int,int> temp;
        temp = make_pair(a[0],a[1]);
        int tt = dic[temp];
        if (kk[tt].id!=i)
            {
                LL temp = min(a[0],a[1]);
                temp = min(temp,kk[tt].max+a[2]);
                if (temp > ans2)
                    ans2 = temp,l = kk[tt].id,r = i;
            }

        temp = make_pair(a[0],a[2]);
        tt = dic[temp];
        if (kk[tt].id!=i)
            {
                LL temp = min(a[0],a[2]);
                temp = min(temp,kk[tt].max+a[1]);
                if (temp > ans2)
                    ans2 = temp,l = kk[tt].id,r = i;
            }

        temp = make_pair(a[1],a[2]);
        tt = dic[temp];
        if (kk[tt].id!=i)
            {
                LL temp = min(a[1],a[2]);
                temp = min(temp,kk[tt].max+a[0]);
                if (temp > ans2)
                    ans2 = temp,l = kk[tt].id,r = i;
            }
    }
    if (ans1>=ans2)
    {
        puts("1");
        printf("%d
",id1);
    }
    else
    {
        puts("2");
        if (l < r)
            printf("%d %d
",l,r);
        else
            printf("%d %d
",r,l);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7632096.html