【树状数组】小奇遐想

转载:https://blog.csdn.net/winter2121/article/details/81255827#commentBox

5727: 小奇遐想

时间限制: 1 Sec  内存限制: 128 MB
提交: 225  解决: 35
[提交] [状态] [讨论版] [命题人:admin]

 

题目描述

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

 

 

输入

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

 

 

输出

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

 

样例输入

 
  1. 5

  2. 1 5 3 2 4

 

样例输出

0

 

提示

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

【分析】

用树状数组求正序对的方法,求数组pre2,pre2[i] 表示 i 处结尾的长度为2的升序子序列种数(即正序对数量)

同理,求数组late2,  late2[i] 表示 i 处为开头向后,长度为2的升序子序列数量(正序对数量)

根据late2数组可继续利用树状数组求得数组late3

late3[i] 表示  i 处为开头向后,长度为3的升序子序列数量

再来一次,倒叙循环,求得数组big ,  big[i] 表示 i 处后面的大于 a[i] 的数的个数

 

好,现在,枚举选中的四个数的第二个数,for(int i=1;i<=n;i++)

那么当把a[i]作为第二个数时对总答案的贡献是 1243型种数 = 12**型种数 - 1234型种数(其中*表示大于2的数)

12**型种数 = pre2[i] * C(big[i], 2)

1234型种数 = pre2[i] * late3[i]

【代码】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int mod=16777216;
const int MAX=2e5+5;
 
 
ll c[202020];
void add(int k,ll num,int top)
{
    for(;k<=top;k+=k&-k)
        c[k]=(c[k]+num)%mod;
}
ll read(int k)
{
    ll res=0;
    for(;k;k-=k&-k)
        res=(res+c[k])%mod;
    return res;
}
 
int a[MAX],n;
ll late3[MAX];
ll pre2[MAX];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    memset(c,0,sizeof(c));
    for(int i=1;i<=n;i++)
    {
        pre2[i]=read(a[i]-1); //i结尾长为2的升序列种数
        pre2[i]%=mod;
        add(a[i],1,n);
    }
    memset(c,0,sizeof(c));
    for(int i=n;i>=1;i--)
    {
        late3[i]=read(n)-read(a[i]); //i开头长为2的升序种数
        late3[i]%=mod;
        add(a[i],1,n);
    }
    memset(c,0,sizeof(c));
    for(int i=n;i>=1;i--)
    {
        ll temp=late3[i];
        late3[i]=read(n)-read(a[i]); //i开头长为3的升序种数
        late3[i]%=mod;
        add(a[i],temp,n);
    }
    // ans = 12**-1234   (*>2)
    memset(c,0,sizeof(c));
    ll ans=0;
    for(int i=n;i>=1;i--)
    {
        ll big=read(n)-read(a[i]);
        add(a[i],1,n);
        ll late=big*(big-1)/2%mod; //从后边选逆序对
        ll res=pre2[i]*late%mod; //12**
        res=( (res-pre2[i]*late3[i]%mod)%mod + mod)%mod;// -1234
        ans=(ans+res)%mod;
    }
    cout<<ans<<endl;
}

 

这个问题可以使用广度优先搜索算法(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、付费专栏及课程。

余额充值