方法一:二分做法
预处理每个点所能到达的最远距离,存到vector里边,然后二分处理结果
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, q, a[N], len, maxn;
vector<int> vet[20];
string st;
signed main(){
cin >> n;
cin >> st;
len = st.size();
for(int i = 0; i < st.size(); i++){
if(st[i] == '1') a[i + 1] = 1;
else a[i + 1] = -1;
}
for(int i = 1; i <= len; i++){
maxn = 1;
bool flag = true;
int seg = -1;
for(int j = i; j <= i + 9 && j <= len; j++){
if(maxn + a[j] <= 0) break;
maxn += a[j];
if(maxn == 1){
seg = j - i;
vet[seg].push_back(i);//任何符合条件的都要放到vet中,不能只放最大的
}
}
}
cin >> q;
while(q --){
int l, r;
cin >> l >> r;
bool flag = false;
for(int i = 9; i >= 0; i --){
vector<int>::iterator x = lower_bound(vet[i].begin(), vet[i].end(), l);
if(x != vet[i].end() && *x + i<= r){
maxn = i;
maxn = maxn + 1;
flag = true;
break;
}
}
if(flag)
cout << maxn << endl;
else cout <<"0" << endl;
}
return 0;
}
#include<bits/stdc++.h>
#define int long long int
using namespace std;
const int mod = 1e9 + 7;
int t, a, b, n;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> t;
while(t -- ){
cin >> n;
int ans = 1;
while(n -- ){
cin >> a >> b;
//要想互质,那么这个质数只能加在一边,或者一边都不加
//不能两边都加,这样两边形成的数就不能不互质了
ans = (ans * (2 * b + 1)) % mod;
}
cout << ans << endl;
}
return 0;
}
#include<bits/stdc++.h>
#define int long long int
using namespace std;
const int N = 2e6 + 10;
int n, m, a[N], dp[N][2], sum;
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];
}
//每个位置只有两种情况,选或者不选;
//选的话,可以从不选的,i - k 转移过来
//不选的话,就是从i - k的两种情况中,选择更大的一个
for(int i = m + 1; i <= n; i++){
dp[i][0] = max(dp[i - m][0], dp[i - m][1]);
dp[i][1] = dp[i - m][0] + a[i - m] + a[i];
}
//分块,分了m个小区间分别dp
for(int i = n - m + 1; i <= n; i++) sum += max(dp[i][0], dp[i][1]);
cout << sum << endl;
return 0;
}