Swust ACM Training Questions hdu1384 && hdu3666 && hdu 4786 &&uva 1395 && UVA 1151

Source: Internet
Author: User
Tags min

Training topic URL: Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=99765#problem/A Password: acm2015

B-intervals (hdu1384)

Test instructions: give you a range of n intervals of [l,r], allowing you to determine num integers so that num integers have at least a common number of CI in the first interval [Li,ri]. The topic first gives you a number n, the next n line tells you three number li,ri,ci, the minimum value of the output num. n<=50000,0<=li,ri<=50000,1<=ci<=ri-li+1;

Analysis: Because the interval is the largest to 50000, in 0 to 50000 of these numbers, we use 0 to indicate that the number is not selected, 1 to select this number, then you can use Sum[i] to indicate how many 1 between 0~i (straightforward is to choose a few numbers, can be generally regarded as 0~i distance), according to the title described , you can get the following relationships:

(1) Sum[bi]-sum[ai-1]>=ci, of which ai,bi represents the interval [Ai,bi]

(2) 0<=sum[i]-sum[i-1]<=1

namely: sum[i]-sum[i-1]>=0

Sum[i-1]-sum[i]>=-1

Know the above inequalities, you can build, ai-1 to BI has a value of CI, I to i-1 has a value of 1 edge, i-1 to I have a value of 0 edge.

This question code://Because this problem has no ring situation, so do not have to sentence ring also line

#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> #define MAXN 50010 #
Define INF 1e9 using namespace std;
int num,p[maxn],n; struct node {int en,va,next;}
E[MAXN*10];
    void Init () {num=0;
memset (P,-1,sizeof (p));
    } void Add (int st,int en,int va) {e[num].en=en;
    E[num].va=va;
    E[NUM].NEXT=P[ST];
p[st]=num++;
} int DIS[MAXN];
BOOL INQ[MAXN];
        void Spfa (int st,int en) {for (int i=0;i<=en;i++) {dis[i]=-inf;
    Inq[i]=false;
    } queue<int> Q;
    Q.push (ST);
    dis[st]=0;
    Inq[st]=true;
        while (Q.size ()) {int X=q.front ();
        Q.pop ();
        Inq[x]=false;
            for (int i=p[x];i!=-1;i=e[i].next) {int y=e[i].en;
            int Len=dis[x]+e[i].va;
                if (Len>dis[y]) {Dis[y]=len;
                    if (!inq[y]) {Q.push (y);
            Inq[y]=true;    }}}}} int main () {while (scanf ("%d", &n) ==1) {init ();
            int st=inf,en=-1;//Because the starting point is unknown, so you have to find out for (int i=1;i<=n;i++) {int a,b,c;
            scanf ("%d%d%d", &a,&b,&c); a++,b++;///because the minimum value may be 0, so prevent my starting point is negative st=min (st,a-1), or///to join the minimum value of Minn, then our starting point is minn-1, so directly record the minimum value of A-1 as the starting point en=
            Max (EN,B);
        Add (A-1,B,C);
            } for (int i=st;i<=en;i++) {Add (i,i-1,-1);
        Add (i-1,i,0);
        } SPFA (St,en);
    printf ("%d\n", Dis[en]);
} return 0;
 }

C-the MATRIX problem (Hdu 3666)

Test instructions: give you a n*m matrix, and an interval upper and lower bounds l,u, ask if you can find the N number a1,a2,a3,......, an and m number b1,b2,b3,......, BM, so that the number of line I in the matrix multiplied by the AI to get the value range in [L,u], And the number of the column J in the Matrix divided by BJ to get the value range also in [L,u], can find you output yes, otherwise no.

Enter first behavior N,m,l,u

The next n*m matrix AIJ

Analysis: To do the problem of differential constraint first we have to find a relationship (two indefinite number subtraction is greater than or less than or equal to a certain value). According to test instructions we can simply get the following relationship:

(1) L<=aij * AI <=u (first request)

(2) L<=AIJ/BJ <=u (second request)

Seeing the above two equations seems to be inconsistent with the inequality of the difference constraint, but we can make the logarithm into two number addition or two number subtraction by taking the logarithm to get the following variable form:

(1) log (L) <= log (Aij) +log (AI) <=log (U)

(2) log (L) <= log (Aij)-Log (BJ) <=log (U)

hahaha, the two add-on can be obtained: 2*log (L) <= 2*log (Aij) +log (AI)-log (BJ) <=2*log (U)

Items to be moved: 2*log (L) -2*log (Aij) <=log (AI)-log (BJ) <= 2*log (U) -2*log (Aij)

Then you can build the edge between I and j+n, the side length is the value in the formula, the specific look at the code is, but this card queue, with the general method of the number of times more than the points awarded the ring will tle, here can be used in the following two ways to optimize:

1. If the number of points is N, then one of the points in the queue more than sqrt (n) there is a ring;

2. All points are more than k*n, then there are rings, K is generally 2 ....

The code is as follows:

#include <stdio.h> #include <string.h> #include <math.h> #include <queue> #include <vector
> #include <algorithm> #define MAXN 50010 #define INF 1e5 using namespace std;
int num,p[maxn],n,m;
    struct node {int en,next;
Double va;
}E[MAXN*10];
    void Init () {num=0;
memset (P,-1,sizeof (p));
    } void Add (int st,int en,double va) {e[num].en=en;
    E[num].va=va;
    E[NUM].NEXT=P[ST];
p[st]=num++;
} double DIS[MAXN];
int CNT[MAXN];
BOOL INQ[MAXN];
        BOOL Spfa () {for (int i=0;i<=n+m;i++) {dis[i]=inf;
        cnt[i]=0;
    Inq[i]=false;
    } queue<int> Q;
    Q.push (1);
    dis[1]=0.0;
    cnt[1]++;
    Inq[1]=true;
        while (Q.size ()) {int X=q.front ();
        Q.pop ();
        Inq[x]=false;
            for (int i=p[x];i!=-1;i=e[i].next) {int y=e[i].en;
            Double Len=dis[x]+e[i].va;
                if (Len<dis[y]) {Dis[y]=len; if (!Inq[y]) {Q.push (y);
                    cnt[y]++;
                    if (cnt[y]> (int) sqrt (n+m)) return false;
                Inq[y]=true;
}}}} return true;
    } int main () {double l,u;
        while (scanf ("%d%d%lf%lf", &n,&m,&l,&u)!=eof) {init ();
        Double L=2.0*log (L), U=2.0*log (U);
                for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {double x;
                scanf ("%lf", &x);
                X=2.0*log (x);
                Add (j,i+m,u-x);
            Add (i+m,j,x-l);
        }} if (SPFA ()) puts ("YES");
    Else puts ("NO");
} return 0;
 }


F-fibonacci Tree (Hdu 4786)

Test instructions: Give you a picture, the color of the picture is white (1) or black (0), ask if you can find a tree, so that the total number of white edge of the tree is the number of fib. The number of points N and the variable m are not less than 1e5.

Analysis: This is the minimum spanning tree deformation problem, with Kruskal solution, we can find two times the minimum spanning tree to get the number of white edge limit, and then determine if there are no fib number in this interval, the first time we take the black edge of the main sort to find the minimum spanning tree, to get the white edge number of the lower limit, The maximum number of white edges is obtained by the minimum spanning tree with the white edge as the main sort. Note If the first diagram is not connected, then output No.

The code is as follows:

#include <stdio.h> #include <string.h> #include <algorithm> #define INF 1e8 using namespace std;
int fa[110000];
BOOL fi[110000]; struct node {int st,en,co;}
E[110000];
    int find (int x) {if (fa[x]==x) return x;
else return Fa[x]=find (fa[x]); } bool Cmp1 (node A,node b)//black-and-white sort {return a.co<b.co;} bool CMP2 (node A,node b)//with white edge as the main sort {return a.co>b.
Co
    } void Init ()//preprocessing fib {memset (fi,false,sizeof (Fi));
    int a=1,b=2,c;
    Fi[a]=fi[b]=true;
        while (1) {c=a+b;
        if (c>100000) break;
        Fi[c]=true;
    A=b,b=c;
    }} int main () {int T;
    scanf ("%d", &t);
        Init ();//preprocessing fib for (int ca=1;ca<=t;ca++) {int n,m;
        scanf ("%d%d", &n,&m);
        for (int i=1;i<=m;i++) scanf ("%d%d%d", &e[i].st,&e[i].en,&e[i].co);
        int ans1=0,ans2=0;
        Sort (E+1,E+M+1,CMP1);/////black edge-based minimum spanning tree for (int i=1;i<=n;i++) fa[i]=i; for (int i=1;i<=m;i++) {int Fx=find (e[i].st), Fy=find (E[i].en);
                if (fx!=fy) {if (e[i].co) ans1++;
            Fa[fx]=fy;
        }} int Fg=1,tem=find (1);
                for (int i=2;i<=n;i++) {if (Find (i)!=tem) {fg=0;
            Break
            }} if (!FG) {printf ("Case #%d:no\n", CA);
        Continue
        }///this is to determine the graph is not connected to the situation of sort (E+1,E+M+1,CMP2);//white edge-based minimum spanning tree for (int i=1;i<=n;i++) fa[i]=i;
            for (int i=1;i<=m;i++) {int Fx=find (e[i].st), Fy=find (E[i].en);
                if (fx!=fy) {if (e[i].co) ans2++;
            Fa[fx]=fy;
        }} fg=0;
                for (int i=ans1;i<=ans2;i++) {if (Fi[i]) {fg=1;
            Break }} if (FG) printf ("Case #%d:yEs\n ", CA);
    else printf ("Case #%d:no\n", CA);
} return 0;
 }

G-slim Span (UVA 1395)

Test instructions: A graph of n (n<=100) nodes is given, and the size of the tree (the maximum edge minus the minimum edge) is minimized, and the value is output.
Analysis: According to the Kruskal algorithm, we can enumerate the minimum edge weights, and then the minimum spanning tree, we can calculate the maximum edge weight minus the minimum edge right minimum value.

Code:

#include <stdio.h> #include <string.h> #include <algorithm> #define INF 1e8 using namespace std;
int fa[110];
int n,m; struct node {int st,en,len;}
E[110000];
    int find (int x) {if (fa[x]==x) return x;
else return Fa[x]=find (fa[x]);
    } BOOL CMP (node A,node b) {return a.len<b.len;} int kruskal (int pos) {for (int i=1;i<=n;i++) fa[i]=i;
    int minn=e[pos].len,maxx=0;
        for (int i=pos;i<=m;i++) {int Fx=find (e[i].st), Fy=find (E[i].en);
            if (fx!=fy) {Maxx=max (Maxx,e[i].len);
        Fa[fx]=fy; }} int Fg=1,tem=find (1);////The following code determines whether the diagram is connected, does not connect to this answer, return-1 for (int i=2;i<=n;i++) {if (Find (i)!=te
            m) {fg=0;
        Break
    }} if (!FG) return-1;
return Maxx-minn; } int main () {while (scanf ("%d%d", &n,&m), n| |
        m) {for (int i=1;i<=m;i++) scanf ("%d%d%d", &e[i].st,&e[i].en,&e[i].len); SoRT (E+1,E+M+1,CMP);
        int ans=1e9;
            int X=kruskal (1);//First Look at whether the graph is connected if (x==-1) {puts ("-1");
        Continue
        } ans=min (ans,x);
            for (int i=2;i<=m-n+2;i++)///Because the minimum number of spanning tree edges is n-1, then there is definitely no minimum spanning tree {int X=kruskal (i) for less than n-1 bars;
        if (x!=-1) ans=min (ans,x);
    } printf ("%d\n", ans);
} return 0;
 }

H-buy or Build (UVA 1151)

Test instructions: There are n points in the two-dimensional plane, tell you the coordinates of each point, your task is to make each point is connected, then, you can create some side, each side of the cost of two points Euclidean distance squared, also give you Q (0<=q<=8) a package, if you choose the first I package, All points in this package become interconnected, and the Tier I package fee is CI.

Analysis: Look at the range of Q it is easy to think of the binary enumeration of what the package is, according to the Kruskal algorithm, if we choose the first package, then the father of all points in the first package can be all unified to one of the points. Note with a long long

The code is as follows:

#include <stdio.h> #include <string.h> #include <vector> #include <algorithm> #define INF 1e8 #
Define LL long long using namespace std;
Vector<int> V[10];
int w[10];
int fa[1100];
int n,m; struct node {int st,en,len;}
E[1100000]; struct Node {int x, y;}
V[1100];
    int find (int x) {if (fa[x]==x) return x;
else return Fa[x]=find (fa[x]); } BOOL CMP (node A,node b) {return a.len<b.len;} int get (node A,node b)//cost {return (a.x-b.x) * (a.x-b.x) + (A.
Y-B.Y) * (A.Y-B.Y));
    } ll Kruskal () {ll ans=0;
        for (int i=1;i<=m;i++) {int Fx=find (e[i].st), Fy=find (E[i].en);
            if (fx!=fy) {Ans+=e[i].len;
        Fa[fx]=fy;
}} return ans;
    } int main () {int q,t;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&q);
        for (int i=0;i<q;i++) v[i].clear ();
            for (int i=0;i<q;i++) {int num,x; scanf ("%d%d", &AMP;NUM,&AMp;w[i]);
                while (num--) {scanf ("%d", &x);
            V[i].push_back (x);
        }} for (int i=1;i<=n;i++) scanf ("%d%d", &v[i].x,&v[i].y);
        m=0;
                for (int i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) {m++;
            E[m].st=i,e[m].en=j,e[m].len=get (V[i],v[j]);
        }} sort (e+1,e+m+1,cmp);/////Just sort once for (int i=1;i<=n;i++) fa[i]=i;
            ll Ans=kruskal ();///pre-processing an answer for (int i=1;i< (1&LT;&LT;Q); i++) {LL tem=0;///total cost of choosing a package
            for (int j=1;j<=n;j++) fa[j]=j; for (int j=0;j<q;j++) {if (!) (
                i& (1&LT;&LT;J))) continue;
                TEM+=W[J];
                int X=find (v[j][0]);
                for (int k=1;k<v[j].size (); k++)///To place these points in a connected block, Kruskal will ignore their original cost when running to these edges, which is also the reason for sorting once.    int Y=find (v[j][k]);
                    if (y!=x) {fa[y]=x;
        }}} ans=min (Ans,tem+kruskal ());///Because the minimum spanning tree answer does not include the cost of the package, so add the line}
        printf ("%lld\n", ans);
    if (T) printf ("\ n");
} return 0;
 }



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.