acwing第74场周赛

比赛链接

1.第一题的思路没什么好说的, 需要注意的一点就是我们在比较的时候不要忘记第一个和最后一个的差值的比较就行了。

AC代码

#include <iostream>
using namespace std ;
int main()
{
    int mi = 1010 ;
    int n ;
    int a[1010] ; 
    cin >> n ; 
    for(int i = 0 ; i < n ; i ++ )
    {
        cin >> a[i] ; 
        if(i) mi = min(mi , abs(a[i] - a[i - 1])) ; 
    }
    mi = min(mi , abs(a[0] - a[n - 1])) ; 
    cout << mi << endl ; 
    return 0 ;
}

2.这个题是一个搜索的模板的题目,我们可以使用宽搜也可以使用深搜,这个跟模板题的唯一区别就是这个题是三维的,和二维的模板题区别不大, 只需要将偏移量数组修改一下就行了。
AC代码(深搜版本)

#include <iostream>
using namespace std ;
const int N = 15 ; 
int n , m , tt ;
char g[N][N][N] ;
int x , y ; 
int dx[6] = {0 , -1 , 0 , 1 , 0 , 0 } , dy[6] = {0,  0 , 1 ,  0 , -1 , 0 } , dz[6] = {1 , 0 , 0 , 0 , 0 , -1 } ; 
int dfs(int k , int x , int y )
{
    g[k][x][y] = '#' ;
    int res = 1 ; 
    for(int i = 0 ; i < 6 ; i ++ ) 
    {
        int k1 = k + dz[i] , b = x + dx[i] , c = y +dy[i] ; 
        if(k1 < 1 || k1 > tt || b < 1 || b > n || c < 1 || c > m || g[k1][b][c] == '#') continue ;
        res += dfs(k1 , b , c ) ; 
    }
    return res ;  
}

int main()
{
    cin >> tt >> n >> m ;
    for(int i = 1;  i <= tt ; i ++ )
    {
        for(int j = 1 ; j <= n ; j ++ )
        {
            for(int t = 1 ; t <= m ; t ++ )
            {
                cin >> g[i][j][t] ; 
            }
        }
    }
    cin >> x >> y ; 
    cout << dfs(1 , x , y ) << endl ;
    return 0 ;
}

宽搜版本

#include <iostream>
#define x first 
#define y second 
#include <cstring> 
using namespace std ;
const int N = 20 ; 
typedef pair<int,pair<int,int> > PIP  ; 
int n , m , tt ;
char g[N][N][N] ;  
bool st[N][N][N] ;
PIP qq[ N*N*N ] ;
int hh = 0 , tt1 = -1 ; 
int x , y ;
int cnt ; // cnt 用来存储的是我们的能够到达的点数,
int dx[6] = {0 , -1 , 0 , 1 , 0 , 0 } ; 
int dy[6] = {0,  0 , 1 ,  0 , -1 , 0 } ;
int dz[6] = {1 , 0 , 0 , 0 , 0 , -1 } ; 
void bfs()
{
    qq[++tt1] = { 1 , {x , y } } ;
    st[1][x][y] = true ; 
    cnt ++ ; 
    while(hh <= tt1 ) 
    {
        auto t = qq[hh++] ;
        for(int i = 0 ; i < 6 ;  i ++ )
        {
            int a = t.x + dz[i], b = t.y.x + dx[i], c = t.y.y + dy[i] ;
            if(a >= 1 && a <= tt && b >= 1 && b <= n && c >= 1 && c <= m &&g[a][b][c] == '.' ) 
            {
                if(st[a][b][c]) continue ; 
                else 
                {
                    cnt ++ ; 
                    st[a][b][c] = true ; 
                }
                qq[++tt1] = {a , {b , c } } ; 
            }
        }
    }
}

int main()
{
    cin >> tt >> n >> m ;
    for(int i = 1;  i <= tt ; i ++ )
    {
        for(int j = 1 ; j <= n ; j ++ )
        {
            for(int t = 1 ; t <= m ; t ++ )
            {
                cin >> g[i][j][t] ; 
            }
        }
    }
    cin >> x >> y ; 
    bfs() ;
    
    cout << cnt << endl ; 
    
    return 0 ;
}

3.思路, 这个题我们使用的是树状数组+离散化去做的;
我们线性枚举 a j a_j aj 这个数, 在枚举的过程中我们如何去寻找 a [ 1....... j − 1 ] a[1 ....... j - 1] a[1.......j1] 之间的比 a [ j ] a[j] a[j]大的数呢?同时如何寻找 a [ j + 1 , n ] a[j+1, n] a[j+1,n] 这个区间内的比 a [ j ] a[j] a[j] 小的数呢 ?
不要怕,树状数组+离散化可以解决这两个问题:
首先先介绍离散化, 我们如何去寻找 a [ j ] a[j] a[j]所对应的离散化后的值呢?对于原数组w 我们开辟另一个数组q, 把原数组赋值给q数组之后, 对q组进行一个升序排序, 假如我们在原数组遍历到 a [ j ] a[j] a[j]这个数, 那么在q数组中一定唯一映射到一个下标 v 这个v就是我们所说的离散化后的值。 (因为原数组中中的任意两个数都是不同的) , 所以我们可以发现原数组中 a [ j ] a[j] a[j] 小的数一共有 v - 1 个。
注意一下, 这个树状数组维护的是每一个离散化后的值在当前出现的个数 也就是说,当我们遍历到 a [ j ] a[j] a[j]并且a[j] 所对应的v还没被更新到树状数组中, 那么 t r [ v ] tr[v] tr[v] 就代表的是 a [ 1....... j − 1 ] a[1 ....... j - 1] a[1.......j1] 之中,比 a [ j ] a[j] a[j] 小的数的个数(设为tt), 但在 a [ 1....... j − 1 ] a[1 ....... j - 1] a[1.......j1] 这个区间我们要的是比 a [ j ] a[j] a[j] 大的数的个数, 所以是 ( j − 1 − t t ) (j - 1 - tt ) (j1tt) (代码中呈现的是(i - tt )因为i 是从0开始的,但原理是一样的) 好! 第一个问题我们已经解决了。同理第二个问题的答案 就是 : (v - 1 - tt ) 因为 (v - 1 ) 代表的是原数组中的所有的比 a [ j ] a[j] a[j] 小的数的个数, tt 则代表的是在 a [ 1....... j − 1 ] a[1 ....... j - 1] a[1.......j1] 之中,比 a [ j ] a[j] a[j] 小的数的个数 , 所以在 a [ j + 1 , n ] a[j + 1 , n] a[j+1,n] 当中比 a [ j ] a[j] a[j]小的数的个数就是 (v - 1 - tt ) , 根据乘法原理以 a [ j ] a[j] a[j] 为中间值的个数就是
( i − t t ) ∗ ( v − 1 − t t ) (i - tt ) * (v - 1 - tt ) (itt)(v1tt) , 现在所有的问题都解决了, 直接上代码:
AC代码:

#include <algorithm>
#include <iostream>
using namespace std ;
typedef long long LL ; 
const int N = 1e6 + 10 ;
int tr[N] , w[N] , q[N] ; // 树状数组, 输入值, 离散化后的数组
int n ; 
int lowbit(int x )
{
    return  x & -x ; 
}

void update(int x , int v ) 
{
    for(int i = x ; i <= n ; i += lowbit(i) ) 
    {
        tr[i] += v ; 
    }
}

int query(int x ) 
{
    int res = 0 ; 
    for(int i = x  ; i >= 1 ; i -= lowbit(i) ) 
    {
        res += tr[i] ; 
    }
    return res ; 
}

int main()
{
    cin >> n ; 
    for(int i = 0 ; i < n ; i ++ ) 
    {
        cin >> w[i] ;
        q[i] = w[i] ; 
    }
    LL res = 0 ; 
    sort(q , q + n ) ; 
    for(int i = 0 ; i < n ; i ++ )
    {
        int v = lower_bound(q , q  + n , w[i] ) - q + 1 ; // 找到离散化后的坐标值
        int tt = query(v) ; //  找到前面比v小的数的个数。
        res += (LL)(i - tt) * (v - 1 - tt) ; // (i - tt ) 是比其大的数的个数。
        update(v , 1) ;
    }
    cout << res << endl ; 
    return 0 ; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值