现在的位置: 首页 > 综合 > 正文

ZJUT Practice 3.3 A – for( HDU – 3626)

2012年12月03日 ⁄ 综合 ⁄ 共 2337字 ⁄ 字号 评论关闭
Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

[]   [Go Back]   [Status]  

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

 

Source

2010 Asia Regional Tianjin Site ―― Online Contest
 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 const int maxn =1010;
 5 struct matrix{
 6     int x,y;
 7 };
 8 matrix ma[maxn];
 9 int flag[maxn];
10 int main(){
11     //freopen("in.txt","r",stdin);
12     for(int n,cases=1;cin>>n && n!=0;cases++){
13         for(int i=0;i<n;i++)
14             cin>>ma[i].x>>ma[i].y;
15         cout<<(cases==1?"":"\n")<<"Case "<<cases<<":\n";
16         for(int i=0;i<n;i++){
17             int min,num=0;
18             memset(flag,0,sizeof(flag));
19             for(int j=0;j<n;j++){
20                 if(ma[j].x>ma[i].x && ma[j].y>ma[i].y){
21                     flag[j]=1;
22                     num++;
23                     min=j;
24                 }
25             }
26             if(num==0)
27                 cout<<"-1 -1\n";
28             else if(num==1)
29                 cout<<ma[min].x<<" "<<ma[min].y<<"\n";
30             else{
31                 for(int j=0;j<n;j++){
32                     if(flag[j]==1 && ma[j].x<ma[min].x){
33                         min=j;
34                     }
35                 }
36                 num=0;
37                 for(int j=0;j<n;j++){
38                     if(flag[j]==1 && ma[j].x==ma[min].x){
39                         num++;
40                         flag[j]=2;
41                     }
42                 }
43                 if(num ==1)
44                     cout<<ma[min].x<<" "<<ma[min].y<<"\n";
45                 else{
46                     for(int j=0;j<n;j++){
47                         if(flag[j]==2 && ma[j].y<ma[min].y)
48                             min=j;
49                     }
50                     cout<<ma[min].x<<" "<<ma[min].y<<"\n";
51                 }
52             }
53         }
54     }
55 }

我用最傻逼的办法去模拟。

 

抱歉!评论已关闭.