SNStudio 防爆代码头V2.0.0 AC题解

//本题解可以AC,且防爆代码头公益开源
//本人因CSP26老头乐评测机爆栈事件特开发此防爆代码头(非文件)

/*
    from SNStudio
    开发历时八天,坚决抵制sbCCbydF老头乐评测机!
    抵制资本主义CCF CSP-S 2026!
    全世界无产主义OI者,联合起来!
*/

防爆代码头V2 + 可AC题解:

/*      V2.0.0      */
#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <cstdio>
#include <cstring>
#include <algorithm>

namespace FastIO {
    #define BUF_SIZE 1000000
    bool IOerror = 0;
    inline char nc() {
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if(p1 == pend) {
            p1 = buf;
            pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if(pend == p1) { IOerror = 1; return -1; }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }
    template <class T> inline bool read(T &x) {
        bool sign = 0; char ch = nc(); x = 0;
        for(; blank(ch); ch = nc());
        if(IOerror) return false;
        if(ch == '-') sign = 1, ch = nc();
        for(; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
        if(sign) x = -x;
        return true;
    }
    inline bool read(double &x) {
        bool sign = 0; char ch = nc(); x = 0;
        for(; blank(ch); ch = nc());
        if(IOerror) return false;
        if(ch == '-') sign = 1, ch = nc();
        for(; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
        if(ch == '.') {
            double tmp = 1, y = 0; ch = nc();
            for(; ch >= '0' && ch <= '9'; ch = nc()) y = y * 10 + ch - '0', tmp *= 10;
            x += y / tmp;
        }
        if(sign) x = -x;
        return true;
    }
    template <class T, class... Args> inline bool read(T &x, Args&... args) {
        return read(x) && read(args...);
    }
    inline bool read(char* s) {
        char ch = nc();
        for(; blank(ch); ch = nc());
        if(IOerror) return false;
        int i = 0;
        for(; !blank(ch) && ch != -1; ch = nc()) s[i++] = ch;
        s[i] = '\0';
        return true;
    }
}
using FastIO::read;

const int MAX_MEMORY = 64 * 1024 * 1024; 
char memory_pool[MAX_MEMORY];
size_t memory_ptr = 0;

void* operator new(size_t size) {
    if (memory_ptr + size > MAX_MEMORY) {
        return malloc(size);
    }
    void* p = memory_pool + memory_ptr;
    memory_ptr += size;
    return p;
}
void operator delete(void* p) noexcept {}
void* operator new[](size_t size) {
    if (memory_ptr + size > MAX_MEMORY) {
        return malloc(size);
    }
    void* p = memory_pool + memory_ptr;
    memory_ptr += size;
    return p;
}
void operator delete[](void* p) noexcept {}

#define il inline
#define ll long long
#define ull unsigned long long

//理论可行,实践开始

struct Patient {
    char id[12];
    int age;
    int index;
};

bool comparePatients(const Patient& a, const Patient& b) {
    bool a_is_elderly = (a.age >= 60);
    bool b_is_elderly = (b.age >= 60);

    if (a_is_elderly != b_is_elderly) {
        return a_is_elderly > b_is_elderly;
    }

    if (a_is_elderly && b_is_elderly) {
        if (a.age != b.age) {
            return a.age > b.age;
        }
        return a.index < b.index;
    }

    return a.index < b.index;
}

int main() {
    int n;
    if (!read(n)) return 0;

    Patient* patients = new Patient[n];

    for (int i = 0; i < n; ++i) {
        read(patients[i].id);
        read(patients[i].age);
        patients[i].index = i;
    }

    std::sort(patients, patients + n, comparePatients);

    for (int i = 0; i < n; ++i) {
        printf("%s\n", patients[i].id);
    }

    return 0;
}

防爆代码头V1.0.0,暂不可AC:

/*      V1.0.0 因OJ限制而RTE    */
#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <cstdio>
#include <cstring>
#include <algorithm>

namespace FastIO {
    #define BUF_SIZE 1000000
    bool IOerror = 0;
    inline char nc() {
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if(p1 == pend) {
            p1 = buf;
            pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if(pend == p1) { IOerror = 1; return -1; }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }
    template <class T> inline bool read(T &x) {
        bool sign = 0; char ch = nc(); x = 0;
        for(; blank(ch); ch = nc());
        if(IOerror) return false;
        if(ch == '-') sign = 1, ch = nc();
        for(; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
        if(sign) x = -x;
        return true;
    }
    inline bool read(double &x) {
        bool sign = 0; char ch = nc(); x = 0;
        for(; blank(ch); ch = nc());
        if(IOerror) return false;
        if(ch == '-') sign = 1, ch = nc();
        for(; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
        if(ch == '.') {
            double tmp = 1, y = 0; ch = nc();
            for(; ch >= '0' && ch <= '9'; ch = nc()) y = y * 10 + ch - '0', tmp *= 10;
            x += y / tmp;
        }
        if(sign) x = -x;
        return true;
    }
    template <class T, class... Args> inline bool read(T &x, Args&... args) {
        return read(x) && read(args...);
    }
    
    // 快速读取字符串
    inline bool read(char* s) {
        char ch = nc();
        for(; blank(ch); ch = nc());
        if(IOerror) return false;
        int i = 0;
        for(; !blank(ch) && ch != -1; ch = nc()) s[i++] = ch;
        s[i] = '\0';
        return true;
    }
}
using FastIO::read;

const int MAX_MEMORY = 500 * 1024 * 1024;
char memory_pool[MAX_MEMORY];
size_t memory_ptr = 0;

void* operator new(size_t size) {
    void* p = memory_pool + memory_ptr;
    memory_ptr += size;
    return p;
}
void operator delete(void* p) noexcept {}
void* operator new[](size_t size) {
    void* p = memory_pool + memory_ptr;
    memory_ptr += size;
    return p;
}
void operator delete[](void* p) noexcept {}

#define reg register
#define il inline
#define ll long long
#define ull unsigned long long

0 条评论

目前还没有评论...

信息

ID
185
时间
ms
内存
MiB
难度
2
标签
递交数
1134
已通过
293
上传者