1. 程式人生 > >[隊內測試Day10.24Final]逆序對+表示式計算+貪心+圖論+數論?

[隊內測試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;
}