Topic 1082: Proxy server (greedy algorithm)

Source: Internet
Author: User

Title Link: http://ac.jobdu.com/problem.php?pid=1082

Detailed Links: https://github.com/zpfbuaa/JobduInCPlusPlus

Reference code:

////1082 Proxy Server. cpp//Jobdu////Created by Pengfei_zheng on 03/05/2017.//copyright©2017 Pengfei_zheng. All rights reserved.//#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<cstring>#include<cmath>#include<climits>#defineMax_size 1001#defineMax_server 5001//#define DEBUGusing namespacestd;intN, M;structserver{//define the struct of server    intx, Y, Z, W; BOOL operator== (ConstServer &a)Const{//reload the = = operator        return(x==a.x && y==a.y && z==a.z && w==A.W); }}; Server S1[max_size]; Server S2[max_server]; intMain () {#ifdef Debug Freopen ("/users/pengfei_zheng/desktop/input.txt","R", stdin);#endif     while(SCANF ("%d", &n)! =EOF) {         for(inti =0; I < n; i++) {scanf ("%d.%d.%d.%d",&s1[i].x,&s1[i].y,&s1[i].z,&S1[I].W); } scanf ("%d",&m);  for(inti =0; I < m; i++) {scanf ("%d.%d.%d.%d",&s2[i].x,&s2[i].y,&s2[i].z,&S2[I].W); }        if(1==n) {//Special Case            inti;  for(i =0; I < m; i++){                if(s1[0]==S2[i]) {                     Break; }            }            if(i<m) printf ("-1\n"); Elseprintf"0\n"); }        Else{            BOOLLoopflag =true; intstart=0, ans =0, idx =0; intMaxidx;  while(loopflag) {Maxidx= -1; Loopflag=false;  for(inti =0; I < n; i++) {idx=start;  while(IDX < m &&!) (s1[i]==S2[idx])) {idx++; } maxidx= max (MAXIDX,IDX);//find the last appear server                }                if(Maxidx < m) {//judge whether this server can reach all of the other serversans++; Loopflag=true; } Start= Maxidx +1;//Update the start IDX} printf ("%d\n", ans); }    }    return 0;}/************************************************************** problem:1082 User:zpfbuaa language:c++ Re sult:accepted time:10 Ms memory:1612 kb****************************************************************/

Topic 1082: Proxy server (greedy algorithm)

Related Article

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.