1. 程式人生 > >SPFA ——2011天津網路賽——E題:for 解題報告:語法的運用

SPFA ——2011天津網路賽——E題:for 解題報告:語法的運用

For

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 807    Accepted Submission(s): 347


Problem 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 ————————————————————————————————————
題目的意思好理解,不好再描述,我也就不再贅述了。如果沒看懂,可以留言。 下面是ac程式碼: 其中用到了pair、map等語法知識,演算法麼,沒怎麼用,因為對應的排序,插入,pair、map已經幫你實現了。越來越感覺pair、map的強大了。 這是第一份程式碼,用int an【】陣列儲存輸入的matrix(每一行),用ans標記是否與其他的matrix(某一行的元素)相同。也就是說,ans就是輸出的答案陣列,也是標誌陣列,這樣不太符合軟體工程的理念,所以第二份程式碼就用了bool flag標誌是否相同,ans初始記錄每次輸入的行(matrix),處理後,記錄的是結果,或原來輸入的行(matrix)。
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;


int main()
{
    map<pair <int, int> , int> h;
	bool flag[1200];
    int ans[1200][2];
    int ab[2100][2];

    int n, j, a, b, ca;
    map<pair <int, int>, int> :: iterator it, cur;
    pair<int, int> p;
    ca = 1;
    scanf("%d" ,&n);
    while (n!= 0)
    {
        if (ca != 1)
            printf("\n");

		h.clear();
		memset(flag, 0, sizeof (flag);

        for (j = 0; j < n; j++){
            scanf("%d %d", &a, &b);
            h.insert(make_pair(make_pair(a, b), j));
			ans [j][0] = -9999999;

        }

        for (it = h.begin(); it != h.end(); it++)
        {
            p = it -> first;
            a = p . first;
            b = p . second;
            cur = it ;
            cur++;
			if (cur != h.end())
				p = cur -> first;
            while (cur !=  h.end())
            {
                p = cur -> first;
                if (p . first > a && p . second > b)
                    break;
                cur++;
            }


            if (cur == h.end())
            {
                ans [ it ->second][1] = ans[it ->second][0] = -1;

            }
            else
            {
                ans[it -> second][0] = p.first;
                ans[it -> second][1] = p.second;
            }
        }

        printf("Case %d:\n" ,ca++);

        a = b = -1;
        for (j = 0;j < n; j++)
        {

			if (ans[j][0] == -9999999)
			{
				it = h.begin();
				while (it != h.end())
				{
					p = it -> first;
					if (p.first == ab[j][0] && p.second == ab[j][1])
					{
					    printf("%d %d\n", ans[it -> second][0], ans[it ->second] [1]);
					    break;
					}
					it++;
				}


			}
			else
			{
				printf("%d %d\n", ans[j][0], ans[j][1]);


			}
		}

        scanf("%d", &n);
    }



	return 0;
}


————————————————————————————————————
第二份程式碼:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;


int main()
{
    map<pair <int, int> , int> h;
	bool flag[1200];
    int ans[1200][2], s;
//    int ab[2100][2], s;

    int n, j, a, b, ca;
    map<pair <int, int>, int> :: iterator it, cur;
    pair<int, int> p;
    ca = 1;
    scanf("%d" ,&n);
    while (n!= 0)
    {
        if (ca != 1)
            printf("\n");

		h.clear();
        s = 0;

        for (j = 0; j < n; j++)
        {
            scanf("%d %d", &a, &b);
            h.insert(make_pair(make_pair(a, b), j));
			ans [j][0] = a;
			ans [j][1] = b;
			if (h.size() == s)
			{
			    flag [j] = false;
			}
			else
			{
			    s = h.size();
			    flag[j] = true;
			}

        }

        for (it = h.begin(); it != h.end(); it++)
        {
            p = it -> first;
            a = p . first;
            b = p . second;
            cur = it ;
            cur++;
			if (cur != h.end())
				p = cur -> first;
            while (cur !=  h.end())
            {
                p = cur -> first;
                if (p . first > a && p . second > b)
                    break;
                cur++;
            }


            if (cur == h.end())
            {
                ans [ it ->second][1] = ans[it ->second][0] = -1;

            }
            else
            {
                ans[it -> second][0] = p.first;
                ans[it -> second][1] = p.second;
            }
        }

        printf("Case %d:\n" ,ca++);
//        printf ("%d %d\n", ans[0][0], ans[0][1]);
        a = b = -1;
        for (j = 0;j < n; j++)
        {

			if (flag[j] == false)
			{
				it = h.begin();
				while (it != h.end())
				{
					p = it -> first;
					if (p.first == ans[j][0] && p.second == ans[j][1])
					{
					    printf("%d %d\n", ans[it -> second][0], ans[it ->second] [1]);
					    break;
					}
					it++;
				}


			}
			else
			{
				printf("%d %d\n", ans[j][0], ans[j][1]);


			}
		}

        scanf("%d", &n);
    }



	return 0;
}