zoukankan      html  css  js  c++  java
  • 【算法学习笔记】66. 模拟法 数组链表 报数优化 SJTU OJ 4010 谁最有耐心

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    struct person
    {
        int data;
        int id;
    };
    
    int l[1001],r[1001];//存储编号为i的左边的编号和右边的编号
    int data[1001];//存储编号为i的耐心值
    int n;//总人数
    
    //初始化
    void init(){
        cin>>n;
        for (int i = 1; i <= n; ++i)
        {
            cin>>data[i];
            l[i] = i-1;
            r[i] = i+1;
        }
        //围城一个圆
        l[1] = n;
        r[n] = 1;
    } 
    
    //删除
    inline void deletePerson(int id){
        r[l[id]] = r[id];
        l[r[id]] = l[id];
    }
    //进行报数游戏的模拟
    int game(){    
        int rem = n;//还剩的人数
        int curId = 1;//当前报数的人
        bool toRight = true; //初始向右
    
        while(rem>1){
            //优化
            int  min = 9999999;//用来找到当前最小的耐心值
            for (int i = 1; i <= n; ++i)
            {
                if(data[i]>0 and data[i]<min)
                    min = data[i];
            }
            //找到之后所有的人耐心值等量减少 减少了循环次数 类比猴子题的%N
            for (int i = 1; i <= n; ++i)
            {
                data[i] -= (min-1);
            }
    
            data[curId]--;
            if(data[curId]==0){
                deletePerson(curId);
                toRight = !toRight;//反向报数
                rem--;
            }
            
            if(toRight)
                curId = r[curId];
            else
                curId = l[curId];
            
        }
        return curId;
    }
    
    
    int main(int argc, char const *argv[])
    {
        init();
        cout<<game()<<endl;
        return 0;
    }
  • 相关阅读:
    左偏树
    output html
    jsp web.xml
    mysql link db
    beamline
    jsp embend java into html / mix scriptlets and HTML
    StringTokenizer
    TreeSet
    centOS 显示中文
    request and response
  • 原文地址:https://www.cnblogs.com/yuchenlin/p/sjtu_oj_4010.html
Copyright © 2011-2022 走看看