山东省ACM第一届省赛 Hello world

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<N≤1000), 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, c≤300. 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


题意:

有一个矩阵对于每个元素找剩余元素中row和column都大于标记元素的显示元素,若有多个符合要求的,找其中row中最小的,若也有多个符合要求的找其中column最小的。


解题思路:

将显示元素按row优先再按column优先进行排序,在对每个元素进行比较。数据比较类型考虑排序简化运算。


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct
{
int a;
int b;
}A;
bool cmp(A a,A b)
{
if(a.a==b.a)
{
return a.b<b.b;
}
else
return a.a<b.a;

}
int main()
{
A p1[1010],p[1010];
int n,count_num=0;
while(scanf("%d",&n)&&n)
{

for(int i=0;i<=n-1;++i)
{
scanf("%d%d",&p[i].a,&p[i].b);
p1[i]=p[i];
}

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

sort(p1,p1+n,cmp);
int flag,j;
for(int i=0;i<=n-1;++i)
{
flag=0;
for( j=0;j<=n-1;++j)
{
if(p[i].a<p1[j].a&&p[i].b<p1[j].b)
{
flag=1;
break;
}
}
if(flag==1)
cout<<p1[j].a<<" "<<p1[j].b<<endl;
else
cout<<"-1 -1"<<endl;
}
cout<<endl;
}
return 0;
 }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值