10881 - Piotr‘s Ants (UVA)

题目链接:Online Judge

根据刘汝佳的解法的思路,我的代码如下:

cpp 复制代码
#include <cstdio>
#include <algorithm>
#include <string>
const int maxn = 10001;

struct ant{
    int id;
    int loc;
    int dir;
};

bool cmp(const ant &a, const ant &b){
    return a.loc < b.loc;
}

bool cmp1(const ant &a, const ant &b){
    return a.id < b.id;
}

ant a[maxn], b[maxn];
int order[maxn];
int N, L, T, n;
char ch;
std::string direction[3] = {"L", "Turning", "R"};

int main(){
    scanf("%d", &N);
    for(int kase = 1; kase <= N; ++kase){
        printf("Case #%d:\n", kase);
        scanf("%d %d %d", &L, &T, &n);
        for(int i = 0; i < n; ++i){
            scanf("%d %c", &a[i].loc, &ch);
            a[i].id = i;
            a[i].dir = ch == 'L' ? -1 : 1;
        }
        std::sort(a, a + n, cmp);
        for(int i = 0; i < n; ++i){
            order[i] = a[i].id;
            b[i].loc = a[i].loc + a[i].dir * T;
            b[i].dir = a[i].dir;
        }
        std::sort(b, b + n, cmp);
        for(int i = 0; i < n; ++i){
            b[i].id = order[i];
            if(i != n - 1 && b[i].loc == b[i + 1].loc){
                b[i].dir = 0;
                b[i + 1].dir = 0;
            }
        }
        std::sort(b, b + n, cmp1);
        for(int i = 0; i < n; ++i){
            if(b[i].loc < 0 || b[i].loc > L){
                printf("Fell off\n");
            } else{
                printf("%d %s\n", b[i].loc, direction[b[i].dir + 1].c_str());
            }
        }
        printf("\n");
    }
    return 0;
}

我起先的代码如下,样例答案是对的,但提交时显示超时:

cpp 复制代码
#include <cstdio>
#include <deque>
#include <set>
#include <algorithm>

struct ant{
    int id;
    int loc;
    int direction;
    bool turnFlag = false;
};
int N, L, T, n, loc;
char ch;
std::deque<ant> de;
std::set<int> fellOff;

bool cmp(const ant &a, const ant &b){
    return a.loc < b.loc;
}

bool cmp1(const ant &a, const ant &b){
    return a.id < b.id;
}

int main(){
    scanf("%d", &N);
    for(int kase = 1; kase <= N; ++kase){
        scanf("%d %d %d", &L, &T, &n);
        de.clear();
        de.resize(n);
        for(int i = 0; i < n; ++i){
            scanf("%d %c", &de[i].loc, &ch);
            de[i].id = i;
            de[i].direction = ch == 'L' ? -1 : 1;
        }
        sort(de.begin(), de.end(), cmp);
        for(int i = 0; i < T; ++i){
            if(de[0].loc == 0 && de[0].direction == -1){
                fellOff.insert(de[0].id);
                de.pop_front();
            }
            if(de.back().loc == L && de.back().direction == 1){
                fellOff.insert(de.back().id);
                de.pop_back();
            }
            for(int j = 0; j < de.size(); ++j){
                if(de[j].direction == -1){
                    de[j].loc--;
                } else{
                    if(j == de.size() - 1){
                        de[j].loc++;
                    } else if(de[j + 1].loc > de[j].loc + 2 || de[j + 1].direction == 1){
                        de[j].loc++;
                    } else{
                        if(de[j + 1].loc == de[j].loc + 2){
                            de[j].loc++;
                            de[j + 1].loc--;
                        }
                        de[j].direction = -1;
                        de[j + 1].direction = 1;
                        j++;
                    }
                }
            }
        }
        for(int i = 0; i < de.size(); ++i){
            if((i != de.size() - 1 && de[i + 1].loc == de[i].loc) || (i != 0 && de[i - 1].loc == de[i].loc)){
                de[i].turnFlag = true;
            }
        }
        sort(de.begin(), de.end(), cmp1);
        int cur = 0;
        printf("Case #%d:\n", kase);
        for(int i = 0; i < n; ++i){
            if(fellOff.find(i) != fellOff.end()){
                printf("Fell off\n");
            } else{
                printf("%d ", de[cur].loc);
                if(de[cur].turnFlag){
                    printf("Turning\n");
                } else{
                    printf("%c\n", de[cur].direction == -1 ? 'L' : 'R');
                }
                cur++;
            }
        }
        printf("\n");
        fellOff.clear();
    }
    return 0;
}
相关推荐
Yungoal几秒前
C++基础项目结构
数据结构·c++·算法
扶摇接北海1766 分钟前
洛谷:B4477 [语言月赛 202601] 考场安排
数据结构·c++·算法
爱丽_12 分钟前
AQS 的 `state`:volatile + CAS 如何撑起原子性与可见性
java·前端·算法
2301_7887705513 分钟前
OJ模拟5
数据结构·算法
羊小猪~~16 分钟前
算法/力扣--字符串经典题目
c++·考研·算法·leetcode·职场和发展·哈希算法
攒了一袋星辰16 分钟前
10万级用户数据日更与定向推送系统的可靠性设计
java·数据库·算法
nap-joker18 分钟前
PIPE4:快速PPI预测器,用于综合的跨物种和跨物种相互作用组
算法·多模态生物医学数据分析·蛋白质互作网络
磊 子26 分钟前
类和对象—>析构+拷贝+运算符重载
开发语言·c++·算法
人道领域28 分钟前
LeetCode【刷题日记】:数组篇(1)含原理讲解
算法·leetcode·职场和发展
RTC老炮32 分钟前
webrtc弱网-BBRv2算法原理
网络·算法·webrtc