比较可惜昨天比赛的时候时间不够了,在比赛结束之后五分钟找出了bug提交通过了。然并软;

首先这题说b数组的后一项要么等于前一项,要么等于前一项加一,而且如果a[i] == a[j] ,那么b[i] == b[j],所以如果a[i] == a[j],b[i]到b[j]这个区间的值都是一样的,可以看做一个整体;

那么这题要求的不就是2^(区间个数 - 1)吗;

刚看这题就觉得区间合并用并查集,但是当时思路不够清晰后来用了线段树ac掉了,今天就把两种方法的代码都贴上;

  • 线段树解法

    1102E - 22 GNU C++11 Happy New Year! 265 ms 9428 KB
    #include "bits/stdc++.h"
    using namespace std;
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const int MOD = ;
    //这里的tree其实就是一个懒标记
    int tree[];
    map<int, int> mp;
    int n, m, L, R, cnt;
    //查询包含q的区间前端
    int queryHead(int l, int r, int id, int q) {
    if (tree[id] != ) {
    return tree[id];
    }
    int mid = l + r >> ;
    if (q <= mid) {
    return queryHead(l, mid, id << , q);
    } else {
    return queryHead(mid + , r, id << | , q);
    }
    }
    //把区间[L, R]的值修改为L;
    void update(int l, int r, int id) {
    if (l >= L && r <= R) {
    tree[id] = L;
    return;
    }
    int mid = l + r >> ;
    if (L <= mid) {
    update(l, mid, id << );
    }
    if (R > mid) {
    update(mid + , r, id << | );
    }
    }
    // 查询线段树中包含q的节点的区间末端
    int queryTail(int l, int r, int id, int q) {
    if (tree[id] != ) {
    return r;
    }
    int mid = l + r >> ;
    if (q <= mid) {
    return queryTail(l, mid, id << , q);
    } else {
    return queryTail(mid + , r, id << | , q);
    }
    }
    // 快速幂
    int quick_pow(int n, int m) {
    int ans = ;
    while (m) {
    if (m & ) {
    ans = 1LL * ans * n % MOD;
    }
    n = 1LL * n * n % MOD;
    m >>= ;
    }
    return ans;
    }
    int main() {
    scanf("%d", &n);
    for (int i = ; i <= n; i++) {
    scanf("%d", &m);
    L = mp.count(m) ? queryHead(, n, , mp[m]) : i;
    R = i;
    update(, n, );
    mp[m] = i;
    }
    int mx = -;
    for (int i = ; i <= n; i = queryTail(, n, , i) + ) {
    int k = queryHead(, n, , i);
    // 因为这题区间合并,这里的k得到的不是合并后的区间末端,只是线段树中的区间末端;所以要比较是否和上一个线段树区间属于同一区间
    if (k != mx) {
    mx = k;
    cnt++;
    }
    }
    printf("%d\n", quick_pow(, cnt - ));
    return ;
    }
  • 并查集解法
    1102E - 22 GNU C++11 Happy New Year! 171 ms 7100 KB
    #include "bits/stdc++.h"
    using namespace std;
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const int MOD = ;
    int pre[], cnt;
    map<int, int> mp;
    int find(int id) {
    if (pre[id] == ) {
    return id;
    }
    return pre[id] = find(pre[id]);
    }
    int quick_pow(int n, int m) {
    int ans = ;
    while (m) {
    if (m & ) {
    ans = 1LL * ans * n % MOD;
    }
    n = 1LL * n * n % MOD;
    m >>= ;
    }
    return ans;
    }
    int main() {
    int n, m;
    scanf("%d", &n);
    for (int i = ; i <= n; i++) {
    scanf("%d", &m);
    int head = mp.count(m) ? find(mp[m]) : i;
    int tail = i;
    while (true) {
    int x = find(tail);
    if (x == head) {
    break;
    }
    pre[x] = head;
    tail = x - ;
    }
    mp[m] = i;
    }
    for (int i = n; i > ; i = find(i) - ) {
    cnt++;
    }
    printf("%d\n", quick_pow(, cnt - ));
    return ;
    }

CF-1102E-Monotonic Renumeration的更多相关文章

  1. 补题Codeforces 1102E. Monotonic Renumeration

    这个题还是不太懂,下面附上的是大佬的题解(https://zhanghuimeng.github.io/post/codeforces-1102e-monotonic-renumeration/) E ...

  2. 【Codeforces 1102E】Monotonic Renumeration

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 会发现如果a[i]=a[j] 那么b[i]~b[j]都是相同的,等于b[i] 而b[i]等于b[i-1]+1或者b[i] 有两种可能 所以对于 ...

  3. Codeforces J. Monotonic Renumeration(组合)

    题目描述: You are given an array consisting of nmonotonic renumeration as an array b consisting of \(n\) ...

  4. Codeforces Round #531 (Div. 3) E. Monotonic Renumeration (构造)

    题意:给出一个长度为\(n\)的序列\(a\),根据\(a\)构造一个序列\(b\),要求: ​ 1.\(b_{1}=0\) ​ 2.对于\(i,j(i\le i,j \le n)\),若\(a_{i ...

  5. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  6. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  7. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  8. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  9. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  10. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

随机推荐

  1. Django框架(九):视图(二) HttpRequest对象、HttpResponse对象

    1. HttpRequest对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpR ...

  2. 用数组来实现Stack

    1:Stack特点 stack:栈,是一种特殊的数据结构,有着先入后出的特点(first in last out).stack中栈底始终不变,只有一端能变化.栈顶在有数据push的时候增大,有数据po ...

  3. [原]你知道怎么使用DebugView查看内核调试信息吗?

    原总结注册表sysinternalsdebugviewprocess explorerprocess monitor 简介 DebugView是sysinternals工具集中的一款用来查看调试信息的 ...

  4. Dynamics CRM - 通过 C# Plugin 来 abandon Business Process Flow

    需求说明: 当一个 Entity 存在 Business Process Process 时,有时我们需要改变其状态,在之前写的博客有讲了可以通过 JavaScript 来实现,本篇就来讲一下如何通过 ...

  5. 2019年icpc上海网络赛 B Light bulbs (分块、差分)

    https://nanti.jisuanke.com/t/41399 题目大意: 有n个灯,m次操作,每次修改[l,r]内的灯,(off - on ,on - off),问最后有几盏灯亮着. 换种说法 ...

  6. 设置Schema-Registry的配置,以支持Schema变化

    设置Schema-Registry的配置,以支持Schema变化 https://blog.csdn.net/lzufeng/article/details/81566766 curl -X PUT ...

  7. kotlin 单例模式

    class Single{ companion object { val instance:Single by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZE ...

  8. Linux服务器性能查看命令

    一.uptime命令 [root@#test~]# uptime15:26:42 up 101 days, 18:44,  3 users,  load average: 0.18, 0.22, 0. ...

  9. 使用okhttp连接网络,再把数据储存进Sqlite

    这次会把所有之前学过的东西应用在一起,写一个登入的功能. 1. Activity调用CONFIG,获得URL后 2. Activity再调用Okhttp,从服务器返回JSON 3. Activity调 ...

  10. poj-3259 Wormholes(无向、负权、最短路之负环判断)

    http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...