9.17

· · 个人记录

20260917

一、比赛基本情况

#### 赛后题解
首先D的移动范围比K广,棋盘有限,所以肯定可以抓上K,然后D追上K最好的办法是:先沿着对角线与K到达同行或同列,然后一步一步紧逼
#### 补题代码
```cpp line-numbers
#include <bits/stdc++.h>
using namespace std ;
long long t , n , rk , ck , rd , cd ;
int main() {
    ios::sync_with_stdio(0) ;
    cin.tie(0) ;
    cout.tie(0) ;
    for(cin >> t ; t -- ;) {
        cin >> n >> rk >> ck >> rd >> cd ;
        long long dx = 0 , dy = 0 ;
        if(ck < cd) {
            dx = cd ;
        } else if(ck > cd) {
            dx = n - cd ;
        }
        if(rk < rd) {
            dy = rd ;
        } else if(rk > rd) {
            dy = n - rd ;
        }
        cout << max(dx , dy) << "\n" ;
    }
    return 0 ;
}

复杂度

  1. 就是求s的一个子序列,每一个字符都唯一,并且字典序要最大
  2. 利用反悔贪心,枚举s的每个字符,设当前字符串为ans,如果s_i已经在ans里出现过了就跳过,否则如果ans还有字符,并且最靠右的字符字典序比s_i小,并且ans的最右边的字符后边还有,那么就把ans的最右边删了并取消标记,最后接上s_i并标记
  3. 可以用栈

    补题代码

    #include <bits/stdc++.h>
    using namespace std ;
    long long t , last[30] , n ;
    string s ;
    bool vis[30] ;
    void work() {
     string ans = "" ;
     stack <char> stk ;
     for(int i = 0 ; i < n ; i ++) {
         char c = s[i] ;
         if(vis[c - 'a' + 1]) {
             continue ;
         }
         while(stk.size() && stk.top() < c && last[stk.top() - 'a' + 1] > i) {
             vis[stk.top() - 'a' + 1] = 0 ;
             stk.pop() ;
         }
         vis[c - 'a' + 1] = 1 ;
         stk.push(c) ;
     }
     while(stk.size()) {
         ans = stk.top() + ans ;
         stk.pop() ;
     }
     cout << ans << "\n" ;
    }
    int main() {
     for(cin >> t ; t -- ; ) {
         cin >> s ;
         n = s.size() ;
         fill(last + 1 , last + 30 , 0) ;
         for(int i = 0 ; i < n ; i ++) {
             last[s[i] - 'a' + 1] = i ;
         }
         fill(vis + 1 , vis + 30 , 0) ;
         work() ;
     }
     return 0 ;
    }

    复杂度