袁小奔08-01文章

· · 个人记录

比赛传送门

\tiny 邀请码: wp81
题目 分数
T1 \color{green}{100}
T2 \color{green}{100}
T3 \color{red}{0}
T4 \color{green}{100}
T5 \color{red}{10}
T6
T7 \color{red}{0}
T8
T9

分数: 310分。

T3: B3667 求区间所有后缀最大值的位置

源代码:

#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e6 + 5;

int n, m, a[N];

deque<int> q;

void Cout(){
    int len = q.size();
    for(int i = 1; i <= len; ++i){
        cout << q.front() << ' ';
        q.push_back(q.front());
        q.pop_front();
    }
    cout << '\n';
    return ;
} 
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    for(int i = 1; i <= n; ++i){
        while(!q.empty() && a[q.back()] <= a[i]) q.pop_back()/*, Cout()*/;
        while(!q.empty() && q.front() + m - 1 < i) q.pop_front()/*, Cout()*/;
        q.push_back(i);
        //Cout();
        if(i >= m) cout << q.size() << '\n';
        //cout << "->\n";
    }
    return 0;
}

错误原因:

while(!q.empty() && a[q.back()] <= a[i]) q.pop_back();
while(!q.empty() && q.front() + m - 1 < i) q.pop_front();

正确思路:
应该先把前面的给砍掉,再把后面的接进来,因为如果先把后面接进来的话,那么后缀最大值可能就会比原本得到的值要不一样。
还要注意一点:1 \le x_i \le 2^{64}

AC Code:

#include<bits/stdc++.h>
#define int unsigned long long

using namespace std;

const int N = 1e6 + 5;

int n, m, a[N];

deque<int> q;

void Cout(){
    int len = q.size();
    for(int i = 1; i <= len; ++i){
        cout << q.front() << ' ';
        q.push_back(q.front());
        q.pop_front();
    }
    cout << '\n';
    return ;
} 
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    for(int i = 1; i <= n; ++i){
        while(!q.empty() && q.front() + m - 1 < i) q.pop_front()/*, Cout()*/;
        while(!q.empty() && a[q.back()] <= a[i]) q.pop_back()/*, Cout()*/;
        q.push_back(i);
        //Cout();
        if(i >= m) cout << q.size() << '\n';
        //cout << "->\n";
    }
    return 0;
}

T5: P3467 [POI 2008] PLA-Postering

源代码:

#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 5e5 + 5;

int n, a[N], cnt;

deque<int> q;
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1, x; i <= n; ++i){
        cin >> x >> a[i];
    }
    for(int i = 1; i <= n; ++i){
        while(!q.empty() && a[q.front()] >= a[i]){
            if(a[q.front()] == a[i]) ++cnt;
            q.pop_front();
        }
        q.push_back(i);
    }
    cout << n - cnt;
    return 0;
}

错误原因: 离谱
正确思路:
这道题老师上课讲过了,我不多说了。

AC Code:

#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 5e5 + 5;

int n, a[N], ans;

stack<int> st;
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1, x; i <= n; ++i){
        cin >> x >> a[i];
    }
    for(int i = 1; i <= n; ++i){
        while(!st.empty() && a[st.top()] > a[i]){
            st.pop();
        }
        if(st.empty()) ++ans;
        else if(a[i] != a[st.top()]) ++ans;
        st.push(i);
    }
    cout << ans;
    return 0;
}

T6: P2564 [SCOI2009] 生日礼物

正确思路:
这题跟逛画展那道题唯一的区别就是这里还需要自己把题目给的条件捋成一条数轴,别的没有区别。上代码。

AC Code:

#include<bits/stdc++.h>
#define int long long
#define p first
#define id second

using namespace std;

const int N = 1e6 + 5;

int n, k, t, x, num[N], cnt, total, minn = INT_MAX;

vector<pair<int, int> > e;

deque<int> q;
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> k;
    e.push_back(make_pair(0, 0));
    for(int i = 1; i <= k; ++i){
        cin >> t;
        while(t--){
            cin >> x;
            e.push_back(make_pair(x, i));
        }
    }
    sort(e.begin(), e.end());
    for(int i = 1; i <= n; ++i){
        q.push_back(i);
        int id = e[i].id;
        if(num[id] == 0) ++cnt;
        ++num[id];
        while(!q.empty() && cnt >= k){
            int j = q.front();
            q.pop_front();
            int id = e[j].id;
            --num[id];
            if(num[id] == 0){
                --cnt;
                minn = min(minn, e[i].p - e[j].p);
            }
        }
    }
    cout << minn;
    return 0;
}

T7: P1823 [COI 2007] Patrik 音乐会的等待

大概说一下思路,这道题目他需要先乘除一个node,然后如果判断站点比ai要小的话,那么就把站点排除并且 Answer加等于站顶并且弹出站顶,然后如果再判断站点和ai相等,那么就把node给改变别的没了。

AC Code:

#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 5e5 + 5;

int n, a[N], ans;

struct P{
    int x, y;
}node;

stack<P> st; 
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    for(int i = 1; i <= n; ++i){
        node = {i, 1};
        while(!st.empty() && a[st.top().x] <= a[i]){
            ans += st.top().y;
            if(a[st.top().x] == a[i]){
                node = {i, st.top().y + node.y};
            }
            st.pop();
        }
        if(!st.empty()) ++ans;
        st.push(node);
    }
    cout << ans;
    return 0;
}

乾坤未定,你我皆是黑马······