二分图最大匹配,枚举。

可以计算出每一个位置可以放哪些数字,每个数字可以放在哪些位置,这样就可以建二分图了。

如果二分图最大匹配不到$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的更多相关文章

  1. 第十五届四川省省赛 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的最大值必 ...

  2. elasticsearch term 查询二:Range Query

    Range Query 将文档与具有一定范围内字词的字段进行匹配. Lucene查询的类型取决于字段类型,对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRang ...

  3. SuRF : Practical Range Query Filtering with Fast Succinct Tries

    1. Introduction 在数据库管理系统中查找某些关键字会导致很大的磁盘I/O开销,针对这一问题,通常会使用一个内存开销小并且常驻内存的过滤器来检测该关键字是否存.比如现在常用的bloom过滤 ...

  4. 【题解】【数组】【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- ...

  5. 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 ...

  6. SuRF: Practical Range Query Filtering with Fast Succinct Tries 阅读笔记

    SuRF(Succinct Range Filter)是一种快速而紧凑的过滤器,同时支持点查询和范围查询(包括开区间查询.闭区间查询.范围计数),可以在RocksDB中用SuRF来替换Bloom过滤器 ...

  7. 307. Range Sum Query - Mutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  8. Query DSL for elasticsearch Query

    Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsea ...

  9. 1.7.4 Query Syntax and Parsing

    1. 查询语法和解析 这部分主要说明了如何指定被使用的查询解析器.同样描述了主查询解析器的支持的语法和功能.同时还描述了在特定环境下使用的其他查询解析器.这里有一些普通查询解析器都能使用的参数,将会在 ...

随机推荐

  1. OpenCV---高斯模糊(均值模糊的另一种)

    高斯分布: 高斯模糊的原理 一:图像产生高斯噪声循环代码实现(耗时) def clamp(pv): #使我们的随机值在0-255之间 : : return pv import cv2 as cv im ...

  2. ECNA-A- Abstract Art

    题目描述 Arty has been an abstract artist since childhood, and his works have taken on many forms. His l ...

  3. NOIP模拟5

    期望得分:100+100+100=300 实际得分:72+12+0=84 T1  [CQOI2009]中位数图 令c[i]表示前i个数中,比d大的数与比d小的数的差,那么如果c[l]=c[r],则[l ...

  4. CF839 C 树形DP 期望

    给一颗树,求从根出发路径长度的期望是多少. 树形DP 要想清楚期望的计算 /** @Date : 2017-08-12 23:09:41 * @FileName: C.cpp * @Platform: ...

  5. 《JavaScript 实战》:实现图片幻滑动展示效果

    滑动展示效果主要用在图片或信息的滑动展示,也可以设置一下做成简单的口风琴(Accordion)效果.这个其实就是以前写的图片滑动展示效果的改进版,那是我第一篇比较受关注的文章,是时候整理一下了. 有如 ...

  6. 【转】CentOS7 yum方式配置LAMP环境

    采用Yum方式搭建: Apache+Mysql+PHP环境 原文地址: http://www.cnblogs.com/zutbaz/p/4420791.html 1.安装Apache yum inst ...

  7. Django之Form组件验证

    今天来谈谈Django的Form组件操作 Django中的Form一般有两种功能: ·输入html ·验证用户输入 Form验证流程 ·定义规则(是一个类)    ·前端把数据提交过来 ·匹配规则 · ...

  8. struts2的action类详解

    Action类的书写方式 方式1

  9. 一个JAVA渣渣的校招成长记,附BAT美团网易等20家面经总结

    欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享美文,分享各种Java学习资源,面试题,以及企业级Java实战项目回复关键字免费领取): 今天分享一篇牛客网上的一个 ...

  10. perl6 Socket: 发送HTTP请求

    sub MAIN(Str $host,Str $path, Int $port) { my $send = "GET $path HTTP/1.1\r\nHost: $host\r\n\r\ ...