zoukankan      html  css  js  c++  java
  • SDNU 1142.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
    

    Source

    思路:傻乎乎的用结构体重载,做了很多次, 烦了很多大佬,就是wa。然后才知道,结构体重载不会自动排序,和优先队列结合在一起用才会有用(泪流成海)。然后改为在外部定义一个结构体的sort排序,就a了。   T^T
    #include<bits/stdc++.h>
    using namespace std;
    
    #define ll long long
    #define eps 1e-9
    #define pi acos(-1)
    
    const int inf = 0x3f3f3f3f;
    const int mod = 1000000007;
    const int maxn = 1000 + 8;
    
    int n;
    
    struct node
    {
        int x, y;
    }a[maxn], b[maxn];
    
    bool cmp(node x, node y)
    {
        if(x.x != y.x)
            return x.x < y.x;
        return x.y < y.y;
    }
    
    int main()
    {
        std::ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int ca = 0;
        while(cin >> n && n)
        {
            for(int i = 0; i < n; i++)
            {
                cin >> a[i].x >> a[i].y;
                b[i].x = a[i].x;
                b[i].y = a[i].y;
            }
            sort(b, b + n, cmp);
            cout << "Case " << ++ca << ":" << '
    ';
            for(int i = 0; i < n; i++)
            {
                int j = 0;
                while((a[i].x >= b[j].x || a[i].y >= b[j].y) && j < n)
                    j++;
                if(j < n)
                    cout << b[j].x << " " << b[j].y << '
    ';
                else
                    cout << "-1 -1" << '
    ';
            }
            cout << '
    ';
        }
        return 0;
    }
    /*
    6
    8 9
    10 10
    2 3
    5 4
    6 7
    2 1
    */
  • 相关阅读:
    Spring注释事务失效及解决办法
    在使用springMVC时,我使用了@Service这样的注解,发现使用注解@Transactional声明的事务不起作用
    如何在Oracle中复制表结构和表数据
    Tomcat Deployment failure ,locked one or more files
    java中对List中对象排序实现
    HDU 5002 Tree LCT 区间更新
    2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树
    Codeforces Round #323 (Div. 1) B. Once Again... 暴力
    Codeforces Round #323 (Div. 2) C. GCD Table 暴力
    hdu 5497 Inversion 树状数组 逆序对,单点修改
  • 原文地址:https://www.cnblogs.com/RootVount/p/11478291.html
Copyright © 2011-2022 走看看