3714: [PA2014]Kuglarz

3714: [PA2014]Kuglarz

Time Limit: 20 Sec   Memory Limit: 128 MB
Submit: 596   Solved: 343
[ Submit][ Status][ Discuss]

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

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

Sample Input

5
1 2 3 4 5
4 3 2 1
3 4 5
2 1
5

Sample Output

7

HINT

Source

[ Submit][ Status][ Discuss]

不妨对原数列处理前缀和,这样每个信息等价于允许sum[r]和sum[l-1]的奇偶性互推
一开始,能得到的只有sum[0]的奇偶性,需要选择某些区间来得到Ans
每次选择一个区间,就将l - 1和r连一条边,这样就是做个mst了
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
 
const int maxn = 2020;
const int maxm = maxn * maxn;
typedef long long LL;
 
struct E{
    int x,y,w; E(){}
    E(int x,int y,int w): x(x),y(y),w(w){}
    bool operator < (const E &B) const{return w < B.w;}
}edgs[maxm];
 
int n,tot,fa[maxn];
LL Ans;
 
int getfa(int k) {return k == fa[k] ? k : fa[k] = getfa(fa[k]);}
int getint()
{
    char ch = getchar(); int ret = 0;
    while (ch < '0' || '9' < ch) ch = getchar();
    while ('0' <= ch && ch <= '9')
        ret = ret*10 + ch - '0',ch = getchar();
    return ret;
}
 
int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
    #endif
     
    n = getint();
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++)
            edgs[++tot] = E(i - 1,j,getint());
    for (int i = 1; i <= n; i++) fa[i] = i;
    sort(edgs + 1,edgs + tot + 1);
     
    for (int i = 1; i <= tot; i++)
    {
        int fx = getfa(edgs[i].x),fy = getfa(edgs[i].y);
        if (fx == fy) continue; fa[fx] = fy; Ans += 1LL * edgs[i].w;
    }
    cout << Ans << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值