进阶技巧与思想(第一,二章)
尺取法
尺取法1 尺取法2 尺取法3
题目
#include <bits/stdc++.h>
int main(){
int n, data[101] = { 0 };
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &data[i]) ;
int s;
scanf("%d", &s);
int l = 0, r = 0, sum = data[0] ; // 初始化尺子
while (l < n && r < n){ // 循环条件为什么是这样?
if(sum >= s) {
if(sum == s) printf("sum[%d, %d]=%d\n", l, r, s) ;
sum -= data[l++] ; // 移动左端点
if(l > r) sum += data[++r] ; // 推动右端点
}else {
sum += data[++r]; // 移动右端点
}
}
return 0;
}
前缀和&&差分
题目
前缀和:
解决区间个数,和等问题。
一维:
#include<bits/stdc++.h>
using namespace std ;
const int N = 1e5 + 10 ;
int n , m , a ;
int b[N] ;
int main(){
cin >> n ;
cin >> a ;
b[1] = a ;
for( int i = 2 ; i <= n ; i++ ){
cin >> a ;
b[i] = b[i-1] + a ;
}
cin >> m ;
for( int i = 1 ; i <= m ; i++ ){
int x , y ;
cin >> x >> y ;
cout << b[y] - b[x-1] << endl ;
}
return 0 ;
}
二维:
#include<bits/stdc++.h>
using namespace std ;
const int N = 130 ;
int n , g[N][N] , ans = 0 ;
int main(){
cin >> n ;
for( int i = 1 ; i <= n ; i++ ){
for( int j = 1 ; j <= n ; j++ ){
cin >> g[i][j] ;
g[i][j] += g[i-1][j] + g[i][j-1] - g[i-1][j-1] ;// 预处理
}
}
for( int i = 1 ; i <= n ; i++ ){
for( int j = 1 ; j <= n ; j++ ){
for( int k = i ; k <= n ; k++ ){
for( int l = j ; l <= n ; l++ ){
ans = max(ans , g[k][l] - g[i-1][l] - g[k][j-1] + g[i-1][j-1]) ;
}
}
}
}
cout << ans << endl ;
return 0 ;
}
差分
二维:
题目
#include<bits/stdc++.h>
using namespace std ;
const int N = 1010 ;
int n , m , q ;
int a[N][N] , b[N][N] ;
void insert(int x1 , int y1 , int x2 , int y2 , int c){
b[x1][y1] += c ;
b[x1][y2+1] -= c ;
b[x2+1][y1] -= c ;
b[x2+1][y2+1] += c ;
}//在左上角(x1 , y1)与右下角(x2 , y2)的范围中所以的数加c。
void P(){ // 输出每次操作后的差分数组。
for( int i = 1 ; i <= n ; i++ ){
for( int j = 1 ; j <= m ; j++ ){
printf("% 2d ", b[i][j]) ;
}
cout << endl ;
}
system("pause") ;
}
int main(){
cin >> n >> m >> q ;
for( int i = 1 ; i <= n ; i++ ){
for( int j = 1 ; j <= m ; j++ ){
cin >> a[i][j] ;
}
}
for(int i = 1 ; i <= n ; i++ ){
for( int j = 1 ; j <= m ; j++ ){
insert(i , j , i , j , a[i][j]) ;//初始差分数组。
//P() ;
}
}
while(q--){
int x1 , y1 , x2 , y2 , c ;
cin >> x1 >> y1 >> x2 >> y2 >> c ;
insert(x1 , y1 , x2 , y2 , c) ;
}
for( int i = 1 ; i <= n ; i++ ){
for( int j = 1 ; j <= m ; j++ ){
b[i][j] += b[i-1][j] + b[i][j-1] - b[i-1][j-1] ;//求前缀和。
cout << b[i][j] << " " ;
}
cout << endl ;
}
return 0 ;
}
树上差分
单调队列
题目
#include<bits/stdc++.h>
using namespace std ;
const int N = 1e6 + 10 ;
int n , k , cnt , a[N] ;
int q[N] , h , t ;
void init(){
h = 1 , t = 0 ;
memset(q , 0 , sizeof(q)) ;
}
bool pd(int x , int i){
if(x == 1) return a[i] >= a[q[t]] ;//Max
return a[i] <= a[q[t]] ;//Min
}
void Get(int x){
init() ;
for( int i = 1 ; i <= n ; i++ ){
while(h <= t && pd(x , i)) t-- ;
q[++t] = i ;
cnt++ ;
if(q[h] < i-k+1) h++ ;
if(i >= k) cout << a[q[h]] << " " ;
}
cout << endl ;
}
int main(){
cin >> n >> k ;
for( int i = 1 ; i <= n ; i++ ){
cin >> a[i] ;
}
Get(0) ;
Get(1) ;
return 0 ;
}
单调栈
题目
#include<bits/stdc++.h>
using namespace std ;
#define PII pair<int , int>
stack<PII> s ;
const int N = 3e6 + 10 ;
int n , a[N] , ans[N] ;
int main(){
scanf("%d", &n) ;
for( int i = 1 ; i <= n ; i++ ){
scanf("%d", &a[i]) ;
while(s.size() != 0 && s.top().first < a[i]){
ans[s.top().second] = i ;
s.pop() ;
}
s.push({a[i] , i}) ;
}
for( int i = 1 ; i <= n ; i++ ) printf("%d ", ans[i]) ;
return 0 ;
}
离散化
主要用于计数。
过程
1.排序
2.去重
3.二分
优点:能去重,省空间;
缺点:查询较慢
题目
vector版:
#include<bits/stdc++.h>
using namespace std ;
#define PII pair<int , int>
const int N = 3e5 + 10 ;
int n , m , a[N] , s[N] ;
vector<int> alls ;
vector<PII> add , xw ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(0) ;
cout.tie(0) ;
cin >> n >> m ;
for( int i = 1 ; i <= n ; i++ ){
int x , c ;
cin >> x >> c ;
alls.push_back(x) ;
add.push_back({x , c}) ;
}
for( int i = 1 ; i <= m ; i++ ){
int l , r ;
cin >> l >> r ;
xw.push_back({l , r}) ;
alls.push_back(l) ;
alls.push_back(r) ;
}
sort(alls.begin() , alls.end()) ;
alls.erase(unique(alls.begin() , alls.end()) , alls.end()) ;
for(auto item : add){
int x = lower_bound(alls.begin() , alls.end() , item.first) - alls.begin() + 1 ;
a[x] += item.second ;
}
for( int i = 1 ; i <= alls.size() ; i++ ){
s[i] = s[i-1] + a[i] ;
}
for(auto item : xw){
int l = lower_bound(alls.begin() , alls.end() , item.first) - alls.begin() + 1 ;
int r = lower_bound(alls.begin() , alls.end() , item.second) - alls.begin() + 1 ;
cout << s[r] - s[l-1] << endl ;
}
return 0 ;
}
//https://www.acwing.com/problem/content/description/804/
静态数组:
#include<bits/stdc++.h>
using namespace std ;
#define PII pair<int , int>
const int N = 1e5 + 10 ;
int n , m , a[N] , b[N] , ans ;
vector<int> alls ;
vector<PII> xw ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(0) ;
cout.tie(0) ;
cin >> n ;
for( int i = 1 ; i <= n ; i++ ){
int l , r ;
cin >> l >> r ;
xw.push_back({l , r}) ;
alls.push_back(l) ;
alls.push_back(r) ;
}
sort(alls.begin() , alls.end()) ;
alls.erase(unique(alls.begin() , alls.end()) , alls.end()) ;
for(auto item : xw){
int l = lower_bound(alls.begin() , alls.end() , item.first) - alls.begin() + 1 ;
int r = lower_bound(alls.begin() , alls.end() , item.second) - alls.begin() + 1 ;
b[l]++ ;
b[r]-- ;
}
for( int i = 1 ; i <= alls.size() ; i++ ){
b[i] += b[i-1] ;
if(b[i]) ans += alls[i+1]-alls[i] ;
}
cout << ans << endl ;
return 0 ;
}
//https://www.luogu.com.cn/problem/P1496
下标映射
优点:查询快
缺点:不能去重。
1.相同的数的取值不同
#include<bits/stdc++.h>
using namespace std ;
const int N = 1100 ;
int n ;
int Rank[N] ;
struct node{
int val , id ;
}a[N];
bool cmp(node x , node y){
return x.val < y.val ;
}
int main(){
system("start https://zhuanlan.zhihu.com/p/630089550") ;
cin >> n ;
for(int i = 1 ; i <= n ; i++){
cin >> a[i].val ;
a[i].id = i ;
}
sort(a+1 , a+1+n , cmp) ;
for(int i = 1 ; i <= n ; i++) Rank[a[i].id] = i ;
for(int i = 1 ; i <= n ; i++){
cout << Rank[i] << " " ;
}
puts("") ;
return 0 ;
}
1.相同的数的取值相同
#include<bits/stdc++.h>
using namespace std ;
const int N = 1100 ;
int n ;
int Rank[N] ;
struct node{
int val , id ;
}a[N];
bool cmp(node x , node y){
return x.val < y.val ;
}
int main(){
system("start https://zhuanlan.zhihu.com/p/630089550") ;
cin >> n ;
for(int i = 1 ; i <= n ; i++){
cin >> a[i].val ;
a[i].id = i ;
}
sort(a+1 , a+1+n , cmp) ;
int cnt = 0 ;
for(int i = 1 ; i <= n ; i++){
if(a[i].val != a[i-1].val) cnt++ ;
Rank[a[i].id] = cnt ;
}
for(int i = 1 ; i <= n ; i++){
cout << Rank[i] << " " ;
}
puts("") ;
return 0 ;
}