SPFA ——2011天津网络赛——E题:for 解题报告:语法的运用

来源:互联网 发布:淘宝手机端搭配套餐 编辑:程序博客网 时间:2024/05/24 04:35

http://acm.hdu.edu.cn/showproblem.php?pid=3626

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
51 85 76 92 83 1107 57 53 19 91 47 42 39 54 53 40
 

Sample Output
Case 1:6 96 9-1 -16 95 7Case 2:9 99 94 5-1 -14 59 53 4-1 -19 94 5
 

Source
2010 Asia Regional Tianjin Site —— Online Contest

————————————————————————————————————
题目的意思好理解,不好再描述,我也就不再赘述了。如果没看懂,可以留言。
下面是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;}




原创粉丝点击