二分图最大匹配,枚举。

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

如果二分图最大匹配不到$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. switch滑动开关

    <!DOCTYPE html> <html> <head > <meta charset="utf-8"> <title> ...

  2. org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined

    请检查你在web.xml中加载spring.xml文件的时候没有加载成功,看你的路径是否正确 <context-param>  <param-name>contextConfi ...

  3. Java集合框架(list,Queue)

    List和Queue都继承自Collection接口 list常规用法 List判断两个对象相等的标准:equals方法返回true class A2 { public boolean equals( ...

  4. Java设计模式の模版方法模式

    概述 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的 ...

  5. Python学习笔记(四十三)virtualenv (创建一套“隔离”的Python运行环境)

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432712108 ...

  6. Try finally的一个实验和为什么避免重载 finalize()方法--例子

    public class TryFinallTest { public TryFinallTest(){ } public void runSomething(String str){ System. ...

  7. 旅游(CSUST省赛选拔赛2+状压dp+最短路)

    题目链接:http://csustacm.com:4803/problem/1016 题目: 思路:状压dp+最短路,比赛的时候有想到状压dp,但是最短路部分写挫了,然后就卡死了,对不起出题人~dis ...

  8. Hibernate总结之常用API

    1. Configuration Configuration是用来读取配置文件,从配置文件中构件SessionFactory的. SessionFactory sessionFactory=new C ...

  9. 59、有用过with statement吗?它的好处是什么?

    python中的with语句是用来干嘛的?有什么作用? with语句的作用是通过某种方式简化异常处理,它是所谓的上下文管理器的一种 用法举例如下: with open('output.txt', 'w ...

  10. C# 操作资源文件

    (1)首先引用这两个命名空间 (2)两种方式调用资源文件中的内容 private void button2_Click(object sender, EventArgs e) { //通过Resour ...