POJ 3683. Priest John's Busiest Day

9 篇文章 0 订阅
2 篇文章 0 订阅

Description

John is the only priest in his town. September 1st is the John’s busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. The i-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must be either at the beginning or the ending of the wedding (i.e. it must be either from Si to Si + Di, or from Ti - Di to Ti). Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.

Note that John can not be present at two weddings simultaneously.

Input

The first line contains a integer N ( 1 ≤ N ≤ 1000).
The next N lines contain the Si, Ti and Di. Si and Ti are in the format of hh:mm.

Output

The first line of output contains “YES” or “NO” indicating whether John can be present at every special ceremony. If it is “YES”, output another N lines describing the staring time and finishing time of all the ceremonies.

Sample Input

2
08:00 09:00 30
08:15 09:00 20

Sample Output

YES
08:00 08:30
08:40 09:00

Solution

  • 可以选择 [s,s+d] [td,t] 两个区间。

  • 那么我们枚举两个区间,判断一下是否冲突并连边。

  • 之后用 tarjan 跑一下 2-SAT 即可。

  • 时间复杂度 O(N2)

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
const int N=2002,M=N*N;
int n,m=1,tot,top,num;
int first[N],next[M],en[M];
int dfn[N],low[N],stack[N];
int f[N],s[N],t[N];
bool bz[N];
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
inline void write(int x)
{
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
inline void writeln(int x,char c)
{
    if(x/60<10) putchar('0');
    write(x/60),putchar(':');
    if(x%60<10) putchar('0');
    write(x%60),putchar(c);
}
inline int min(int x,int y)
{
    return x<y?x:y;
}
inline bool check(int x,int y)
{
    return s[x]<t[y] && t[x]>s[y];
}
inline void insert(int x,int y)
{
    next[++tot]=first[x];
    first[x]=tot;
    en[tot]=y;
}
void tarjan(int x)
{
    dfn[x]=low[x]=++tot;
    bz[stack[++top]=x]=true;
    for(int i=first[x];i;i=next[i])
        if(!dfn[en[i]])
        {
            tarjan(en[i]);
            low[x]=min(low[x],low[en[i]]);
        }else
            if(bz[en[i]]) low[x]=min(low[x],dfn[en[i]]);
    if(dfn[x]==low[x])
    {
        int y;num++;
        do
        {
            f[y=stack[top--]]=num;
            bz[y]=false;
        }while(y^x);
    }
}
int main()
{
    int n=read();
    for(int i=1;i<=n;i++)
    {
        int x=read(),x1=read(),y=read(),y1=read(),d=read();
        x=x*60+x1,y=y*60+y1;
        if(x+d>y) return 0&puts("NO");
        s[++m]=x,t[m]=x+d;
        s[++m]=y-d,t[m]=y;
    }
    for(int i=2;i<m-2;i+=2) for(int i1=0;i1<2;i1++)
        for(int j=i+2;j<m;j+=2) for(int j1=0;j1<2;j1++)
                if(check(i^i1,j^j1))
                {
                    insert(i^i1,j^j1^1);
                    insert(j^j1,i^i1^1);
                }
    tot=0;
    for(int i=2;i<=m;i++)
        if(!dfn[i]) tarjan(i);
    bool pd=false;
    for(int i=2;i<m;i+=2)
        if(f[i]==f[i^1])
        {
            pd=true;
            break;
        }
    if(pd) puts("NO"); else
    {
        puts("YES");
        for(int i=2;i<m;i+=2)
        {
            int j=f[i]<f[i^1]?i:i^1;
            writeln(s[j],' ');
            writeln(t[j],'\n');
        }
        putchar('\n');
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值