首页 / 算法 / 题目1082:代理服务器(贪心算法)
题目1082:代理服务器(贪心算法)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了题目1082:代理服务器(贪心算法),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1983字,纯文字阅读大概需要3分钟。
内容图文
题目链接:http://ac.jobdu.com/problem.php?pid=1082
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1082 代理服务器.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> #define MAX_SIZE 1001 #define MAX_SERVER 5001 //#define debugusingnamespace std; int n, m; struct Server{//define the struct of serverint x, y, z, w; booloperator == (const Server &a) const{//reload the == operatorreturn (x==a.x && y==a.y && z==a.z && w==a.w); } }; Server s1[MAX_SIZE]; Server s2[MAX_SERVER]; int main(){ #ifdef debug freopen("/Users/pengfei_zheng/Desktop/input.txt", "r", stdin); #endifwhile(scanf("%d",&n)!=EOF){ for(int i = 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(int i = 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 caseint i; for(i = 0 ; i < m ; i++){ if(s1[0]==s2[i]){ break; } } if(i<m) printf("-1\n"); else printf("0\n"); } else{ bool loopFlag = true; int start=0, ans = 0, idx = 0; int maxIdx; while(loopFlag){ maxIdx = -1; loopFlag = false; for(int i = 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 the other servers ans++; loopFlag = true; } start = maxIdx + 1; //update the start Idx } printf("%d\n",ans); } } return0; } /************************************************************** Problem: 1082 User: zpfbuaa Language: C++ Result: Accepted Time:10 ms Memory:1612 kb ****************************************************************/
原文:http://www.cnblogs.com/zpfbuaa/p/6803520.html
内容总结
以上是互联网集市为您收集整理的题目1082:代理服务器(贪心算法)全部内容,希望文章能够帮你解决题目1082:代理服务器(贪心算法)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。