简单题->hdu 3626 For

For
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

LMY is a mathematics lover. Now LMY likes to play matrix. LMY designs such a matrix problem. In a large matrix, there are some elements has been marked. For every marked element, return a marked element whose row and column are larger than the original marked element’s row and column respectively. If there are multiple solutions, return the element whose row is the smallest; and if there are still multiple solutions, return the element whose column is the smallest. If there is no solution, return -1 -1.
Of course LMY wants YY also like matrix. So she lets YY develop a program to solve the problem. But YY is very busy now. He should learn English for his enrollment examination for Ph.D. students. Could you help YY develop the program?
 

Input

The input consists of multiple test cases. For each test case, the first line contains only one integer n. n ≤ 1000. Each of the next n lines describes a marked element’s row and column. A marked element’s row and column can be repeatedly showed. There is a blank line between two consecutive test cases.
End of input is indicated by a line containing a zero.
 

Output

Start each test case with "Case #:" on a single line, where # is the case number starting from 1. For each element’s row and column, output the result. The format is showed as sample output. There is a blank line between two consecutive test cases.
 

Sample Input

     
     
5 1 8 5 7 6 9 2 8 3 1 10 7 5 7 5 3 1 9 9 1 4 7 4 2 3 9 5 4 5 3 4 0
 

Sample Output

     
     
Case 1: 6 9 6 9 -1 -1 6 9 5 7 Case 2: 9 9 9 9 4 5 -1 -1 4 5 9 5 3 4 -1 -1 9 9 4 5
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define MAXN 1000 + 10

struct Node
{
    int x, y, id;
    int ansx, ansy;
}temp[MAXN];

int n, k;

bool cmp(Node a, Node b)
{
    if (a.x < b.x)
    {
        return true;
    }
    else if (a.x == b.x && a.y < b.y)
    {
        return true;
    }

    return false;
}

bool cmp2(Node a, Node b)
{
    return a.id < b.id;
}

void solve()
{
    sort(temp, temp + n, cmp);

    for (int i = 0; i < n; i++)
    {
        int u = -1;

        for (int j = i + 1; j < n; j++)
        {
            if (temp[j].x > temp[i].x)
            {
                u = j;
                break;
            }
        }

        if (u != -1)
        {
            for (int j = u; j < n; j++)
            {
                if (temp[j].y > temp[i].y)
                {
                    temp[i].ansx = temp[j].x;
                    temp[i].ansy = temp[j].y;
                    break;
                }
            }
        }
    }

    sort(temp, temp + n, cmp2);

    cout << "Case " << ++k << ":" << endl;

    for (int i = 0; i < n; i++)
    {
        printf("%d %d\n", temp[i].ansx, temp[i].ansy);
    }
}

void input()
{
    int ff = 0;
    while(scanf("%d", &n) != EOF)
    {
        if (n == 0)
        {
            break;
        }
        if(ff != 0)
        {
            printf("\n");
        }
        ff = 1;

        for (int i = 0; i < n; i++)
        {
            scanf("%d %d", &temp[i].x, &temp[i].y);
            temp[i].id = i;
            temp[i].ansx = temp[i].ansy = -1;
        }

        solve();
    }


}

int main()
{
    input();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值