C.洛谷 P7286
题意
有一个数组 ,下标为 到 。请求出对于任意的 , 的最大值是多少,
。
思路
对于题目给定的式子,可以分开处理。
- :枚举 统计 的最大值;
- :将 按从大到小的顺序排序,再遍历每个 ,应为排序后具有单调性,所以当前 一定为最小值。对于每个 排序后顺序有变化,所以要提前记录编号,在枚举过程中记录前缀最大值即可,每次只需将前缀最大编号加上当前编号即可。注意区分编号和下标的区别。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n, ans, mx;
struct P{
int v, id;
}a[N];
bool cmp(const P &a, const P &b){
return a.v > b.v;
}
signed main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i].v;
a[i].id = i;
ans = max(ans, a[i].v * i);
}
sort(a + 1, a + 1 + n, cmp);
for(int i = 1; i <= n; i++){
ans = max(ans, a[i].v * (a[i].id + mx));
mx = max(mx, a[i].id);
}
cout << ans;
return 0;
}
F.洛谷 P3939
题意
有 只编号为 的兔子,第 只颜色为 。给定 次询问,有以下两种操作:
- 询问在区间 里有多少只颜色为 的兔子;
- 和 两只兔子交换了位置。
思路
对于每种颜色,用 vector 记录每次出现的为位置即可。
每次查询时,使用二分找出在区间 在颜色 中的第一次和最后一次的值,相减即为答案。
每次修改时,如果 则忽略操作;否则,就让 对应的值 ,让 对应的值 。这样就能保证每个颜色的出现下标保持单调性。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, m, a[N];
vector<int> g[N];
int get(int c, int x){
return lower_bound(g[c].begin(), g[c].end(), x) - g[c].begin();
}
int gett(int c, int x){
return upper_bound(g[c].begin(), g[c].end(), x) - g[c].begin();
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
g[a[i]].push_back(i);
}
for(int i = 1; i <= m; i++){
int op, l, r, c, x;
cin >> op;
if(op == 1){
cin >> l >> r >> c;
cout << gett(c, r) - get(c, l) << "\n";
}
else{
cin >> x;
if(a[x] == a[x + 1]) continue;
l = get(a[x], x);
r = get(a[x + 1], x + 1);
g[a[x]][l]++;
g[a[x + 1]][r]--;
swap(a[x], a[x + 1]);
}
}
return 0;
}
I.洛谷 P9350
题意
有 个点第 个点 有坐标 和影响 。要从中选择 个点,使所有点都被考虑(选择)。如果第 个点被选择或考虑,所有满足以下条件的点 也将被考虑:
- 点 和点 满足 。
请求出 的最小值。
思路
首先按照坐标从小到大排序保证单调性。
接着用单调栈维护需要选择的点。考虑式子 可化为 ,now 表示当前下标,top 表示栈顶下标。对于满足式子的所有栈内元素,都弹出。最后判断当前节点是否受栈顶元素影响,如果不被则将当前元素插入栈。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 10;
int n;
struct P{
int x, e;
}a[N];
bool cmp(const P &a, const P &b){
return a.x < b.x;
}
stack<int> st;
signed main(){
cin >> n;
for(int i = 1; i <= n; i++){
auto &[x, e] = a[i];
cin >> x >> e;
}
sort(a + 1, a + 1 + n, cmp);
for(int i = 1; i <= n; i++){
auto [x, e] = a[i];
while(!st.empty() && e - a[st.top()].e >= x - a[st.top()].x){
st.pop();
}
if(st.empty() || a[st.top()].e - e < x - a[st.top()].x){
st.push(i);
}
}
cout << st.size();
return 0;
}J.洛谷 P9816
题意
给定多项式 。定义 ,。
给定模数 。有 次询问,每次给出 ,查询 的值。
思路
考虑倍增。
对于每个 ,可以先预先处理出往后走一步的值,接着使用倍增处理走 步的值。
在每次询问时,先计算一边 ,使其值在 范围内。最后使用位枚举计算出跳完后的结果。
代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
ll m, q, mod, cnt;
ll f[N][30], a[25], b[25];
ll ksm(ll a, ll b){
ll sum = 1;
while(b){
if(b & 1){
(sum *= a) %= mod;
}
a = a * a % mod;
b /= 2;
}
return sum % mod;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> m >> q >> mod;
for(ll i = 1; i <= m; i++){
cin >> a[i]; a[i] %= mod;
cin >> b[i];
}
for(ll i = 0; i <= 100000; i++){
for(ll j = 1; j <= m; j++){
(f[i][0] += a[j] * ksm(i, b[j]) % mod) %= mod;
}
}
for(ll j = 1; j <= 25; j++){
for(ll u = 0; u <= 100000; u++){
f[u][j] = f[f[u][j - 1]][j - 1];
}
}
while(q--){
ll x, y;
cin >> x >> y;
x %= mod;
ll sum = 0;
for(ll i = 1; i <= m; i++){
sum += (a[i] * ksm(x, b[i])) % mod;
sum %= mod;
}
y--;
for(ll i = 0; i <= 25; i++){
if((y >> i) & 1){
sum = f[sum][i];
}
}
cout << sum << "\n";
}
return 0;
}