topcoder算法练习3
SRM144 DIV1 1100 point
Problem Statement |
|||||||||||||
|
NOTE: There are images in the examples section of this problem statement that help describe the problem. Please view the problem statement in the HTML window to view them. Given a picture composed entirely of horizontal and vertical line segments, calculate the minimum number of times you must lift your pen to trace every line segment in the picture exactly n times. Each line segment will be of the form "<x1> <y1> <x2> <y2>" (quotes for clarity), representing a segment from (x1,y1) to (x2,y2). Segments may cross each other. Segments may also overlap, in which case you should count the overlapping region as appearing in the drawing only once. For example, say the drawing were composed of two lines: one from (6,4) to (9,4), and one from (8,4) to (14,4). Even though they overlap from (8,4) to (9,4), you should treat the drawing as if it were a single line from (6,4) to (14,4). You would not need to lift your pen at all to trace this drawing. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
| - | The pen starts on the paper at a location of your choice. This initial placement does not count toward the number of times that the pen needs to be lifted. | ||||||||||||
Constraints |
|||||||||||||
| - | segments will contain between 1 and 50 elements, inclusive. | ||||||||||||
| - | Each element of segments will contain between 7 and 50 characters, inclusive. | ||||||||||||
| - | Each element of segments will be in the format "<X1>_<Y1>_<X2>_<Y2>" (quotes for clarity). The underscore character represents exactly one space. The string will have no leading or trailing spaces. | ||||||||||||
| - | <X1>, <Y1>, <X2>, and <Y2> will each be between -1000000 and 1000000, inclusive, with no unnecessary leading zeroes. | ||||||||||||
| - | Each element of segments will represent a horizontal or vertical line segment. No line segment will reduce to a point. | ||||||||||||
| - | n will be between 1 and 1000000, inclusive. | ||||||||||||
Examples |
|||||||||||||
| 0) | |||||||||||||
|
|||||||||||||
| 1) | |||||||||||||
|
|||||||||||||
| 2) | |||||||||||||
|
|||||||||||||
| 3) | |||||||||||||
|
|||||||||||||
| 4) | |||||||||||||
|
|||||||||||||
| 5) | |||||||||||||
|
|||||||||||||
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
这道题思路不难,但是还是感觉实现起来好费劲
思路是这样的
step1 将字符串数组编程数字线段列表
step2 去掉overlap的得到不重叠的线段组
step3 找交叉点,包括端点 记录每个点的交叉的出度 再乘以n
step4 一对奇数出度点代表 抬笔一次
这个程序太复杂,根本没写完,自己太挫了,觉得算overlap好麻烦啊
#include <vector>
#include <list>
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h> using namespace std; class PenLift{
public:
int numTimes(vector <string>, int);
};
int PenLift::numTimes(vector <string> segments, int n){
//turn string segment vector to int segment vector
int penlift = ;
int segnum = segments.size();
list<int*> seglist;
int i,j,k;
int start,space;
string sseg;
string snum; // string for single number
string::iterator iter;
for(i=;i<segnum;i++){
start=;
space=;
sseg=segments.at(i);
j=;
k=;
int nseg[];
for(iter=sseg.begin();iter!=sseg.end();iter++){
if(*iter==' '){
space = j;
snum=sseg.substr(start,space-start);
nseg[k]=atoi(snum.c_str());
start = space+;
k++;
}
j++;
}
snum=sseg.substr(start,j-start);
snum=sseg.substr(start,space-start);
nseg[k]=atoi(snum.c_str());
seglist.push_back(nseg);
}
//reduce overlap
list<int*>::iterator iteri,iterj,itert;
for(iteri=seglist.begin();iteri!=seglist.end();iteri++){
for(iterj=iteri++;iterj!=seglist.end();iterj++){
if((int*)(*iteri)[]==(int*)(*iterj)[]){
if(iterj[]<=iterj[]){
if(iteri[]>=iterj[]&&iteri[]<=iterj[]){
if(iteri[]>=iterj[]&&iteri[]<=iterj[]){ }else if( ){}
}
}else{ }
}
}
}
return penlift;
}
topcoder算法练习3的更多相关文章
- topcoder算法练习2
Problem Statement In most states, gamblers can choose from a wide variety of different lottery ...
- ITWorld:2014年全球最杰出的14位编程天才
近日,ITWorld 整理全球最杰出的 14 位程序员,一起来看下让我们膜拜的这些大神都有哪些?(排名不分先后) 1.Jon Skeet 个人名望:程序技术问答网站 Stack Overflow 总排 ...
- BFS/DFS算法介绍与实现(转)
广度优先搜索(Breadth-First-Search)和深度优先搜索(Deep-First-Search)是搜索策略中最经常用到的两种方法,特别常用于图的搜索.其中有很多的算法都用到了这两种思想,比 ...
- IT求职中,笔试、面试的算法准备
PS:此文章为转载,源地址:http://www.newsmth.net/nForum/#!article/CoderInterview/849 作者应该是在美国进行的笔试面试,感觉面试的的公 ...
- *[topcoder]LCMSetEasy
http://community.topcoder.com/stat?c=problem_statement&pm=13040 DFS集合全排列+LCM和GCD.但事实上,有更简单的算法,列在 ...
- *[topcoder]LittleElephantAndBalls
http://community.topcoder.com/stat?c=problem_statement&pm=12758&rd=15704 topcoder的题经常需要找规律,而 ...
- [topcoder]KingdomReorganization
http://community.topcoder.com/stat?c=problem_statement&pm=11282&rd=14724 这道题是最小生成树,但怎么转化是关键. ...
- [topcoder]ActivateGame
http://community.topcoder.com/stat?c=problem_statement&pm=10750&rd=14153 http://apps.topcode ...
- [topcoder]BestRoads
http://community.topcoder.com/stat?c=problem_statement&pm=10172&rd=13515 http://community.to ...
随机推荐
- ie8此加载项无法恢复&网站还原错误问题解决=lr成功打开ie成功录制脚
问题:ie8打开公司数字神经时出现此加载项无法恢复,网站还原错误. 解决方法: 1.管理ie加载项,全部禁用以后问题仍然存在,确定不是加载项问题. 2.重设ie8,工具-internet选项-高级-重 ...
- Linux学习笔记29——IPC状态命令
一 IPC IPC是进程间通讯,在前面,我们相继学习了进程间通讯机制有信号量,内存共享,消息队列.状态命令(ipcs)和删除命令(ipcrm)提供了一种检查和清理IPC机制的方法. 二 状态命令 1 ...
- 【C++】冒泡排序、插入排序、快速排序
#include<iostream> using namespace std; void BubbleSort(int *a,int istart,int len)//冒泡排序 { //a ...
- [转]让程序在崩溃时体面的退出之SEH
原文地址:http://blog.csdn.net/starlee/article/details/6636723 SEH的全称是Structured Exception Handling,是Wind ...
- php异步请求模拟多进程
在A请求页面发起另一个B页面请求 不需要等待B页面执行结束再返回 直接往下执行A页面的请求 A页面代码 <?php $url = 'http://'.$_SERVER['HTTP_HOST']. ...
- SDUT2608(Alice and Bob)
题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynom ...
- 使用jq工具在Shell命令行处理JSON数据
由于近期要处理一些 JSON 数据格式.一大早经过一番搜索后,终于找到了 jq 这个非常棒的工具.jq 同意你直接在命令行下对 JSON 进行操作,包含分片.过滤.转换等等. 首先在mac下安装jq. ...
- LeetCode201 Bitwise AND of Numbers Range Java 题解
题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...
- DataTable用法
在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...
- 基于Html5的兼容所有主流浏览器的在线视频播放器videoJs
在一个新的项目上需要实现在线视频播放,原本打算借助优酷的视频存储和播放,但是发现这个需要用户注册优酷账户,严重影响用户体验,于是这个方案被毙掉了.于是开始了自己开发一个在线播放器的想法,当然尽量使用已 ...


