H - Hello World!

Description

We know that Ivan gives Saya three problems to solve (Problem F), and this is the first problem.
We need a programmer to help us for some projects. If you show us that you or one of your friends is able to program, you can pass the first hurdle.
I will give you a problem to solve. Since this is the first hurdle, it is very simple.”
We all know that the simplest program is the “Hello World!” program. This is a problem just as simple as the “Hello World!”
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 showed 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.
Saya is not a programmer, so she comes to you for help
Can you solve this problem for her?

Input

The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N1000), which represents the number of marked element.
Each of the next N lines containing two integers r and c, represent the element’s row and column. You can assume that 0<r, c300. A marked element can be repeatedly showed.
The last case is followed by a line containing one zero.

Output

For each case, print the case number (1, 2 …), and for each element’s row and column, output the result. Your output format should imitate the sample output. Print a blank line after each test case.

Sample Input

3
1 2
2 3
2 3

0

Sample Output

Case 1:
2 3
-1 -1
-1 -1

题意:

有一个大的矩阵,标识出一些点,输入的是标记的点的行与列坐标,然后寻找一个比标记的元素的行与列都要大的元素,如果有多种情况,那么输出行最小的,如果行相同有多种情况,输出列最小的。

思路:

创建结构体,用来存放行与列,运用结构体定义两个结构体变量,对在其中的一个进行sort排序,注意,sort排序中需要我们进行自定义排序,然后在和未排序的每一个元素进行比较行与列,第一个符合条件行大于这个元素行,列大于这个元素的列的即为所要输出的,如果没有,则输出-1   -1。

细节:

注意sort函数中的自定义排序,可以使用bool cmp,如果 a.r<b.r 则return1,若是a.r=b.r那么在比较列,如果a.c<b.c,则返回1.

代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct point
{
    int r,c;
};
bool cmp(const point &a,const point &b)
{
    if(a.r<b.r)
    return 1;
    if((a.r==b.r)&&(a.c<b.c))
    return 1;
    return 0;
}
int main()
{
    int n,i,j,sum=0,r,c;
    while(cin>>n)
    {
        if(n==0)
        break;
        point a[1002],b[1002];
        for(i=0;i<n;i++)
        {
            cin>>r>>c;
            a[i].r=r;
            a[i].c=c;
            b[i].r=r;
            b[i].c=c;
        }

        sort(b,b+n,cmp);

        cout<<"Case "<<++sum<<":"<<endl;
        for(i=0;i<n;i++)
        {
            r=a[i].r;
            c=a[i].c;
            for(j=0;j<n;j++)
            {
                if(b[j].r>r&&b[j].c>c)
                {
                    cout<<b[j].r<<' '<<b[j].c<<endl;
                    break;
                }
            }
            if(j==n)
                cout<<-1<<' '<<-1<<endl;
        }
        cout<<endl;
    }
    return 0;
}

心得:

此题目的关键是要理解,两个相同的结构体,只要对其中一个进行排序,然后在和另一个未排序的每一个元素进行行与列的比较,如果有符合条件的输出即可,注意是输出第一个符合条件的.注意解题的方法和思考的方向。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值