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
Sample Output
Case 1: 2 3 -1 -1 -1 -1
依旧省赛题。给出N和点,找出横纵坐标都比当前点大的点。若有多个点,输出找到的第一个点,即横坐标(横坐标相同则为纵坐标)最小的点。一开始想用二维数组存该点是否已标记,确实也这么做了,发现既费空间,又费时间,果然TLE。要用更直接的方法:排序,查找。
正确思路:结构体存点(开两个,防止乱序),排序一个,遍历,输出。代码贴上:
#include<cstdio> #include<algorithm> using namespace std; struct point{ int x,y; }; bool cmp(point x,point y){ if(x.x<y.x)return true; else if(x.x==y.x&&x.y<y.y)return true; else return false; } int main(){ int N,kase=0; while(scanf("%d",&N)==1&&N){ point p1[1100],p2[1100]; for(int i=0;i<N;i++){ scanf("%d %d",&p1[i].x,&p1[i].y); p2[i].x=p1[i].x; p2[i].y=p1[i].y; } sort(p2,p2+N,cmp); printf("Case %d:\n",++kase); for(int i=0;i<N;i++){ int j=0; while((p1[i].x>=p2[j].x||p1[i].y>=p2[j].y)&&j<N) j++; if(j<N) printf("%d %d\n",p2[j].x,p2[j].y); else printf("-1 -1\n"); } printf("\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。