zoukankan      html  css  js  c++  java
  • hdu 1872 稳定排序

    题目连接

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

    稳定排序

    Description

    大家都知道,快速排序是不稳定的排序方法。
    如果对于数组中出现的任意a[i],a[j](i<j),其中a[i]==a[j],在进行排序以后a[i]一定出现在a[j]之前,则认为该排序是稳定的。


    某高校招生办得到一份成绩列表,上面记录了考生名字和考生成绩。并且对其使用了某排序算法按成绩进行递减排序。现在请你判断一下该排序算法是否正确,如果正确的话,则判断该排序算法是否为稳定的。

    Input

    本题目包含多组输入,请处理到文件结束。
    对于每组数据,第一行有一个正整数$N(0<N<300)$,代表成绩列表中的考生数目。
    接下来有N行,每一行有一个字符串代表考生名字(长度不超过50,仅包含'a'~'z'),和一个整数代表考生分数(小于500)。其中名字和成绩用一个空格隔开。
    再接下来又有N行,是上述列表经过某排序算法以后生成的一个序列。格式同上。

    Output

    对于每组数据,如果算法是正确并且稳定的,就在一行里面输出"Right"。如果算法是正确的但不是稳定的,就在一行里面输出"Not Stable",并且在下面输出正确稳定排序的列表,格式同输入。如果该算法是错误的,就在一行里面输出"Error",并且在下面输出正确稳定排序的列表,格式同输入。

    注意,本题目不考虑该排序算法是错误的,但结果是正确的这样的意外情况。

    Sample Input

    3
    aa 10
    bb 10
    cc 20
    cc 20
    bb 10
    aa 10
    3
    aa 10
    bb 10
    cc 20
    cc 20
    aa 10
    bb 10
    3
    aa 10
    bb 10
    cc 20
    aa 10
    bb 10
    cc 20

    Sample Output

    Not Stable
    cc 20
    aa 10
    bb 10
    Right
    Error
    cc 20
    aa 10

    bb 10

    简单题,c++ A了 g++ WA了 %>_<%。。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<string>
     8 #include<map>
     9 using std::cin;
    10 using std::cout;
    11 using std::endl;
    12 using std::find;
    13 using std::sort;
    14 using std::map;
    15 using std::pair;
    16 using std::vector;
    17 using std::string;
    18 #define pb(e) push_back(e)
    19 #define sz(c) (int)(c).size()
    20 #define mp(a, b) make_pair(a, b)
    21 #define all(c) (c).begin(), (c).end()
    22 #define iter(c) decltype((c).begin())
    23 #define cls(arr,val) memset(arr,val,sizeof(arr))
    24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    27 const int Max_N = 310;
    28 typedef unsigned long long ull;
    29 struct Node {
    30     string name;
    31     int score, id;
    32     bool operator <(const Node a) const {
    33         if (a.score != score) return a.score < score;
    34         return id < a.id;
    35     }
    36 }A[Max_N], B[Max_N];
    37 int N;
    38 void solve() {
    39     int f = 0;
    40     sort(A, A + N);
    41     rep(i, N) {
    42         if (A[i].name != B[i].name) {
    43             if (B[i].score == A[i].score) f = 1;
    44             else f = -1;
    45         }
    46     }
    47     if (!f) { puts("Right"); return; }
    48     if (1 == f)  puts("Not Stable");
    49     else puts("Error");
    50     rep(i, N) cout << A[i].name << " " << A[i].score << endl;
    51 }
    52 int main() {
    53 #ifdef LOCAL
    54     freopen("in.txt", "r", stdin);
    55     freopen("out.txt", "w+", stdout);
    56 #endif
    57     std::ios::sync_with_stdio(false);
    58     while (cin >> N) {
    59         rep(i, N) { cin >> A[i].name >> A[i].score; A[i].id = i; }
    60         rep(i, N) cin >> B[i].name >> B[i].score;
    61         solve();
    62     }
    63      return 0;
    64 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    zlib压缩一个文件为gzip格式
    最短路径算法Dijkstra
    用MonoDevelop开发Linux游戏
    Flask入门1HelloWorld
    Windows+Linux打造和谐的开发环境
    WinCE 6.0下的编译选项
    使用cvrtbin.exe转换bin文件到nb0
    美的电磁炉(SK2106)加热断断续续问题的快速修复(原创)
    解决XP下双网卡不能同时用
    USB驱动程序打包(转)
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4603108.html
Copyright © 2011-2022 走看看