二分图最大匹配,枚举。

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

如果二分图最大匹配不到$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. 驱动学习3.1:获取zynqled的物理地址

    自己想要打印EMIO管脚的物理地址,在SDK提供的头文件中加入printf是无法打印的,基于此 我将需要打印地址的几个函数提取出来,放在main函数中,然后在里面加入printf打印这些用户管脚的地址 ...

  2. apache源码安装必须依赖的库apr----/etc/ld.so.conf 文件介绍

    Apache所依赖的库,封装了各个系统相关的API等.虽然都是Apache开发的,但是现在最新版本的Apache和APR源码是分开的.要编Apache就必须使用APR. /etc/ld.so.conf ...

  3. 关于变长数组的一点小想法-C语言定义数组但是数组长度不确定怎么办

    很多数据机构,比如栈,链表等,都可以动态分配存储空间 那么数组呢?一般声明时都要指定数组长度,那么数组可以实现动态分配么? 假设数组存的是int型 那么 你先申请10个元素 int* a = (int ...

  4. JAVA多线程提高七:Callable与Future的应用

    Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { publ ...

  5. HEXO与Github.io搭建个人博客

    HEXO与Github.io搭建个人博客 HEXO搭建    HEXO是基于Node.JS的一款简单快速的博客框架,能够支持多线程,支持markdown,可以将生成的静态网页发布到github.io以 ...

  6. js_beautifier && css_beautifier for emeditor

    // // Unpacker for Dean Edward's p.a.c.k.e.r, a part of javascript beautifier // written by Einar Li ...

  7. 基本控件文档-UIView属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  8. 原 jQuery中document的ready和load事件的区别?

    概述: 大家在工作中用jQuery的时候一定会在使用之前这样:   1 2 3 4 5 6 7 8 //document ready $(document).ready(function(){     ...

  9. thinkphp 5.0 代码执行漏洞

    https://github.com/vulhub/vulhub/blob/master/thinkphp/5-rce docker-compose -f /home/root/compose.yml ...

  10. Linux 添加普通用户到 sudoers 文件

    前言 Linux 的普通用户(uid >= 500)不具有某些命令的执行权限,为了执行较高权限的命令,一般有两种方法: 第一种是使用 su - 命令切换到 root 用户去执行: 另外一种方法是 ...