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入门-025-数组-重中之重
数组是PHP中一个 很很很很很很很很很很重要 的一个 数据类型 . 学习数组,大家主要学习两部份的知识: 1.数组的定义,定义中的一些注意的坑 2.数组的函数使用 认识数组 数组定义 数组在之前我们让 ...
- Vue.js随笔二(新建路由+component+添加样式+变量的显示)
创建一个页面: 1.首先让我们看一下整个vue.js的目录,如下图所示: 2.现在让我们创建一个页面吧: 2-1首先你需要新建路由(就和建立一个如何找到项目文件的目录一个意思):进入src/route ...
- NOIP模拟赛8
今天又爆零啦... T1 题目描述 #define goodcatdog gcd #define important i #define judge j 神说 每个梦想就是一轮月亮,高高地孤寂地挂在 ...
- HDU 2619 完全剩余类 原根
求有多少$i(<=n-1)$,使 $x^i \mod n$的值为$[1,n-1]$,其实也就是满足完全剩余类的原根数量.之前好像在二次剩余的讲义PPT里看到这个过. 直接有个定理,如果模k下有 ...
- 26 THINGS I LEARNED IN THE DEEP LEARNING SUMMER SCHOOL
26 THINGS I LEARNED IN THE DEEP LEARNING SUMMER SCHOOL In the beginning of August I got the chance t ...
- Spark Core 资源调度与任务调度(standalone client 流程描述)
Spark Core 资源调度与任务调度(standalone client 流程描述) Spark集群启动: 集群启动后,Worker会向Master汇报资源情况(实际上将Worker的资 ...
- linux系统df和du命令的区别
发现一台用户的电脑,df检查出来的/磁盘空间占用了16G,比用du查看得到的磁盘空间大的多,du查看/下所有程序目录加起来还不到5G.这是什么原因呢? 即便是有隐藏文件,查了也很小啊. 因为df和 ...
- Javascript的执行过程详细研究
下面我们以更形象的示例来说明JavaScript代码在页面中的执行顺序.如果说,JavaScript引擎的工作机制比较深奥是因为它属于底层行为,那么JavaScript代码执行顺序就比较形象了,因为我 ...
- 【BZOJ】4129: Haruna’s Breakfast 树分块+带修改莫队算法
[题意]给定n个节点的树,每个节点有一个数字ai,m次操作:修改一个节点的数字,或询问一条树链的数字集合的mex值.n,m<=5*10^4,0<=ai<=10^9. [算法]树分块+ ...
- $.when()方法翻译2
mac不知道为何,文章字数一多,浏览器就重启.只好分开写了. In the event a Deferred was resolved with no value, the corresponding ...