Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1084 Accepted Submission(s): 418
Problem Description
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
Write a program that:
> reads the number of intervals, their endpoints and integers c1, ..., cn from the standard input,
> computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i = 1, 2, ..., n,
> writes the answer to the standard output
Input
The first line of the input contains an integer n (1 <= n <= 50 000) - the number of intervals. The following n lines describe the intervals. The i+1-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50 000 and 1 <= ci <= bi - ai + 1.
Process to the end of file.
Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i = 1, 2, ..., n.
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Author
1384
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<climits>
#define find_min(a,b) a<b?a:b
#define find_max(a,b) a>b?a:b
using namespace std;
const int N = 50010;
struct Edge
{
int s,e,v;
int next;
}edge[N];
int n,e_num,p_num,vis[N],head[N],dist[N];
int left_x,right_x;
void AddEdge(int a,int b,int c)
{
edge[e_num].s=a;
edge[e_num].e=b;
edge[e_num].v=c;
edge[e_num].next=head[a];
head[a]=e_num++;
}
queue<int>q;
void spfa()
{
while(!q.empty())
{
int cur=q.front();
q.pop();
vis[cur]=0;
for(int i=head[cur];i!=-1;i=edge[i].next){
int u=edge[i].e;
if(dist[u] < dist[cur]+edge[i].v){
dist[u]=dist[cur]+edge[i].v;
if(!vis[u]){
q.push(u);vis[u]=1;
}
}
}
if(cur>left_x && dist[cur]-1 > dist[cur-1]){
dist[cur-1]=dist[cur]-1;
if(!vis[cur-1]){
q.push(cur-1);vis[cur-1]=1;
}
}
if(cur<right_x && dist[cur] > dist[cur+1]){
dist[cur+1]=dist[cur];
if(!vis[cur+1]){
q.push(cur+1);vis[cur+1]=1;
}
}
}
printf("%d\n",dist[right_x]);
}
void getmap(){
int i,a,b,c;
e_num=p_num=1;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
left_x=INT_MAX; right_x=INT_MIN;
for(i=1;i<=n;i++){
scanf("%d%d%d",&a,&b,&c);
AddEdge(a,b+1,c);//可能存在a==b的情况
left_x=find_min(left_x,a);
right_x=find_max(right_x,b+1);
}
//省略掉那个虚点,可以优化spfa.虚点作用是让所有点入队,且dist[]更新为0,
//所以省略掉这点的话,需要手动把所有点入队,dist[]赋初值为0
memset(dist,0,sizeof(dist));
for(i=left_x;i<=right_x;i++){
q.push(i);vis[i]=1;
}
}
int main(){
while(~scanf("%d",&n)){
getmap();
spfa();
}
return 0;
}
King
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6113 Accepted: 2427
Description
Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound king.'' After nine months her child was born, and indeed, she gave birth to a nice son.
Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence.
The old king was very unhappy of his son. But he was ready to make everything to enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some hope that his son would be able to make some decisions.
After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his decisions were wrong.
Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems was in the form of subsequences Si = {aSi, aSi+1, ..., aSi+ni} of a sequence S = {a1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki resp.) and declared these constraints as his decisions.
After a while he realized that some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to his advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not.
Input
The input consists of blocks of lines. Each block except the last corresponds to one set of problems and king's decisions about them. In the first line of the block there are integers n, and m where 0 < n <= 100 is length of the sequence S and 0 < m <= 100 is the number of subsequences Si. Next m lines contain particular decisions coded in the form of quadruples si, ni, oi, ki, where oi represents operator > (coded as gt) or operator < (coded as lt) respectively. The symbols si, ni and ki have the meaning described above. The last block consists of just one line containing 0.
Output
The output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy when such a sequence does not exist. Otherwise it contains text lamentable kingdom. There is no line in the output corresponding to the last ``null'' block of the input.
Sample Input
4 2
1 2 gt 0
2 2 lt 2
1 2
1 0 gt 0
1 0 lt 0
0
Sample Output
lamentable kingdom
successful conspiracy
//差分约束系统
//题意相当晦涩,看了好半天才大概知道个什么
//对题目中给定的si,ni,ki,和一个给定的序列S[1....N]
//如果(si,ni,gt,ki),意思就是存在约束条件S[si]+S[si+1]+...S[si+ni] > ki
//如果(si,ni,lt,ki),意思就是存在约束条件S[si]+S[si+1]+...S[si+ni] < ki
//判断所给的约束条件有无解,有解就输出lamentable kingdom,无解就输出successful conspiracy
//为了将这些约束条件转化为差分约束,不妨设T[x] = S[1]+S[2]+....S[x];
//那么上面两条式子就可以转化为
//T[si+ni] - T[si-1] > ki
//T[si+ni] - T[si-1] < ki
//又差分约束系统的求解只能针对>=或<=,无法求解>的系统,还好ki是整数,可以很容易将<转化为<=
//T[si-1] - T[si+ni] <= -ki - 1
//T[si+ni] - T[si-1] <= ki - 1
//剩下的就是普通的差分约束系统的求解了,A了这道题,学会了一种无需建立超级源点的SPFA解法
//在SPFA开始时将所有结点都放进队列,这样表示一开始和所有点都相连了,初始化dis数组为全0,相当于超级源点的边权值
//总结:1、小于和小于等于关系的转化 2、超级源点的另一种建法
#include<iostream>
#include<queue>
#include<memory.h>
#include<stdio.h>
#define MAXN 105
#define MAXM 400
using namespace std;
bool inq[MAXN];
int dis[MAXN],cnt[MAXN];
int head[MAXN],V[MAXM],W[MAXM],next[MAXM];
int m,N,M,si,ni,ki;
char o[2];
void addEdge(int u,int v,int w)
{
V[m] = v;
W[m] = w;
next[m] = head[u];
head[u] = m++;
}
void buildGraph()
{
m = 0;
memset(head,-1,sizeof(head));
while(M--)
{
scanf("%d%d%s%d",&si,&ni,o,&ki);
if(o[0] == 'g')
addEdge(si+ni,si-1,-ki-1);
else
addEdge(si-1,si+ni,ki-1);
}
}
bool SPFA()
{
queue<int> q;
memset(inq,true,sizeof(inq));
memset(cnt,0,sizeof(cnt));
memset(dis,0,sizeof(dis));//初始化dis全0
for(int i = 0;i <= N;++i) q.push(i);//把所有结点放进队列中
while(!q.empty())
{
int u = q.front(); q.pop();
inq[u] = 0;
for(int e = head[u];e != -1;e = next[e])
{
if(dis[u] + W[e] < dis[V[e]])
{
dis[V[e]] = dis[u] + W[e];
if(!inq[V[e]])
{
q.push(V[e]);
inq[V[e]] = 1;
if(++cnt[V[e]] > N) return 0;//注意这里的入队次数,不包括一开始把所有结点塞进队列的那一次
}
}
}
}
return 1;//当前差分约束系统有解
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d",&N)!=EOF)
{
if(N==0)
break;
scanf("%d",&M);
buildGraph();
if(SPFA()) printf("lamentable kingdom\n");
else printf("successful conspiracy\n");
}
return 0;
}
另一种解法
#include<stdio.h>
#include<string.h>
#define MAXN 105
struct node
{
int s;
int e;
int v;
}a[MAXN];
int n,m,s[MAXN];
int bellman_ford()
{
int i,j,flag;
memset(s,127,sizeof(s));
flag=1;
for(i=0;i<=n&&flag;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(s[a[j].e]>s[a[j].s]+a[j].v)
{
s[a[j].e]=s[a[j].s]+a[j].v;
flag=1;
}
}
}
return flag;
}
int main()
{
int i,j,si,ni,ki,t;
char oi[4];
while(scanf("%d",&n)==1)
{
if(!n)
break;
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d%d%s%d",&si,&ni,oi,&ki);
if(oi[0]=='g')
{
a[i].s=si-1;
a[i].e=ni+si;
a[i].v=-ki-1;
}
else
{
a[i].s=ni+si;
a[i].e=si-1;
a[i].v=ki-1;
}
}
t=bellman_ford();
if(!t)
printf("lamentable kingdom\n");
else
printf("successful conspiracy\n");
}
return 0;
}