【GXNU 0402 周赛】

知识点: 双指针

在这里插入图片描述
在这里插入图片描述

目前我还不能证明它为什么能得到答案, 但是差不多也可以基本验证出来, 虽然不严谨.

1双指针移动后, 总是在我们要找的正确位置的下一个位置, 所以最后在计算的时候可以合理进行调整
2双指针写的时候基本上都有一个附加条件, j < n or j > 0
例子(奇怪的蛋糕问题): while(flag && cnt >= 1 && stk[cnt] != b) cnt -- , k ++ ;
从 1 2 中找 5, 如果不加设置, 那么 cnt 将会一直减

#include <iostream>
#include <cstring>
#include <algorithm>
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y <<","<< #z << "=" << z << endl
#define debv(v) for(auto x:v) cout<<x<<" "


using namespace std;

const int N = 1e5 + 10;
typedef long long ll;

int n;
ll a[N];
ll s[N];
ll ans;

int main()
{
    cin >> n;
    
    for(int i = 1; i <= n; i ++ ) cin >> a[i], s[i] = s[i - 1] + a[i], ans = max(a[i] * a[i], ans);
    
    int k = 0, j = 0;
    for(int i = 1; i <= n; i ++ )
    {
        j = i, k = i;
        j -- , k ++;
       // deb3(j, i, k);
        // 往右
        while(k <= n && a[k] > a[i]) k ++ ; // k 总是在3 5 4 的 4 这
        while(j >= 1 && a[j] >= a[i]) j -- ;
        
        // 往左
        ll res = a[i] * (s[k - 1] - s[j]);
        ans = max(res, ans);
    }
    
    cout << ans << endl;
    return 0;
}

知识点: 栈, 双指针

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
映射


    int temp[7] = {1, 2, 3, 4, 5, 6, 7};
    
    for(int i = 0; i < 14; i ++ ) stk[++ tt] = temp[(id ++ ) % 7], cout << stk[tt] <<' ';

在这里插入图片描述

#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y <<","<< #z << "=" << z << endl
#define debv(v) for(auto x:v) cout<<x<<" "


using namespace std;
typedef pair<int, int> PII;

const int N = 1e5 + 10, M = 1e6 + 10;

int stk[M];
int n, id;
int tt = -1;
PII q[N];

int check(int a, int b)
{
    int flag = 0;
    for(int i = 0; i < tt; i ++ )
        if(stk[i] == b)
            return 1;
    return 0;
}

int main()
{
    cin >> n;
    
    for(int i = 0; i < n; i ++ ) cin >> q[i].x >> q[i].y;
    sort(q, q + n);
    
    int last = 1;
    int temp[7] = {1, 2, 3, 4, 5, 6, 7};
    for(int i = 0; i < n; i ++ )
    {
        int a = q[i].x, b = q[i].y;
        
        // 做蛋糕 -> 取蛋糕
        for(int i = last; i <= a; i ++ )
            stk[++ tt] = temp[(id ++ ) % 7];
            
        // 取蛋糕, 能不能取
        int flag = check(a, b);
        if(flag) 
        {
            int k = 0;
            while(tt >= 0 && stk[tt] != b) tt --, k ++; //  1 2 3
            tt -- , k ++ ;
            
            cout << k << endl;
            last = a + 2;
        }
        else
        {
            cout << 0 << endl;
            last = a + 1;
        }
    }
   
}

知识点: 滑动窗口 + 队列

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y <<","<< #z << "=" << z << endl
#define debv(v) for(auto x:v) cout<<x<<" "


using namespace std;
typedef pair<int, int> PII;

const int N = 1e5 + 10;

int cnt[N];
bool st[N];
PII q[N];
int n, d, k;

int main()
{
    cin >> n >> d >> k;
    
    for(int i = 0; i < n; i ++ ) cin >> q[i].x >> q[i].y;
    sort(q, q + n);
    // for(int i = 0; i < n; i ++ ) deb2(q[i].x, q[i].y);
    for(int i = 0, j = 0; i < n; i ++ ) // 滑动
    {
        int t = q[i].y;
        cnt[t] ++ ;
        
        while(q[i].x - q[j].x >= d) cnt[q[j ++].y] -- ;
        
        if(cnt[t] >= k) st[t] = true;
    }
    
    for(int i = 0; i <= 1e5; i ++ )
        if(st[i])
            cout << i << endl;
}

例题AcWing 154. 滑动窗口

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n;
struct node
{
    int i, pos, d;
    
};
struct compare {
    bool operator()(node & a, node & b) {
        return a.pos != b.pos ? a.pos > b.pos : a.d < b.d;
    }
};

priority_queue<node, vector<node>, compare> q ;
int main()
{
    int n,k;
    cin>>n;
    for(int i = 1; i <= n; i ++ )
    {
        int x,y;
        cin >> x >> y;
        q.push({i, x, y});
    }
    int res = 0;
    while(q.size() != 1)
    {
        res ++;
        auto x = q.top();
        q.pop();
        if(res & 1)
            q.push({x.i, x.d + x.pos, x.d});
    }
    if(res & 1)
    cout << q.top().i << " " << q.top().pos << '\n';
    else cout << q.top().i << " " << q.top().pos + q.top().d << '\n';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值