前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >洛谷P2831 愤怒的小鸟(状压dp)

洛谷P2831 愤怒的小鸟(状压dp)

作者头像
attack
发布2018-11-29 17:14:21
5480
发布2018-11-29 17:14:21
举报
文章被收录于专栏:数据结构与算法

题意

题目链接

Sol

这题。。。。我样例没过就A了??。。算了,就当是样例卡精度吧。。

直接状压dp一下,\(f[sta]\)表示干掉\(sta\)这个集合里面的鸟的最小操作数

转移的时候判断一下一次能干掉多少鸟。。

代码语言:javascript
复制
#include<bits/stdc++.h>
#define LL long long 
using namespace std;
const int MAXN = 100001;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, a[MAXN], block, siz[MAXN], flag[MAXN], rak[MAXN], tp[MAXN];
vector<int> v[MAXN];
set<int> s[MAXN];
int comp(const int &x, const int &y) {
    return a[x] == a[y] ? x < y : a[x] < a[y];
}
int main() {
    N = read(); M = read(); block = sqrt(M);
    for(int i = 1; i <= N; i++) a[i] = read(), tp[i] = i;
    sort(tp + 1, tp + N + 1, comp);
    for(int i = 1; i <= N; i++) rak[tp[i]] = i; 
    for(int i = 1; i <= M; i++) {
        int x = read(), y = read();
        if(rak[x] > rak[y]) v[x].push_back(y), siz[x]++;
        else v[y].push_back(x), siz[y]++;
    }
    LL ans = 0;
    for(int i = 3; i <= N; i++) {
        int x = tp[i];
        for(int j = 0, to; j < v[x].size(); j++) flag[to = v[x][j]] = i;
        for(int j = 0, to; j < v[x].size(); j++) {
            if(siz[to = v[x][j]] <= block) {
                for(int k = 0; k < v[to].size(); k++) 
                    if(flag[v[to][k]] == i) ans += a[x];
            } else {
                for(int k = 0; k < v[x].size(); k++)
                    if(s[to].count(v[x][k])) ans += a[x];
            }
            s[x].insert(to);
        }
    }
    cout << ans;
    return 0;
}
/*
2 1
13 17
2 1

*/
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-10-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档