2019牛客国庆集训派对day3
E. Grid
大意: 给定$n\cdot m$个点的图, 初始无边, $q$个操作, $(1,a,b)$表示第$a$列到第$b$列全连起来, $(2,a,b)$表示把第$a$行到第$b$行全连起来, 每次操作后输出连通块个数.
直接用$set$暴力模拟即可.
还有一种线段树做法, 设一共$a$行连通, $b$列连通, 可以发现答案是$nm-a(m-1)-b(n-1)+max(0,(a-1)(b-1))$, 可以用线段树维护$a$和$b$即可
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head int n,m,q;
struct _ {
int l,r;
bool operator < (const _ &rhs) const {
return l<rhs.l;
}
};
set<_> A,B;
//A为行
//B为列 int main() {
while (~scanf("%d%d%d", &n, &m, &q)) {
A.clear(),B.clear();
ll ans = (ll)n*m, suma = , sumb = ;
while (q--) {
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if (op==) {
auto p = A.lower_bound({l,});
if (p!=A.begin()&&(--p)->r>=l) l = p->l;
while () {
p = A.lower_bound({l,});
if (p==A.end()||p->l>r) break;
r = max(r, p->r);
ans += (p->r-p->l+1ll)*(m-(B.empty()?:sumb));
suma -= p->r-p->l+;
A.erase(p);
if (B.size()&&A.empty()) ans += sumb-;
}
suma += r-l+;
ans -= (r-l+1ll)*(m-(B.empty()?:sumb));
if (B.size()&&A.empty()) ans -= sumb-;
A.insert({l,r});
}
else {
auto p = B.lower_bound({l,});
if (p!=B.begin()&&(--p)->r>=l) l = p->l;
while () {
p = B.lower_bound({l,});
if (p==B.end()||p->l>r) break;
r = max(r, p->r);
ans += (p->r-p->l+1ll)*(n-(A.empty()?:suma));
sumb -= p->r-p->l+;
B.erase(p);
if (A.size()&&B.empty()) ans += suma-;
}
sumb += r-l+;
ans -= (r-l+1ll)*(n-(A.empty()?:suma));
if (A.size()&&B.empty()) ans -= suma-;
B.insert({l,r});
}
printf("%lld\n",ans);
}
}
}
G. 排列
大意: 给定$m$个二元组$(a_i,b_i)$, 给定序列$p$, 求将$p$重排, 使得$\sum\limits_{i=1}^m |p_{a_i}-p_{b_i}|$最小, 输出最小值.
从小到大添入每一个$p$, 这样就能去掉绝对值号, 跑一个状压$dp$就行了.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head const int N = 1e6+;
int n,m,p[N],g[N],cnt[<<];
ll dp[<<];
void chkmin(ll &a, ll b) {a>b?a=b:;}
int main() {
REP(i,,(<<)-) cnt[i]=cnt[i>>]+(i&);
while (~scanf("%d%d", &n, &m)) {
REP(i,,n-) scanf("%d",p+i),g[i]=;
REP(i,,m) {
int a,b;
scanf("%d%d",&a,&b);
--a,--b;
g[a] ^= <<b;
g[b] ^= <<a;
}
sort(p,p+n);
dp[] = ;
int mx = (<<n)-;
REP(i,,mx) dp[i] = 1e18;
REP(i,,mx-) {
REP(j,,n-) if (i>>j&^) {
int x = cnt[i&g[j]], y = cnt[~i&mx&g[j]];
chkmin(dp[i^<<j],dp[i]+(ll)(x-y)*p[cnt[i]]);
}
}
printf("%lld\n", dp[mx]);
}
}
2019牛客国庆集训派对day3的更多相关文章
- 2019牛客国庆集训派对day3 买一送一
题目链接: 题意:有n个点,n-1条单向边,每个点都销售一类商品 问从点1开始走,买第一样商品类型为x,买第二样商品类型为y,问不同有序对<x,y>的数量 解法: col[i]表示这个点的 ...
- 2019牛客国庆集训派对day5
2019牛客国庆集训派对day5 I.Strange Prime 题意 \(P=1e10+19\),求\(\sum x[i] mod P = 0\)的方案数,其中\(0 \leq x[i] < ...
- 2019 牛客国庆集训派对day1-C Distinct Substrings(exkmp+概率)
链接:https://ac.nowcoder.com/acm/contest/1099/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 计算几何板子题【2019牛客国庆集训派对day7——三角形和矩形】【多边形相交的面积】
链接:https://ac.nowcoder.com/acm/contest/1112/J来源:牛客网 题目描述 Bobo 有一个三角形和一个矩形,他想求他们交的面积. 具体地,三角形和矩形由 8 个 ...
- 2019牛客国庆集训派对day7 A 2016
链接:https://ac.nowcoder.com/acm/problem/52800来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K ...
- 2019牛客国庆集训派对day1(A, B E F K)
链接:https://ac.nowcoder.com/acm/contest/1099#question A:可知符合条件的图中间肯定存在一个由1构成的矩形,找到由1构成矩形的边界,判断出现的1的数量 ...
- 牛客国庆集训派对Day3 B Tree
Tree 思路: 树形dp 注意0不存在逆元,任何一个数乘以0就变成0了,就没有价值浪,所以要暴力转移 代码: #pragma GCC optimize(2) #pragma GCC optimize ...
- 牛客国庆集训派对Day3 I Metropolis
Metropolis 思路: 多源点最短路 只要两个不同源点的最短路相遇,我们就更新两个源点的答案 代码: #pragma GCC optimize(2) #pragma GCC optimize(3 ...
- 牛客国庆集训派对Day3 A Knight
Knight 思路: bfs打表找规律 如下图 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) ...
随机推荐
- bash循环得到日期目录
#!/bin/bash today=$(date "+%Y%m%d") echo 'today is :'${today} single_input="raw_data/ ...
- Zuul超时配置
在实际运用过程中,发现有时候zuul的第一次请求经常会超时.不知道你们怎样,我经常在重启zuul后,第一次访问经常会出现超时现象,但是第二次访问就不会了. 第一次经常超时 这是因为zuul采用了懒加载 ...
- 使用ADO.NET
Program using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- restframework之节流
基本思路(原生Django而言): 在django2.x中,若出现节流(访问频率控制)的需求,我们首先想到的是使用一个字典(dict类型)来存储所有IP地址的访问时间记录,这是针对于匿名用户(IP)而 ...
- The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
https://www.opentechguides.com/how-to/article/powershell/105/powershel-security-error.html Unblockin ...
- el-cascader遇到一个坑的问题
经仔细分析,如果二级和三级的value一样,就会出现这个问题.
- 记某app内购破解 – 安卓逆向菜鸟的初体验
前言 因为某个机缘,我拿到一个赛车app,玩了一会想买个装备,居然要我掏钱包,作为一名cracker,我觉得我的尊严受到了严重的蔑视(无奈钱包空空),我觉得要捍卫我那脆弱的玻璃心(钱包),所以,开干吧 ...
- Sword 第三方库介绍二
/* uuid生成 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <ass ...
- python中的捕获异常、异常跟踪
# 捕获异常,打印异常信息 try: 1/0 except Exception as e: print(e) 输出结果是integer division or modulo by zero,只知道是报 ...
- LeetCode_412. Fizz Buzz
412. Fizz Buzz Easy Write a program that outputs the string representation of numbers from 1 to n. B ...