【CF Gym100228】Graph of Inversions
Portal --> qwq(貌似是CodeForces Gym 100228 (ECNA2003) - I)
Description
对于长度为 \(n\) 的序列 \(A\) ,定义其逆序图 \(G\) 如下:无向图 \(G\)有\(n\) 个节点,编号为 \(0..n-1\) ;对于任意的$ 0≤i<j≤n−1$ ,如果有 \(a[i]>a[j]\),那么 \(G\)中存在一条 \(i\)和 \(j\)之间的边。例如:\(A=\{1,3,4,0,2\}, G=\{(0,3),(1,3),(1,4),(2,3),(2,4)\}\)
定义独立集 \(S\):对于\(∀x∈S,y∈S\) ,都不存在一条边$ (x,y)$
定义覆盖集 \(S\) :对于\(∀x∉S\),至少存在一条边$ (x,y)$,使得 \(y∈S\)
现在给你一个逆序图 \(G\)(保证合法),求$ G$ 有多少个点集既是独立集又是覆盖集。
数据范围:\(1<=n<=1000,0<=m<=n*(n-1)/2\)
Solution
首先。。图的独立集是。。一个np问题==那所以直接在图上面搞什么的显然是不理智的qwq
那所以。。要好好利用逆序图这个条件
把独立集和覆盖集放在回原来的序列里面来看,其实就是:\(S\)中的元素无法构成逆序对(也就是说。。必须递增),并且任意非\(S\)元素均能与\(S\)中至少一个元素构成逆序对
所以我们其实是要找有多少个递增的子序列满足第二个条件
这个要怎么找呢。。考虑dp,记\(f[i]\)表示以\(i\)结尾的满足条件的子序列有多少个,那么考虑转移,\(f[i]\)能够转移到\(f[j]\),当且仅当满足\(a[i]<a[j]\)并且\(i\)和\(j\)中间的这段都要能和子序列中的至少一个元素构成逆序对,也就是要么小于\(a[i]\)要么大于\(a[j]\),然后因为如果小于\(a[i]\)的话不满足第一个转移条件,所以\(i\)到\(j\)之间的,除了之前能够转移的位置,其他肯定都是小于\(a[i]\)的不用管,我们只要看\(>a[i]\)中最大的那个是不是\(>a[j]\)就好了,具体实现其实很简单,因为这些需要单独考虑的位置肯定是之前遇到的能够转移的位置,所以我们开多一个\(tmp\)记录一下最大值即可
至于这个序列要怎么还原,因为只有大于和小于关系,所以。。我们钦定一下这个序列是一个\(1\)到\(n\)的排列,然后我们可以通过逆序对得到每个数前面比它大的有多少个,后面比它大的有多少个,那就可以得到每个数的具体值了(为了方便统计答案我们可以将\(a[n+1]\)钦定成一个很大的数然后计算到\(n+1\)位,答案就是\(f[n+1]\))
代码大概长这个样子
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int N=1010;
struct Rec{
int x,y;
}rec[N*(N-1)/2];
int a[N],cnt[N];
ll f[N];
int n,m,ans;
void dp(){
int tmp;
f[0]=1;
for (int i=0;i<=n;++i){
tmp=n+2;
for (int j=i+1;j<=n+1;++j){
if (a[j]<a[i]||a[j]>=tmp) continue;
f[j]+=f[i];
tmp=a[j];
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i) cnt[i]=n-i;
for (int i=1;i<=m;++i){
scanf("%d%d",&rec[i].x,&rec[i].y);
++rec[i].x; ++rec[i].y;
if (rec[i].x>rec[i].y) swap(rec[i].x,rec[i].y);
++cnt[rec[i].y]; --cnt[rec[i].x];
}
for (int i=1;i<=n;++i) a[i]=n-cnt[i];
a[n+1]=n+1;
dp();
printf("%d\n",f[n+1]);
}
【CF Gym100228】Graph of Inversions的更多相关文章
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 【CF#303D】Rotatable Number
[题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...
- 【CF 463F】Escape Through Leaf
题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- 【CF 585E】 E. Present for Vitalik the Philatelist
E. Present for Vitalik the Philatelist time limit per test 5 seconds memory limit per test 256 megab ...
- 【35.20%】【CF 706D】Vasiliy's Multiset
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【26.8%】【CF 46D】Parking Lot
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【31.42%】【CF 714A】Meeting of Old Friends
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【31.95%】【CF 714B】Filya and Homework
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
随机推荐
- Two Sum - 新手上路
不是计算机相关专业毕业的,从来没用过leetcode,最近在学习数据结构和算法,用leetcode练练手. 新手上路,代码如有不妥之处,尽管指出来. 今天抽空做的第一个题:Two Sum(最简单的呃呃 ...
- 小白初识 - 基数排序(RadixSort)
基数排序算是桶排序和计数排序的衍生吧,因为基数排序里面会用到这两种其中一种. 基数排序针对的待排序元素是要有高低位之分的,比如单词adobe,activiti,activiti就高于adobe,这个是 ...
- python爬取淘宝华为手机
import re from selenium import webdriver from selenium.common.exceptions import TimeoutException fro ...
- Pyhone学习之环境搭建
一.python 环境搭建 本章节我们将向大家介绍如何在本地搭建Python开发环境.Python可应用于多平台包括 Linux 和 Mac OS X.你可以通过终端窗口输入 "python ...
- 17 Tips For Writing An Excellent Email Subject Line
Out of the billions of emails that are sent every day, how can you make sure that yours stands out? ...
- pyextend库-unpack列表集合字符串解包函数
pyextend - python extend lib unpack (iterable, count, fill=None) 参数: iterable: 实现 __iter__的可迭代对象, 如 ...
- CF刷刷水题找自信 2
CF 1114A Got Any Grapes(葡萄)? 题目意思:给三个人分葡萄,三个人对葡萄的颜色有一些要求,问所准备的三种颜色的葡萄能否满足三人的要求. 解题意思:直接按条件判断即可. #in ...
- c# 读取blob数据
Stream stream = new MemoryStream(data); BinaryReader r = new BinaryReader(stream); int iRawImageWidt ...
- 机器学习实战第二章----KNN
tile的使用方法 tile(A,n)的功能是把A数组重复n次(可以在列方向,也可以在行方向) argsort()函数 argsort()函数返回的是数组中值从大到小的索引值 dict.get()函数 ...
- Numpy and Pandas
安装 视频链接:https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/ pip install numpy pip instal ...