zoukankan      html  css  js  c++  java
  • bjfu1250 模拟

    这题貌似是蓝桥杯的一题改了个题面。

    就是模拟啦,应该有比我的更简洁的方法。

    我的方法是把所有的人(蚂蚁)按位置排完序以后从左往右看,每次有一个向左走的,就会把最左边的t出,这个变成向右中,同时,从左端到此位置的人都会相遇一遍,处理一下就好了。

    不废话了,直接上代码

    /*
     * Author    : ben
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <functional>
    #include <numeric>
    #include <cctype>
    using namespace std;
    
    typedef struct People {
        bool sad;
        int x;
        People (int xx = 0, bool s = false) {
            sad = s;
            x = xx;
        }
    } People;
    
    bool inline operator<(const People& p1, const People& p2) {
        return abs(p1.x) < abs(p2.x);
    }
    
    const int MAXP = 55;
    People pe[MAXP];
    int N;
    
    void run() {
        People *start = pe;
        People *end = &pe[N - 1];
        while (start < end && (*start).x < 0) {
            start++;
        }
        while (start < end) {
            People *p = start;
            while (p <= end && (*p).x > 0) {
                p++;
            }
            if (p > end) {
                break;
            }
            (*p).x = 0 - (*p).x;
            while (--p >= start) {
                (*p).sad = (*(p + 1)).sad = (*p).sad or (*(p + 1)).sad;
            }
            start++;
        }
    }
    
    int main() {
        int x;
        while (scanf("%d", &N) == 1) {
            memset(pe, 0, sizeof(pe));
            pe[0].sad = true;
            for (int i = 0; i < N; i++) {
                scanf("%d", &pe[i].x);
            }
            sort(pe, pe + N);
            run();
            int ans = 0;
            for (int i = 0; i < N; i++) {
                ans += pe[i].sad;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    第二阶段冲刺--第五天
    git托管代码随笔--运用ssh传输,不用每次提交频繁输入github账号密码
    项目冲刺--第十天
    项目冲刺--第九天
    随堂练习--用例图练习
    项目冲刺--第四天
    第五次个人作业: 案例分析--微软必应词典客户端
    Code.R团队展示
    Android 自定义AlertDialog
    Ubuntu打开系统监视器查看进程&资源等信息
  • 原文地址:https://www.cnblogs.com/moonbay/p/4257228.html
Copyright © 2011-2022 走看看