[树状数组]小奇遐想

题目描述
撷来一缕清风飘渺
方知今日书信未到
窗外三月天霁垂柳新长枝条
风中鸟啼犹带欢笑
——《清风醉梦》
小奇望着青天中的悠悠白云,开始了无限的遐想,在它的视野中,恰好有n朵高度不同的白云排成一排,他想从左到右选出四朵白云a,b,c,d,使得h_a<h_b<h_d<h_c,即看起来像是彩虹的形状!它想知道有多少种方案数。
 

 

输入
第一行包括1个整数n。
第二行包括n个整数,第i个正数表示h_i,保证这n个整数是n的一个全排列。
 

 

输出
输出一个整数表示答案。(mod 16777216)

 

样例输入
5
1 5 3 2 4

 

样例输出
0

 

提示

对于10%的数据n<=600;对于40%的数据n<=5000;
对于100%的数据n<=200000。

思路:求选4多云朵其高度满足“1243”的选择方案数。可以逆向考虑,求出“12_ _”的总方案数(其中_ _都大于1和2,但_ _内部相对大小任意),再减去“1234”的方案数,结果就是“1243”的方案数。

"12_ _"的方案数求法:对每一个a[i],将a[1~i-1]中比a[i]小的数的个数求出来(树状数组),记录在l[i]里,则a[i+1~n]中比a[i]小的数的个数就为(a[i]-1)-l[i],记录在r[i]里,则a[i+1~n]中比a[i]大的数的个数也可以求出来,即n-i-r[i];则“12_ _”的方案数就是sum{l[i]*C(n-i-r[i],2)}(i=1~n);

"1234"的方案数求法:对每一个a[i],将a[1~i-1]中比a[i]小的数的l数组求和(树状数组),即sum{l[j]}(j<i&&a[j]<a[i])(记为tot[i]),则“1234”的方案数就是sum{tot[i]*(n-i-r[i])}(i=1~n);

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define lowbit(x) x&(-x)
#define mod 16777216
using namespace std;

int a[200010],c[200010];
int l[200010],r[200010];
int n;

void add(int x,int val){
  for(int i=x;i<=n;i+=lowbit(i)) c[i]+=val;
}

int getsum(int x){
  int ret=0;
  for(int i=x;i>0;i-=lowbit(i)) ret+=c[i];
  return ret;
}

long long sum1(){
  long long ret=0;
  for(int i=1;i<=n;i++){
    ret=(ret+l[i]*(n-i-r[i])*(n-i-r[i]-1)/2%mod)%mod;
  }
  return ret;
}

long long sum2(){
  long long ret=0;
  memset(c,0,sizeof(c));
  for(int i=1;i<=n;i++){
    ret=(ret+getsum(a[i])*(n-i-r[i])%mod)%mod;
    add(a[i],l[i]);
  }
  return ret;
}

int main(){
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    scanf("%d",&a[i]);
    l[i]=getsum(a[i]);
    r[i]=(a[i]-1)-l[i];
    add(a[i],1);
  }
  long long ans=(sum1()-sum2()+mod)%mod;
  printf("%lld\n",ans);
}

树状数组可以维护在i左边的比a[i]小的数的个数

转载于:https://www.cnblogs.com/lllxq/p/9379489.html

这个问题可以使用广度优先搜索算法(BFS)来解决。具体步骤如下: 1. 创建一个队列,初始时将起点加入队列。 2. 创建一个二维数组dist,用于记录每个位置到起点的最短距离。初始时将起点的距离设为0,其余位置的距离设为无穷大。 3. 进入循环,每次从队列取出一个位置,遍历其上下左右四个方向的相邻位置,如果相邻位置可达且未被访问过,则将其加入队列,并更新其距离为当前位置的距离+1。 4. 直到队列为空或者找到终点时退出循环。 5. 如果找到了终点,则输出起点到终点的最短距离,并可以通过dist数组反向查找最短路线。 根据题目给出的迷宫结构,可以使用以下代码实现: ```c++ #include <iostream> #include <queue> #include <cstring> using namespace std; const int N = 5; int maze[N][N] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; int dist[N][N]; bool visited[N][N]; struct Point { int x, y; Point(int _x, int _y) : x(_x), y(_y) {}; }; int bfs(Point start, Point end) { queue<Point> q; q.push(start); memset(visited, false, sizeof(visited)); visited[start.x][start.y] = true; memset(dist, 0x3f, sizeof(dist)); dist[start.x][start.y] = 0; int dx[] = { -1, 0, 1, 0 }; int dy[] = { 0, 1, 0, -1 }; while (!q.empty()) { Point cur = q.front(); q.pop(); if (cur.x == end.x && cur.y == end.y) { return dist[cur.x][cur.y]; } for (int i = 0; i < 4; i++) { int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (nx < 0 || nx >= N || ny < 0 || ny >= N || maze[nx][ny] == 1 || visited[nx][ny]) { continue; } q.push(Point(nx, ny)); visited[nx][ny] = true; dist[nx][ny] = dist[cur.x][cur.y] + 1; } } return -1; } int main() { Point start(0, 0); Point end(N - 1, N - 1); int shortestPath = bfs(start, end); if (shortestPath == -1) { cout << "No path exists!" << endl; } else { cout << "Shortest path length: " << shortestPath << endl; // 可以通过dist数组反向查找最短路线 } return 0; } ``` 输出结果为: ``` Shortest path length: 8 ``` 即起点到终点的最短距离为8。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值