window.cnblogsConfig = {//可以放多张照片,应该是在每一个博文上面的图片,如果是多张的话,那么就随机换的。 homeTopImg: [ "https://cdn.luogu.com.cn/upload/image_hosting/xkzro04i.png" ], }

hdu 1425 sort 对拍及命名空间训练

P.S. 以下程序仅适用于 Windows 操作系统,暂不支持 Linux。

待验证正解程序 sort.cpp

#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;

int a[1000003], n;

namespace Bubble // 冒泡排序
{
    void Main()
    {
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n - i; ++j)
                if(a[j] < a[j + 1])
                    swap(a[j], a[j + 1]);
    }
}

namespace Merge // 归并排序
{
    void Main()
    { stable_sort(a + 1, a + n + 1, greater<int>()); }
}

//namespace Quick // 快速排序
//{
//  void Main()
//  { sort(a + 1, a + n + 1, greater<int>()); }
//}

int main()
{
    freopen("sort.in", "r", stdin);
    freopen("sort.out", "w", stdout);
    int m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    if(n <= 1e4) // 分点作答
        Bubble::Main();
    else
        Merge::Main();
//  else
//      Quick::Main();
    for(int i = 1; i <= m; ++i)
        printf("%d ", a[i]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

数据生成程序 gen.cpp

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <unordered_set>
using namespace std;
using UL = unsigned long;

unordered_set<int> S;

UL ulrand()
{
    return (
            (((UL)rand() << 24) & 0xFF000000UL) |
            (((UL)rand() << 12) & 0x00FFF000UL) |
            (((UL)rand()) & 0x00000FFFUL));
}

int main()
{
    freopen("sort.in", "wb", stdout);
    srand(time(NULL));
    int n = ulrand() % 999999 + 1, m = ulrand() % 999999 + 1;
    printf("%d %d\n", n, m);
    while(S.size() < n)
    {
        int x = ulrand() % 1000001 - 500000;
        S.insert(x);
    }
    while(!S.empty())
    {
        printf("%d ", *S.cbegin());
        S.erase(S.cbegin());
    }
    fclose(stdout);
    return 0;
}

暴力正确程序 baoli.cpp

#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;

int a[1000003];

int main()
{
    freopen("sort.in", "r", stdin);
    freopen("sort.ans", "w", stdout);
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1, greater<int>()); // 真·暴力
    for(int i = 1; i <= m; ++i)
        printf("%d ", a[i]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

对拍验证程序 pai.cpp

#include <cstdio>
#include <cstdlib>
#include <conio.h>
using namespace std;

int cnt = 0;

inline void output()
{
    freopen("sort.in", "r", stdin);
    printf("# %d\n", cnt);
    int n;
    scanf("%d", &n);
    printf("%d\n", n);
    for(int x; n--; )
    {
        scanf("%d", &x);
        printf("%d ", x);
    }
    printf("\n\n");
    fclose(stdin);
}

int main()
{
    // 编译
    system("g++.exe -O2 -std=c++14 -static -Wl,--stack=2147483647 gen.cpp -o gen.exe");
    system("g++.exe -O2 -std=c++14 -static -Wl,--stack=2147483647 baoli.cpp -o baoli.exe");
    system("g++.exe -O2 -std=c++14 -static -Wl,--stack=2147483647 sort.cpp -o sort.exe");
   // 开始对拍
    while(++cnt)
    {
        system("gen.exe");
        system("baoli.exe");
        system("sort.exe");
        if(system("fc /b sort.out sort.ans")) // 执行二进制对比,加速!(注意空格,换行等)
        {
            system("cls");
            output(); // 输出 hack 数据
            freopen("CON", "r", stdin);
            getch(); //systen("pause") 貌似出了亿些问题
        }
    }
    return 0;
}
posted @ 2023-10-10 22:24  TigerTanWQY  阅读(7)  评论(0)    收藏  举报  来源