Description

魔术师的桌子上有n个杯子排成一行,编号为1,2,…,n,其中某些杯子底下藏有一个小球,如果你准确地猜出是哪些杯子,你就可以获得奖品。花费c_ij元,魔术师就会告诉你杯子i,i+1,…,j底下藏有球的总数的奇偶性。
采取最优的询问策略,你至少需要花费多少元,才能保证猜出哪些杯子底下藏着球?

Input

第一行一个整数n(1<=n<=2000)。
第i+1行(1<=i<=n)有n+1-i个整数,表示每一种询问所需的花费。其中c_ij(对区间[i,j]进行询问的费用,1<=i<=j<=n,1<=c_ij<=10^9)为第i+1行第j+1-i个数。

Output

输出一个整数,表示最少花费。

题解:

参考小胖的奇偶那道题。

那道题用的并查集维护。

如果知道 i..j 之间奇偶性的话,实际上知道的是 sum[j]-sum[i-1]的奇偶性(sum为前缀和)。

可以用扩展域的并查集维护任意两个sum[i]之间的差值。

有了这个结论,如果全部知道哪些杯底有球,就需要所有的sum之间的关系已知,也就是并查集中所有点都在一个集合中,是一棵树。

所以遍转化成了最小生成树问题。

代码:

Kruskal 10384 ms

#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
int n;
struct N{
    int x,y,w;
}map[*];
int tot;
bool cmp(N a,N b){
    return a.w<b.w;
}
];
int get(int x){
    return f[x]==x?x:f[x]=get(f[x]);
}
int main(){
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    scanf("%d",&n);
    ;i<=n;i++){
        for(int j=i;j<=n;j++){
            scanf("%d",&map[tot].w);
            map[tot].x=i;
            map[tot].y=j+;
            tot++;
        }
    }
    sort(map,map+tot,cmp);
    );
    ;i<=n+;i++) f[i]=i;
    ;i<tot;i++){
        if(get(map[i].x)!=get(map[i].y)){
            f[get(map[i].x)]=get(map[i].y);
            cost+=map[i].w;
        }
    }
    printf("%lld\n",cost);

    ;
}

Prim 16480 ms

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
//by zrt
//problem:
using namespace std;
int n;
][];
struct N{
    int x,w;
    N(,){
        x=a,w=b;
    }
    friend bool operator < (N a,N b){
        return a.w>b.w;
    }
};
priority_queue<N> q;
];
int main(){
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    scanf("%d",&n);
    int x;
    ;i<=n;i++){
        for(int j=i;j<=n;j++){
            scanf("%d",&x);
            val[j+][i]=val[i][j+]=x;
        }
    }
    );
    q.push(N(,));
    int c;
    n=n+;
    while(!q.empty()){
        x=q.top().x;c=q.top().w;q.pop();
        if(vis[x]) continue;
        vis[x]=;cost+=c;
        ;i<=n;i++){
            if(!vis[i]){
                q.push(N(i,val[x][i]));
            }
        }
    }
    printf("%lld\n",cost);
    ;
}

BZOJ 3714: [PA2014]Kuglarz的更多相关文章

  1. bzoj 3714 [PA2014]Kuglarz 最小生成树

    [PA2014]Kuglarz Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1335  Solved: 672[Submit][Status][Di ...

  2. bzoj 3714 [PA2014]Kuglarz——思路+最小生成树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3714 如果用s[ i ]表示前 i 个的奇偶性,那么c(i_j)表示s[ i-1 ]^s[ ...

  3. bzoj 3714: [PA2014]Kuglarz【最小生成树】

    参考:https://blog.csdn.net/aarongzk/article/details/48883741 没想到吧.jpg 来自题解: "如果用sum[i]表示前i个杯子底球的总 ...

  4. 3714: [PA2014]Kuglarz

    3714: [PA2014]Kuglarz 链接 思路: 好题.对于每个点都需要确定它的值,那么一个点可以直接询问[i,i]来确定,或者已经知道了[i,j]和[i+1,j]推出来. 但是可能产生冲突, ...

  5. 【BZOJ】3714: [PA2014]Kuglarz

    题意 \(n(1 \le n \le 2000)\)个数每个数是\(0\)或\(1\),现在可以花费\(c_{i, j}\)知道\([i, j]\)的奇偶性,问将所有数都找出来的最小花费. 分析 如果 ...

  6. bzoj3714: [PA2014]Kuglarz

    [PA2014]KuglarzTime Limit: 20 Sec Memory Limit: 128 MBSubmit: 553 Solved: 317[Submit][Status][Discus ...

  7. [PA2014]Kuglarz

    [PA2014]Kuglarz 题目大意: 有一个长度为\(n(n\le2000)\)的0/1串,你可以花\(c_{i,j}\)的钱,询问区间\([i,j]\)的异或和.问至少要多少元才能知道原来的序 ...

  8. 【BZOJ3714】[PA2014]Kuglarz 最小生成树

    [BZOJ3714][PA2014]Kuglarz Description 魔术师的桌子上有n个杯子排成一行,编号为1,2,…,n,其中某些杯子底下藏有一个小球,如果你准确地猜出是哪些杯子,你就可以获 ...

  9. bzoj 3714 [ PA 2014 ] Kuglarz —— 思路+最小生成树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3714 因为每个杯子下最多一个小球,所以从奇偶性就可以看出有没有球: 询问一段区间,等于知道一 ...

随机推荐

  1. android里面线程睡眠事件使用方法

    SystemClock.sleep(时间); 不用Thread.sleep()的原因:要抛异常,占用资源

  2. VS活动解决方案平台

    测试环境:win7 x64 测试程序:WCF查询数据库后将数据集返回到Winform程序加载并显示 测试结果: 1.从感觉来说Exe在 x86目标平台生成,启动速度快. 2.内存消耗:x86的程序在超 ...

  3. 几道华为经典C语言面试题

    1.找错 void test1() { char string[10]; char* str1="0123456789"; strcpy(string, str1); } 这里st ...

  4. python xml包使用记录

    <?xml version="1.0" encoding="utf-8" ?> <request> <functionID> ...

  5. Activity(一)

    一个应用程序中至少包含一个Activity,Activity启动流程:当启动一个应用程序时,android操作系统会访问该应用程序的AndroidManifest.xml文件(该文件中说明了应用程序使 ...

  6. 算法:C++排列组合

    题目:给定1-n数字,排列组合. 解法:递归.第一个数字有n种选择,第二个数字有n-1种选择,依次递归排列输出.用数组表示n个数字,用过的数字置0. 实现语言:C++ #include <ios ...

  7. GIS中栅格数据的拼接

    Datamanager Tools——Raster——Raster Dataset——Mosaic to New Raster 如果最大值是实际的真值,选择masaic operator要保留Max ...

  8. Php+Redis 实现Redis提供的lua脚本功能

    <?php require_once "predis-0.8/autoload.php"; $config['schema'] = 'tcp'; $config['host' ...

  9. 说明一下JNI 与AIDL

    代码在评论中. JNI: 为什么需要JNI: 因为android是由[JAVA & C/C++]组成.Java运行在Dalvik虚拟机中. 没有办法直接访问底层硬件.底层HW相关目前技术一般都 ...

  10. linux下安装protobuf教程+示例(详细)

    (.pb.h:9:42: fatal error: google/protobuf/stubs/common.h: No such file or directory 看这个就应该知道是没有找到头文件 ...