bzoj3389:[usaco2004 dec]cleaning shifts arrangement on duty

Source: Internet
Author: User
Tags cmath

Idea: can be greedy, can also shortest way.

Greedy writing: Because in the guarantee of legal premise, we choose the interval must be the right end point as far as possible, so we choose a legitimate and right end of the next interval is good (if there is no legitimate output-1 can). Time complexity O (NLOGN) (sort is nlogn, greed is O (n)).

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm>using namespace std; #define MAXN 25005 int n,t,ans;int last[1000005];    struct node{int l,r; BOOL operator < (const node &a) Const{return l<a.l| | (L==A.L&AMP;&AMP;R&LT;A.R);}} A[MAXN];    inline int read () {int X=0;char ch=getchar (); for (;ch< ' 0 ' | |    Ch> ' 9 '; Ch=getchar ());    for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x=x*10+ch-' 0 '; return x;}    int main () {N=read (), T=read ();    for (int i=1;i<=n;i++) A[i].l=read (), A[i].r=read ();    Sort (a+1,a+n+1); int cnt=0;    for (int i=1;i<=n;i++) if (A[I].L!=A[I+1].L) a[++cnt]=a[i];    N=cnt;int now=0;        for (int i=1;i<=n;i++) {int X=0;bool flag=0;        while (a[i].l<=now+1&&i<=n) X=max (X,A[I].R), i++,flag=1;        if (!flag) {ans=-1;break;}        if (X>now) now=x,ans++;    i--;    } if (now!=t) ans=-1;    printf ("%d\n", ans); return 0;}

The shortest way to do this: interval [l,r] means you can go from l-1 to R, then we will l-1 a weight of 1 to the edge to r, and then because the interval can have intersection, so I also need to i-1 a weight of 0 edge, Then run the shortest path with 0 as the starting point (starting with 0 because it is L-1-R). Time complexity O (TLOGT)

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Queue> #include <cmath>using namespace std; #define MAXN 1000005#define inf 1e9 int N,t,tot;int now[maxn],pre[ Maxn*2],son[maxn*2],val[maxn*2],dis[maxn];bool VIS[MAXN];    inline int read () {int X=0;char ch=getchar (); for (;ch< ' 0 ' | |    Ch> ' 9 '; Ch=getchar ());    for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x=x*10+ch-' 0 '; return x;}    void Add (int a,int b,int c) {son[++tot]=b;    Pre[tot]=now[a];    Now[a]=tot; Val[tot]=c;}    void link (int a,int b,int c) {Add (a,b,c);} struct node{int id,val;    Node () {} node (int a,int b) {id=a,val=b;} BOOL operator < (const node &a) Const{return val>a.val;}}; Priority_queue<node> Heap;    void Dijkstra (int x) {memset (dis,127,sizeof (DIS)), dis[x]=0;    Heap.push (Node (x,0));        while (!heap.empty ()) {node x=heap.top (); Heap.pop (); int id=x.id,v=x.val;        if (Vis[id]) continue;vis[id]=1; for (iNT P=now[id];p; p=pre[p]) if (Dis[son[p]]>v+val[p]) Heap.push (node (son[p],dis[son[p]]=v+val[p]));    }} int main () {N=read (), T=read ();    for (int i=1,a,b;i<=n;i++) A=read (), b=read (), Link (a-1,b,1);    for (int i=1;i<=t;i++) Link (i,i-1,0);    Dijkstra (0);    if (DIS[T]&GT;1E9) puts ("1"), Else printf ("%d\n", dis[t]); return 0;}

bzoj3389:[usaco2004 dec]cleaning shifts scheduled on duty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.