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;
}
相关推荐
焜昱错眩..11 分钟前
代码随想录算法训练营第三十九天|62.不同路径 63.不同路径ll
算法
焦耳加热4 小时前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
wan5555cn4 小时前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
u6064 小时前
常用排序算法核心知识点梳理
算法·排序
蒋星熠6 小时前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
小欣加油7 小时前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
3Cloudream7 小时前
LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
算法·leetcode·字符串·双指针·滑动窗口·哈希表·中等
王璐WL7 小时前
【c++】c++第一课:命名空间
数据结构·c++·算法
空白到白8 小时前
机器学习-聚类
人工智能·算法·机器学习·聚类
索迪迈科技8 小时前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法