SCU 4443 Range Query
二分图最大匹配,枚举。
可以计算出每一个位置可以放哪些数字,每个数字可以放在哪些位置,这样就可以建二分图了。
如果二分图最大匹配不到$n$,则无解。否则构造字典序最小的解,可以枚举每一位放什么数字,然后再判断是否有解。
#include<bits/stdc++.h>
using namespace std; const int maxn=+;
int n,m1,m2;
int pL[],pR[],nL[],nR[];
int f,ans[],cun[],u[][]; const int INF = 0x7FFFFFFF;
struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){}
};
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int s, t; void init()
{
for (int i = ; i < maxn; i++) G[i].clear();
edges.clear();
}
void Addedge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int w = edges.size();
G[from].push_back(w - );
G[to].push_back(w - );
}
bool BFS()
{
memset(vis, , sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a)
{
if (x == t || a == )
return a;
int flow = , f;
for (int &i = cur[x]; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
edges[G[x][i]].flow+=f;
edges[G[x][i] ^ ].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[x] = -;
return flow;
}
int dinic(int s, int t)
{
int flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} int main()
{
while(~scanf("%d%d%d",&n,&m1,&m2))
{
f=; memset(u,,sizeof u);
for(int i=;i<=n;i++) nL[i]=pL[i]=,nR[i]=pR[i]=n; for(int i=;i<=m1;i++)
{
int a,b,c; scanf("%d%d%d",&a,&b,&c);
for(int j=a;j<=b;j++) pL[j]=max(c,pL[j]);
nL[c] = max(nL[c],a);
nR[c] = min(nR[c],b);
} for(int i=;i<=m2;i++)
{
int a,b,c; scanf("%d%d%d",&a,&b,&c);
for(int j=a;j<=b;j++) pR[j]=min(c,pR[j]);
nL[c] = max(nL[c],a);
nR[c] = min(nR[c],b);
} for(int i=;i<=n;i++)
{
if(nL[i]>nR[i]) f=;
if(pL[i]>pR[i]) f=;
} if(f==)
{
printf("-1\n");
continue;
} init(); s=, t=*n+; for(int i=;i<=n;i++) Addedge(s,i,), Addedge(i+n,t,); for(int i=;i<=n;i++)
for(int j=pL[i];j<=pR[i];j++)
if(nL[j]<=i&&i<=nR[j]) Addedge(i,j+n,), u[i][j]=; int pi = dinic(s,t); if(pi!=n)
{
printf("-1\n");
continue;
} memset(cun,,sizeof cun);
for(int pos=;pos<=n;pos++)
{
for(int num=;num<=n;num++)
{
if(cun[num]) continue; if(u[pos][num]==) continue; init();
s=, t=*n+;
for(int i=;i<=n;i++)
{
if(i>pos) Addedge(s,i,);
if(cun[i]==&&i!=num) Addedge(i+n,t,);
} for(int i=pos+;i<=n;i++)
for(int j=pL[i];j<=pR[i];j++)
if(nL[j]<=i&&i<=nR[j])
if(cun[j]==&&j!=num) Addedge(i,j+n,); pi = dinic(s,t);
if(pi==n-pos)
{
ans[pos]=num;
cun[num]=;
break;
}
}
} for(int i=;i<=n;i++)
{
printf("%d",ans[i]);
if(i<n) printf(" ");
else printf("\n");
}
}
return ;
}
SCU 4443 Range Query的更多相关文章
- 第十五届四川省省赛 SCU - 4443 Range Query
先给你1~N的N个数 再给你每种最多50个的条件(ai,bi,ci) 或者[ai,bi,ci] (ai,bi,ci)表示下标ai到bi的最小值必为ci [ai,bi,ci]表示下标ai到bi的最大值必 ...
- elasticsearch term 查询二:Range Query
Range Query 将文档与具有一定范围内字词的字段进行匹配. Lucene查询的类型取决于字段类型,对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRang ...
- SuRF : Practical Range Query Filtering with Fast Succinct Tries
1. Introduction 在数据库管理系统中查找某些关键字会导致很大的磁盘I/O开销,针对这一问题,通常会使用一个内存开销小并且常驻内存的过滤器来检测该关键字是否存.比如现在常用的bloom过滤 ...
- 【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query
A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper- ...
- How to write date range query in Nest ElasticSearch client?
Looking at the source code, there are two overloads of the OnField method. When I use the the that t ...
- SuRF: Practical Range Query Filtering with Fast Succinct Tries 阅读笔记
SuRF(Succinct Range Filter)是一种快速而紧凑的过滤器,同时支持点查询和范围查询(包括开区间查询.闭区间查询.范围计数),可以在RocksDB中用SuRF来替换Bloom过滤器 ...
- 307. Range Sum Query - Mutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- Query DSL for elasticsearch Query
Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsea ...
- 1.7.4 Query Syntax and Parsing
1. 查询语法和解析 这部分主要说明了如何指定被使用的查询解析器.同样描述了主查询解析器的支持的语法和功能.同时还描述了在特定环境下使用的其他查询解析器.这里有一些普通查询解析器都能使用的参数,将会在 ...
随机推荐
- 前端PHP入门-023-重点日期函数之程序执行时间检测
我们有的时经常需要做程序的执行时间执行效率判断. 实现的思路如下: <?php //记录开始时间 //记录结整时 // 开始时间 减去(-) 结束时间 得到程序的运行时间 ?> 可是大家不 ...
- mysql主从同步碰到的问题
一.mysql 安装https://www.cnblogs.com/jxrichar/p/9248480.html二.主从配置参考https://www.cnblogs.com/superfat/p/ ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- GridControl详解(二)表格的列名配置
点击Run Designer控件上的按钮,弹出视图设计窗口: 列配置: 我们配置完列名后,会发现设计视图发生了变化:
- 51nod1110 距离之和最小 V3
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * 权值.求X轴上 ...
- oschina ios开发学习
应该跟android版的类似,例如服务器端在oschina-prefix.pch里 #define api_news_list @"http://www.oschina.net/action ...
- 【转载】iPhone系统概览
iPhone OS OverviewiPhone系统概览iPhone OS comprises the operating system and technologies that you use t ...
- java规范之checkstyle
1. 概述 随着中心的代码规范的建立和实施,项目组对代码规范要求,以及软件工程师们对自身代码的编写规范重要性的认知,“代码规范”已经成为了中心的一个“热词”.然后怎么才能写出有规范的代码,怎么才能养成 ...
- Tinyos 第三版Make系统
1.make系统安装 cd tools ./Bootstrap ./configure make sudo make install 2.make系统结构 3.第三版Makerules文件部分解析 # ...
- linux 查看内存和cpu占用比较多的进程
1.可以使用一下命令查使用内存最多的10个进程 ps -aux | sort -k4nr | head -n 102. 可以使用一下命令查使用CPU最多的10个进程 ps ...