知识点: 双指针
目前我还不能证明它为什么能得到答案, 但是差不多也可以基本验证出来, 虽然不严谨.
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;
}
#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';
}