World Exhibition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1957 Accepted Submission(s): 971
There is something interesting. Some like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of X (1 <= X <= 10,000) constraints describes which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between person 1 and person N that satisfies the distance constraints.
The next line: Three space-separated integers: N, X, and Y.
The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.
The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.
题意:数轴上有n个点,给出这n个点的几个约束--其中x个:a1点和b1点最大距离小于c1,y个:a2点和b2点的最小距离大于c2,求1和n的最大距离。
思路:差分约束模板题。
将b-a<=c转换为b<=a+c,建一条从a到b的价值为c的边,b-a>=c转换为a<=b-c,建一条从b到a的价值为-c的边。这样1到n的最大距离转换为图中1到n的最短路。
基于spfa的差分约束。
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=300001;
const int inf =0x7ffffff;
struct edge
{
int from,to,w,next;
}e[1000001];
int head[maxn];
int vis[maxn];
int dist[maxn];
int n,x,y,cnt;
int num[maxn];
bool flag;
void add(int i,int j,int w)
{
e[cnt].from=i;
e[cnt].to=j;
e[cnt].w=w;
e[cnt].next=head[i];
head[i]=cnt++;
}
void spfa(int s)
{
queue <int> q;
for(int i=1;i<=n;i++)
dist[i]=inf;
memset(vis,false,sizeof(vis));
q.push(s);
dist[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(dist[v]>dist[u]+e[i].w)
{
dist[v]=dist[u]+e[i].w;
if(!vis[v])
{
vis[v]=true;
q.push(v);
if(++num[v]>n)
{
flag=false;
return;
}
}
}
}
}
}
int main()
{
int a,b,c,s,e;
int t;
scanf("%d",&t);
while(t--)
{
memset(num,0,sizeof(num));
flag=true;
scanf("%d%d%d",&n,&x,&y);
cnt=0;
memset(head,-1,sizeof(head));
while(x--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
while(y--)
{
scanf("%d%d%d",&a,&b,&c);
add(b,a,-c);
}
s=1;e=n;
spfa(s);
if(!flag)printf("-1\n");
else if(dist[e]==inf) printf("-2\n");
else printf("%d\n",dist[e]);
}
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15469 | Accepted: 6550 |
Description
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
Output
Sample Input
4 3 6 2 4 0 2 4 7
Sample Output
4
题意:给定n个区间,现在要求你求出一个最小整数集合,满足所有给出的区间中至少包含集合中两个元素。
思路:差分约束。设s[i]表示前[0,i]区间内包含的集合中元素数,那么对于区间[a,b]可以得出s[b]>=s[a-1]+2,另外又有s[i-1]>=s[i]-1,s[i]>=s[i-1]。
然后同hdu3592一样建边,但是由于这次为大于号,所以应当求最长路,所以把边值取反,再利用spfa,最后结果取反就好。
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=10005;
const int inf =0x7ffffff;
struct edge
{
int from,to,w,next;
}e[1000001];
int head[maxn];
int vis[maxn];
int dist[maxn];
int n,m,cnt,r;
void add(int i,int j,int w)
{
e[cnt].from=i;
e[cnt].to=j;
e[cnt].w=w;
e[cnt].next=head[i];
head[i]=cnt++;
}
void spfa(int s)
{
queue <int> q;
for(int i=1;i<=10001;i++)
dist[i]=inf;
memset(vis,false,sizeof(vis));
q.push(s);
dist[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(dist[v]>dist[u]+e[i].w)
{
dist[v]=dist[u]+e[i].w;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
}
int main()
{
int a,b,s,e;
scanf("%d",&m);
cnt=0;
memset(head,-1,sizeof(head));
r=0;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b+1,-2);
r=max(r,b+1);
}
for(int i=1;i<=r;i++)
{
add(i,i-1,1);
add(i-1,i,0);
}
s=0;e=r;
spfa(s);
printf("%d\n",-dist[e]);
}
DreamGrid is learning the LIS (Longest Increasing Subsequence) problem and he needs to find the longest increasing subsequence of a given sequence of length .
Recall that
-
A subsequence of length is a sequence satisfying and .
-
An increasing subsequence is a subsequence satisfying .
DreamGrid defines the helper sequence where indicates the maximum length of the increasing subsequence which ends with . In case you don't know how to derive the helper sequence, he provides you with the following pseudo-code which calculates the helper sequence.
procedure lis_helper(: original sequence)
{Let be the length of the original sequence,
be the -th element in sequence , and
be the -th element in sequence }
for := 1 to
:= 1
for := 1 to ( - 1)
if and
:= + 1
return { is the helper sequence}
DreamGrid has derived the helper sequence using the program, but the original sequence is stolen by BaoBao and is lost! All DreamGrid has in hand now is the helper sequence and two range sequences and indicating that for all .
Please help DreamGrid restore the original sequence which is compatible with the helper sequence and the two range sequences.
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the length of the original sequence.
The second line contains integers () seperated by a space, indicating the helper sequence.
For the following lines, the -th line contains two integers and (), indicating the range sequences.
It's guaranteed that the original sequence exists, and the sum of of all test cases will not exceed .
Output
For each test case output one line containing integers separated by a space, indicating the original sequence. If there are multiple valid answers, print any of them.
Please, DO NOT print extra spaces at the end of each line, or your solution may be considered incorrect!
Sample Input
4 6 1 2 3 2 4 3 0 5 2 4 3 3 1 2 3 5 1 5 5 1 2 1 3 1 100 200 200 300 200 400 400 500 100 500 7 1 2 3 1 1 4 2 0 3 0 3 0 3 0 3 0 3 0 3 0 3 2 1 1 1 2 2 3
Sample Output
1 2 3 2 5 3 200 300 200 500 200 0 1 2 0 0 3 1 2 2
题意:给你一组最长上升子序列的dp区间值,然后给你每个点的取值范围,求可行的原序列 首先满足l[i] <= a[i] <= r[i], 可以建一个源点 n+1,那么有 a[n+1] - a[i] <= -l[i], a[i] - a[n+1] <= r[i] 对于之前出现过a[i]的点 j,要使a[i]>=a[j](保证没法转移), 有 a[j] - a[i] <= 0 还有对于从上一个a[i]-1转移过来的点 j, 要使a[i] > a[j],有 a[j] - a[i] <= -1 跑一边最短路,d[i] 即为所求
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=100005;
const ll inf =0x7f7f7f7f;
struct edge
{
int to,next;
ll w;
}e[1000001];
int head[maxn];
int vis[maxn];
ll dist[maxn];
int n,x,y,cnt;
int last[100005];
bool flag;
void add(int i,int j,ll w)
{
// e[cnt].from=i;
e[cnt].to=j;
e[cnt].w=w;
e[cnt].next=head[i];
head[i]=cnt++;
}
void spfa(int s)
{
queue <int> q;
for(int i=1;i<=n;i++)
dist[i]=inf;
memset(vis,false,sizeof(vis));
q.push(s);
dist[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(dist[v]>dist[u]+e[i].w)
{
dist[v]=dist[u]+e[i].w;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
}
int main()
{
int s;
int t;
scanf("%d",&t);
while(t--)
{
cnt=0;
memset(head,-1,sizeof(head));
memset(last,0,sizeof(last));
flag=true;
scanf("%d",&n);
s=n+1;
int a;
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
if(last[a])
{
add(last[a],i,0);
}
if(a)
{
add(i,last[a-1],-1);
}
last[a]=i;
}
ll l,r;
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&l,&r);
add(i,s,-l);
add(s,i,r);
}
spfa(s);
for(int i=1;i<n;i++)printf("%lld ",dist[i]);
printf("%lld\n",dist[n]);
}
}