Problem Statement

A museum exhibits $N$ jewels, Jewel $1, 2, ..., N$.
The coordinates of Jewel $i$ are $(x_i, y_i)$ (the museum can be regarded as a two-dimensional plane), and the value of that jewel is $v_i$.

Snuke the thief will steal some of these jewels.

There are $M$ conditions, Condition $1, 2, ..., M$, that must be met when stealing jewels, or he will be caught by the detective.
Each condition has one of the following four forms:

  • ($t_i$ =L, $a_i$, $b_i$) : Snuke can only steal at most $b_i$ jewels whose $x$ coordinates are $a_i$ or smaller.
  • ($t_i$ =R, $a_i$, $b_i$) : Snuke can only steal at most $b_i$ jewels whose $x$ coordinates are $a_i$ or larger.
  • ($t_i$ =D, $a_i$, $b_i$) : Snuke can only steal at most $b_i$ jewels whose $y$ coordinates are $a_i$ or smaller.
  • ($t_i$ =U, $a_i$, $b_i$) : Snuke can only steal at most $b_i$ jewels whose $y$ coordinates are $a_i$ or larger.

Find the maximum sum of values of jewels that Snuke the thief can steal.

Constraints

  • $1 \leq N \leq 80$
  • $1 \leq x_i, y_i \leq 100$
  • $1 \leq v_i \leq 10^{15}$
  • $1 \leq M \leq 320$
  • $t_i$ is L, R, U or D.
  • $1 \leq a_i \leq 100$
  • $0 \leq b_i \leq N - 1$
  • $(x_i, y_i)$ are pairwise distinct.
  • $(t_i, a_i)$ are pairwise distinct.
  • $(t_i, b_i)$ are pairwise distinct.

Input

Input is given from Standard Input in the following format:

$N$
$x_1$ $y_1$ $v_1$
$x_2$ $y_2$ $v_2$
$:$
$x_N$ $y_N$ $v_N$
$M$
$t_1$ $a_1$ $b_1$
$t_2$ $a_2$ $b_2$
$:$
$t_M$ $a_M$ $b_M$

Output

Print the maximum sum of values of jewels that Snuke the thief can steal.


Sample Input 1

7
1 3 6
1 5 9
3 1 8
4 3 8
6 2 9
5 4 11
5 7 10
4
L 3 1
R 2 3
D 5 3
U 4 2

Sample Output 1

36

Stealing Jewel $1, 5, 6$ and $7$ results in the total value of $36$.


Sample Input 2

3
1 2 3
4 5 6
7 8 9
1
L 100 0

Sample Output 2

0

Sample Input 3

4
1 1 10
1 2 11
2 1 12
2 2 13
3
L 8 3
L 9 2
L 10 1

Sample Output 3

13

Sample Input 4

10
66 47 71040136000
65 77 74799603000
80 53 91192869000
24 34 24931901000
91 78 49867703000
68 71 46108236000
46 73 74799603000
56 63 93122668000
32 51 71030136000
51 26 70912345000
21
L 51 1
L 7 0
U 47 4
R 92 0
R 91 1
D 53 2
R 65 3
D 13 0
U 63 3
L 68 3
D 47 1
L 91 5
R 32 4
L 66 2
L 80 4
D 77 4
U 73 1
D 78 5
U 26 5
R 80 2
R 24 5

Sample Output 4

305223377000

这个范围,基本上只要是多项式复杂度都能过得去了吧。

\(x\) 坐标小于等于 \(a_i\) 的至多有 \(b_i\) 个,这个条件很不友好。我们把他转换一下,这其实说明如果将所有选了的宝石按照 \(x\) 坐标从小到大排序,排名大于 \(b_i\) 的,\(x\) 坐标要大于 \(a_i\)。同理,坐标大于等于 \(a_i\) 的至多有 \(b_i\) 个的条件,我们可以转化成按 \(x\) 坐标从小到大排序之后,倒数排名要大于 \(b_i\) 的,\(x\) 坐标要小于等于 \(b_i\)。\(y\) 坐标同理。

这一个正着一个倒着的,怎么玩啊?反正 \(n\) 小的离谱,我们可以枚举一下总共选多少个数,然后可以求出 \(x\) 坐标排名为 \(i\) 的数 \(x\) 坐标的范围。\(y\) 坐标同理。

在求出 \(x\),\(y\) 排名第 \(i\) 的范围时,发现现在的问题转变为一个选数的问题,要给 \(x\) 排名第 \(i\) 的选一个坐标,然后还要给 \(y\) 排名第 \(i\) 的选一个坐标。这个问题就是经典的费用流模型了。

从源点连向表示 \(x\) 排名为第 \(i\) 个的点,流量1费用0,再从 \(x\) 排名第 \(i\) 个的点连向每个符合要求的坐标,流量1费用0。要把一个坐标拆成两个点,中间连流量1费用 \(v_i\)。如果他在 \(y\) 坐标可以排名第 \(i\),那么就从他连向表示 \(y\) 坐标排名第 \(i\) 的点,再连回汇点。

判断一下是否满流就行了。但我们想求最大费用,相当于取负后求最小费用,然后一起加一个 \(10^{15}\) 避免负环。最后答案减去 \(5\times i\times10^{15}\) 就行了,\(i\) 为现在枚举到的选宝石个数。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INFLL=4e18,INFL=1e15+10;
const int N=505,T=N-1,S1=85,K=85,S2=170,S3=255,INF=2e9;
int hd[N],e_num(1),n,m,x[K],y[K],q[N*N*N],l,r,a[N],b[N],vhd[N],v[N],lx[K],ly[K],rx[K],ry[K],cnt;
char ch[N][5];
LL d[N],ret,ans,vv[N];
struct edge{
int v,nxt,f;
LL w;
}e[N*N*5];
void add_edge(int u,int v,int f,LL w)
{
e[++e_num]=(edge){v,hd[u],f,INFL-w};
hd[u]=e_num;
e[++e_num]=(edge){u,hd[v],0,w-INFL};
hd[v]=e_num;
}
int bfs()
{
memset(d,0x7f,sizeof(d));
memcpy(hd,vhd,sizeof(hd));
v[d[q[l=r=1]=0]=0]=1;
while(l<=r)
{
// printf("%d\n",q[l%N]);
for(int i=hd[q[l]];i;i=e[i].nxt)
{
// printf("%d\n",e[i].v);
if(d[e[i].v]>d[q[l]]+e[i].w&&e[i].f)
{
d[e[i].v]=d[q[l]]+e[i].w;
if(!v[e[i].v])
{
++r;
v[e[i].v]=1,q[r]=e[i].v;
}
}
}
v[q[l]]=0;
++l;
}
// printf("%lld\n",d[T]);,
return d[T]<INFLL;
}
int dfs(int x,int s)
{
if(x==T)
return s;
v[x]=1;
int g;
// printf("%d %d\n",x,s);
for(int&i=hd[x];i;i=e[i].nxt)
{
if(!v[e[i].v]&&e[i].f&&d[e[i].v]==d[x]+e[i].w&&(g=dfs(e[i].v,min(s,e[i].f))))
{
e[i].f-=g;
e[i^1].f+=g;
ans+=e[i].w*g;
v[x]=0;
return g;
}
}
v[x]=0;
return 0;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d%lld",x+i,y+i,vv+i);
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%s%d%d",ch[i],a+i,b+i);
for(int i=1;i<=n;i++)//枚举选多少个珠宝
{
// printf("%d\n",i);
e_num=1;
memset(hd,0,sizeof(hd));
memset(lx,0,sizeof(lx));
memset(rx,0x7f,sizeof(rx));
memset(ly,0,sizeof(ly));
memset(ry,0x7f,sizeof(ry));
for(int j=1;j<=n;j++)
add_edge(S2+j,S3+j,1,vv[j]);
for(int j=1;j<=i;j++)
{
add_edge(0,j,1,0);
add_edge(j+S1,T,1,0);
}
for(int j=1;j<=m;j++)
{
if(ch[j][0]=='L')
for(int k=b[j]+1;k<=i;k++)
lx[k]=max(lx[k],a[j]+1);
if(ch[j][0]=='R')
for(int k=1;k<=i-b[j];k++)
rx[k]=min(rx[k],a[j]-1);
if(ch[j][0]=='D')
for(int k=b[j]+1;k<=i;k++)
ly[k]=max(ly[k],a[j]+1);
if(ch[j][0]=='U')
for(int k=1;k<=i-b[j];k++)
ry[k]=min(ry[k],a[j]-1);
}
// for(int j=1;j<=i;j++)
// printf("%d %d %d %d\n",lx[j],rx[j],ly[j],ry[j]);
// puts("");
for(int j=1;j<=i;j++)
{
for(int k=1;k<=n;k++)
{
if(lx[j]<=x[k]&&x[k]<=rx[j])
add_edge(j,k+S2,1,0);
if(ly[j]<=y[k]&&y[k]<=ry[j])
add_edge(k+S3,j+S1,1,0);
}
}
ans=cnt=0;
memcpy(vhd,hd,sizeof(vhd));
int kk;
while(bfs())
while(kk=dfs(0,INF))
cnt+=kk;
if(cnt==i)
ret=max(ret,i*INFL*5-ans);
}
printf("%lld",ret);
}

[AGC031E] Snuke the Phantom Thief的更多相关文章

  1. 「题解」agc031_e Snuke the Phantom Thief

    本文将同步发布于: 洛谷博客: csdn: 博客园: 简书. 题目 题目链接:洛谷 AT4695.AtCoder agc031_e. 题意简述 在二维平面上,有 \(n\) 颗珠宝,第 \(i\) 颗 ...

  2. HDOJ/HDU 1982 Kaitou Kid - The Phantom Thief (1)(字符串处理)

    Problem Description Do you know Kaitou Kid? In the legend, Kaitou Kid is a master of disguise, and c ...

  3. HDU——1982Kaitou Kid - The Phantom Thief (1)(坑爹string题)

    Kaitou Kid - The Phantom Thief (1) Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/327 ...

  4. 【HDOJ】1983 Kaitou Kid - The Phantom Thief (2)

    不仅仅是DFS,还需要考虑可以走到终点.同时,需要进行预处理.至多封闭点数为起点和终点的非墙壁点的最小值. #include <iostream> #include <cstdio& ...

  5. HDU 1983 Kaitou Kid - The Phantom Thief (2)

    神题,搜索太差,来自网络的题解与程序 思路: 封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四 ...

  6. 【AtCoder】AGC031

    A - Colorful Subsequence 答案是 \(\prod_{c = 'a'}^{'z'} (cnt[c] + 1)\) #include <bits/stdc++.h> # ...

  7. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  8. AtCoder Grand Contest 031题解

    题面 传送门 题解 比赛的之后做完\(AB\)就开始发呆了--简直菜的一笔啊-- \(A - Colorful\ Subsequence\) 如果第\(i\)个字母选,那么它前面任意一个别的字母的选择 ...

  9. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  10. codeforces 632+ E. Thief in a Shop

    E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

随机推荐

  1. C# QRCode二维码的解析与生成

    已知一张二维码图片,怎么生成一张一模一样的图片出来? 最近有个项目,需要用到QRCode,之前只做过Datamatrix格式的,想着应该也是差不多的,于是就依葫芦画瓢,掏出我的陈年OnBarcode类 ...

  2. 论文解读(WDGRL)《Wasserstein Distance Guided Representation Learning for Domain Adaptation》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:Wasserstein Distance Guided Representation Learning f ...

  3. 如何通过抖音订单API接口获取订单详情

    要通过抖音订单API接口获取订单详情,您需要进行以下步骤: 1.获取Access Token:使用APP ID和APP Secret调用获取Access Token API接口来获取您的Access ...

  4. CodeForces-1324E-Sleeping-Schedule

    题意 \(Vova\)有一个睡眠时间表,一天有\(h\)小时,\(Vova\)会睡\(n\)次觉,一次睡一天,在第\(i-1\)次睡醒后,\(Vova\)在\(a_i\)或\(a_i-1\)个小时候可 ...

  5. QA|Pycharm:allure : 无法将“allure”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。|Allure

    Pycharm中生成allure测试报告时报错如图: 单独执行allure --version也不行,cmd这样执行也报同样的错 网上查了 说是环境变量问题,加一下cmd可以了,重启pycharm也可 ...

  6. 使用docker搭建seafile服务器

    工作需要在单位和家里的不同电脑上同步指定文件夹及其内容.对比了一些解决方案,最终还是选择熟悉的seafile来做. 需要按照官方文档进行seafile的安装,选择官方推荐的docker方式快速部署. ...

  7. 【RocketMQ】Rebalance负载均衡总结

    消费者负载均衡,是指为消费组下的每个消费者分配订阅主题下的消费队列,分配了消费队列消费者就可以知道去消费哪个消费队列上面的消息,这里针对集群模式,因为广播模式,所有的消息队列可以被消费组下的每个消费者 ...

  8. Java 21 正式 GA,虚拟线程真的来了

    UTC 时间 2023 年 9 月 19 日,期盼已久的 Java 21 终于发布正式版! 本文一起来看看其中最受 Java 开发者关注的一项新特性:Loom 项目的两个新特性之一的 "虚拟 ...

  9. 模拟.NET应用场景,综合应用反编译、第三方库调试、拦截、一库多版本兼容方案

    免责声明 使用者本人对于传播和利用本公众号提供的信息所造成的任何直接或间接的后果和损失负全部责任.公众号及作者对于这些后果不承担任何责任.如果造成后果,请自行承担责任.谢谢! 大家好,我是沙漠尽头的狼 ...

  10. 数据泵(impdb)导入Oracle分片的数据库dump文件

    数据泵(impdb)导入Oracle数据库 一.sqlplus登录目标数据库,创建导入的目录路径 #该目录要在导入的数据库本机建立,如果是docker就在容器内部创建 create directory ...