首页 > 其他分享 >E - Yutori

E - Yutori

时间:2023-02-04 19:23:12浏览次数:28  
标签:tired int 位置 rest else -- Yutori

E - Yutori

https://atcoder.jp/contests/abc161/tasks/abc161_e

 

思路

https://blog.csdn.net/weixin_45750972/article/details/105325285

两种贪心策略:

从左边尽早完成任务;

从右边尽早完成任务。

 

对于第i次的工作天,如果两种策略在序列中位置相同, 则此位置即为目标答案;

此位置, 就是第i天的最早工作位置,也是第i天的最晚工作位置,即此为唯一的一个位置;

这意味着,对于所有策略,此位置不变。

 

 

 

 

Code

https://atcoder.jp/contests/abc161/submissions/38592576

int n, k, c;
string s;
 
int main()
{
    cin >> n >> k >> c;
    cin >> s;
 
    int slen = s.size();
    vector<int> lflag(k, -1);
    vector<int> rflag(k, -1);
 
    // greedy working from left
    bool tired;
    int rest;
    int workdays;
    
    tired = false;
    rest = 0;
    workdays = 0;
    
    for(int i=0; i<n; i++){
        char one = s[i];
        
//        cout << "day i="<< i << " got char=" << one << endl;
//        cout << "before action, my property, tired=" << tired << " rest="<<rest << " workdays=" << workdays <<endl;
        
        if (tired == false){
            if (one == 'o') {
                // work one day
//                cout << "not tired, work one day." << endl;
                
                lflag[workdays] = i;
                
                workdays++;
                if (workdays >= k){
                    break;
                }
                
                tired = true;
                rest = 0;
            } else if (one == 'x'){
                // can not work
                rest++;
            }
        } else {
            // worked before today, feel tired, need rest
            /*
            rest c days
            rest:
            0 --> 1
            1 --> 2
            ...
            c-1 --> c
             */
//            cout << "feel tired, need rest" << endl;
//            cout << "before rest = " << rest << endl;
            // let him rest
            rest++;
//            cout << "after rest = " << rest << endl;
//            
//            cout << "c days = " << c << endl;
            if (rest >= c){
                tired = false;
            }
        }
        
//        cout << "after action, my property, tired=" << tired << " rest="<<rest << " workdays=" << workdays <<endl;
//        cout << endl;
    }
 
//    for(int i=0; i<n; i++){
//        cout << lflag[i];
//    }
//    cout << endl;
 
    // greedy working from right
    tired = false;
    rest = 0;
    workdays = 0;
    
    for(int i=0; i<n; i++){
        char one = s[slen-i-1];
 
        if (tired == false){
            if (one == 'o') {
                // work one day
                rflag[k-workdays-1] = slen-i-1;
                
                workdays++;
                if (workdays >= k){
                    break;
                }
 
                tired = true;
                rest = 0;
            } else if (one == 'x'){
                // can not work
                rest++;
            }
        } else {
            // worked before today
            /*
            rest c days
            rest:
            0 --> 1
            1 --> 2
            ...
            c-1 --> c
             */
            // let him rest
            rest++;
 
            if (rest >= c){
                tired = false;
            }
        }
    }
 
//    for(int i=0; i<n; i++){
//        cout << rflag[i];
//    }
//    cout << endl;
 
    for(int i=0; i<k; i++){
        if (lflag[i] == rflag[i]) {
            cout << lflag[i]+1 << endl;
        }
    }
 
    return 0;
}
 

 

标签:tired,int,位置,rest,else,--,Yutori
From: https://www.cnblogs.com/lightsong/p/17092184.html

相关文章