[队内测试Day10.24Final]逆序对+表达式计算+贪心+图论+数论?

T1

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<map>
#define L(w) w << 1
#define R(w) w << 1|1
const int MAXN = 10000 + 50;
using namespace std;
int n;
char s1[MAXN],s2[MAXN];
map <char,int> id;
int tot;
int A[MAXN];
int L[MAXN << 2],R[MAXN << 2];
int sum[MAXN << 2];
void build(int w,int l,int r){
    L[w] = l;
    R[w] = r;
    if(l == r){
        return;
    }
    int mid = l + r >> 1;
    build(L(w),l,mid);
    build(R(w),mid + 1,r);
}
void update(int w){
    sum[w] = sum[L(w)] + sum[R(w)];
}
void change(int w,int pos){
    if(L[w] == pos && R[w] == pos){
        sum[w] ++;
        return;
    }
    int mid = L[w] + R[w] >> 1;
    if(pos <= mid)change(L(w),pos);
    else change(R(w),pos);
    update(w);
}
int ans;
int ask(int w,int l,int r){
    if(l > r)return 0;
    if(L[w] == l && R[w] == r){
        return sum[w];
    }
    int mid = L[w] + R[w] >> 1;
    int t = 0;
    if(r <= mid)t = ask(L(w),l,r);
    else if(l > mid)t = ask(R(w),l,r);
    else t += ask(L(w),l,mid) + ask(R(w),mid + 1,r);
}
int main(){
    freopen("order.in","r",stdin);
    freopen("order.out","w",stdout);
    scanf("%d",&n);
    scanf("%s",s1 + 1);
    scanf("%s",s2 + 1);
    for(int i = 1;i <= n;i ++){
        id[s1[i]] = ++tot;
    }
    build(1,1,n + 1);
    for(int i = 1;i <= n;i ++){
        A[i] = id[s2[i]];
        change(1,A[i]);
        ans += ask(1,A[i] + 1,n);
    }
    printf("%d",ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

T2

#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
#define INF 23333333333
using namespace std;
const int MAXN = 300000 + 50;
LL dp[MAXN];
LL w[MAXN];
LL maxx[MAXN];
int n;
int main(){
    freopen("resell.in","r",stdin);
    freopen("resell.out","w",stdout);
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++){
        scanf("%lld",&w[i]);
        dp[i] = maxx[i] = -INF;
    }
    dp[0] = 0;
    maxx[0] = 0;
    LL sum = 0;
    for(int i = 1;i <= n;i ++){
        int lim = min(i,n - i + 1);
        for(int j = 0;j <= lim;j ++){
            if(j != 0)dp[j] = max(dp[j],maxx[j - 1] - w[i]);
            dp[j] = max(dp[j],maxx[j + 1] + w[i]);
            dp[j] = max(dp[j],maxx[j]);
        }
        for(int j = 0;j <= lim;j ++){
            maxx[j] = max(maxx[j],dp[j]);
        }
    }
    printf("%lld",dp[0]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

T3

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 1000 + 50;
int map[MAXN][MAXN];
int n,m;
bool used[2][4][MAXN][MAXN];
int step[2][4][MAXN][MAXN];
int mu[] = {-1,1,0,0},
    mr[] = {0,0,-1,1};
struct zt{
    int c,dir,x,y,step;
};
bool operator < (zt a,zt b){
    return a.step > b.step;
}
priority_queue <zt> q;
int ans = -1;
int main(){
    freopen("cowboy.in","r",stdin);
    freopen("cowboy.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= m;j ++){
            scanf("%d",&map[i][j]);
        }
    }
    used[0][0][1][1] = true;
    q.push((zt){0,0,1,1,0});
    while(!q.empty()){
        zt u = q.top();
        q.pop();
        if(u.x == n && u.y == m){
            ans = step[u.c][u.dir][u.x][u.y];
            break;
        }
        for(int i = 0;i <= 3;i ++){
            int x = u.x + mu[i];
            int y = u.y + mr[i];
            if(!map[x][y] || (!u.c && map[x][y] == 3))continue;

            if(map[x][y] == 1){
                if(!used[u.c][i][x][y]){
                    used[u.c][i][x][y] = true;
                    step[u.c][i][x][y] = step[u.c][u.dir][u.x][u.y] + 1;
                    q.push((zt){u.c,i,x,y,step[u.c][i][x][y]});
                }
            }
            if(map[x][y] == 2){
                if(!used[1][i][x][y]){
                    used[1][i][x][y] = true;
                    step[1][i][x][y] = step[u.c][u.dir][u.x][u.y] + 1;
                    q.push((zt){1,i,x,y,step[1][i][x][y]});
                }
            }
            if(map[x][y] == 3){
                if(!u.c || used[u.c][i][x][y])continue;
                used[u.c][i][x][y] = true;
                step[u.c][i][x][y] = step[u.c][u.dir][u.x][u.y] + 1;
                q.push((zt){u.c,i,x,y,step[u.c][i][x][y]});
            }
            if(map[x][y] == 4){
                if(used[u.c][i][x][y])continue;
                used[u.c][i][x][y] = true;
                int x1 = x,y1 = y,temp = 0;
                //switch(i){
                    if(i == 0){
                        while(map[x1][y1] == 4){
                            x1 --;
                            temp ++;
                        }
                        if(!map[x1][y1] || map[x1][y1] == 3)x1 ++;
                    }//
                    else if(i == 1){
                        while(map[x1][y1] == 4)x1 ++,temp ++;
                        if(!map[x1][y1] || map[x1][y1] == 3)x1 --;
                    }//
                    else if(i == 2){
                        while(map[x1][y1] == 4)y1 --,temp ++;
                        if(!map[x1][y1] || map[x1][y1] == 3)y1 ++;
                    }//
                    else if(i == 3){
                        while(map[x1][y1] == 4)y1 ++,temp ++;
                        if(!map[x1][y1] || map[x1][y1] == 3)y1 --;
                    }//
                //}
                temp ++;
                //if(map[x1][y1] == 0 || map[x1][y1] == 3)continue;
                if(map[x1][y1] == 1){
                    if(!used[0][i][x1][y1]){
                        used[0][i][x1][y1] = true;
                        step[0][i][x1][y1] = step[u.c][u.dir][u.x][u.y] + temp;
                        q.push((zt){0,i,x1,y1,step[0][i][x1][y1]});
                    }
                }
                if(map[x1][y1] == 2){
                    if(!used[1][i][x1][y1]){
                        used[1][i][x1][y1] = true;
                        step[1][i][x1][y1] = step[u.c][u.dir][u.x][u.y] + temp;
                        q.push((zt){1,i,x1,y1,step[1][i][x1][y1]});
                    }
                }
                if(map[x1][y1] == 4){
                //  if(!used[0][i][x1][y1]){
                //      used[0][i][x1][y1] = true;
                        temp --;
                        step[0][i][x1][y1] = step[u.c][u.dir][u.x][u.y] + temp;
                        q.push((zt){0,i,x1,y1,step[0][i][x1][y1]});
                //  }
                }
            }
        }
    }
    printf("%d",ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值