跳到正文

个人记录

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

TigerTanWQY约 6 分钟
原文
文章信息与版本

文章信息

发布
2023年10月10日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 6 分钟

版本

  1. v1当前

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

待验证正解程序 sort.cpp

C++
#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

C++
#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

C++
#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

C++
#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;
}
原文
存档评论1 条抓取于