Codeforces Round #167 (Div. 1)

本博客深入探讨了Codeforces平台上的两个竞赛题目:Dima and Staircase与Dima and Two Sequences,详细解释了解题思路和关键算法应用。通过线段树等数据结构的高效使用,实现复杂操作的简化,以及对于序列排列问题的巧妙解决策略,旨在提升读者的竞赛解题能力和算法理解。

http://codeforces.com/contest/273

A. Dima and Staircase

简单的线段树成段更新询问操作

// Author : JayYe  Created Time: 2013-11-23 19:32:38
#include 
 
  
#include 
  
   
#include 
   
    
using namespace std;

typedef __int64 ll;
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
const int maxn = 100000 + 10;

ll mx[maxn<<2], mark[maxn<<2];
void down(int rt) {
    if(mark[rt]) {
        mark[rt<<1] = mark[rt<<1|1] = mark[rt];
        mx[rt<<1] = mx[rt<<1|1] = mx[rt];
        mark[rt] = 0;
    }
}

void up(int rt) {
    mx[rt] = max(mx[rt<<1], mx[rt<<1|1]);
}

void create(int rt, int l, int r) {
    if(l == r) {
        scanf("%I64d", &mx[rt]);
        return ;
    }
    int mid = (l + r)/2;
    create(lson); create(rson);
    up(rt);
}

ll query(int rt, int l, int r, int L, int R) {
    if(L <= l && R >= r) {
        return mx[rt];
    }
    down(rt);
    int mid = (l + r)/2;
    ll ret = 0;
    if(L <= mid)    ret = max(ret, query(lson, L, R));
    if(R > mid) ret = max(ret, query(rson, L, R));
    up(rt);
    return ret;
}

void update(int rt, int l, int r, int L, int R, ll val) {
    if(L <= l && R >= r) {
        mark[rt] = val;
        mx[rt] = val;
        return ;
    }
    down(rt);
    int mid = (l + r)/2;
    if(L <= mid)    update(lson, L, R, val);
    if(R > mid) update(rson, L, R, val);
    up(rt);
}

int main() {
    int n, m, w, h;
    scanf("%d", &n);
    create(1, 1, n);
    scanf("%d", &m);
    while(m--) {
        scanf("%d%d", &w, &h);
        ll now = query(1, 1, n, 1, w);
        printf("%I64d\n", now);
        update(1, 1, n, 1, w, now + h);
    }
    return 0;
}

   
  
 


B. Dima and Two Sequences

题意:给你两个序列a, b,分别构成点 ( a(1), 1 ), ( a(2), 2), ... ( a(n), n) , (b(1), 1) , ( b(2), 2) ... ( b(n), n)

将这2n个点排成一列,要求x不下降,问有多少种不同的排列方法。


思路:对于不同的x,肯定是排在不同的块中的,所以对于相同的x进行处理排列数,然后相乘即可。由于相同的x中y相同的最多就两个相同,所以直接暴力处理这个块中排列数即可。

// Author : JayYe  Created Time: 2013-11-23 19:50:38
#include 
  
   
#include 
   
    
#include 
    
     
#include 
     
using namespace std;
typedef __int64 ll;

const int maxn = 200000 + 10;

struct PP {
    int x, y;
    PP(){}
    PP(int x, int y): x(x), y(y) {}
    bool operator < (const PP & a) const {
        if(x == a.x)    return y < a.y;
        return x < a.x;
    }
}a[maxn];

ll exgcd(ll a, ll b, ll &x, ll &y) {
    if(!b) {
        x = 1; y = 0;
        return a;
    }
    ll ret = exgcd(b, a%b, y, x);
    y -= a/b*x;
    return ret;
}

ll inv(ll a, ll m) {
    ll x, y, d = exgcd(a, m, x, y);
    if(x < 0)   x += m;
    return x;
}

map
      
           mp, qw;

int main() {
    int n, x, m;
    scanf("%d", &n);
    int tot = 0;
    for(int i = 1;i <= n; i++) {
        scanf("%d", &x);
        a[tot++] = PP(x, i);
    }
    for(int i = 1;i <= n; i++) {
        scanf("%d", &x);
        a[tot++] = PP(x, i);
    }
    scanf("%d", &m);
    sort(a, a + tot);
    ll ans = 1;
    for(int i = 0;i < tot; i++) {
        if(i + 1 < tot) {
            if(a[i].x == a[i+1].x && a[i].y == a[i+1].y) {
                mp[a[i].x] ++; mp[a[i].x] ++;
                qw[a[i].x] ++;
                i++;
            }
            else
                mp[a[i].x]++;
        }
        else {
            mp[a[i].x]++;
        }
    }
    for(map
       
         ::iterator it = mp.begin();it != mp.end(); it++) { int cur = it->second; int now = qw[it->first]; for(int i = 1;i <= cur; i++) if(i % 2 == 0) { if(now) { ans = ans*(i/2)%m; now--; } else ans = ans*i%m; } else ans = ans*i%m; } printf("%I64d\n", ans); return 0; } 
       
      
    
   
  

评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值