hackerrank----Flatland Space Stations

原题目网址:   https://www.hackerrank.com/challenges/flatland-space-stations

<span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);">
</span>
<span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);">Flatland is a country with </span><span class="MathJax_SVG" id="MathJax-Element-1-Frame" style="color: rgb(57, 66, 78); font-family: inherit; font-size: 16px; border: 0px; font-variant: inherit; font-stretch: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: normal; word-break: break-word; display: inline-block; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; background-color: rgb(255, 255, 255);"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.395ex" height="1.676ex" style="vertical-align: -0.338ex;" viewbox="0 -576.1 600.5 721.6" role="img" focusable="false"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><path stroke-width="1" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g></svg></span><span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);"> cities, </span><span class="MathJax_SVG" id="MathJax-Element-2-Frame" style="color: rgb(57, 66, 78); font-family: inherit; font-size: 16px; border: 0px; font-variant: inherit; font-stretch: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: normal; word-break: break-word; display: inline-block; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; background-color: rgb(255, 255, 255);"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.04ex" height="1.676ex" style="vertical-align: -0.338ex;" viewbox="0 -576.1 878.5 721.6" role="img" focusable="false"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><path stroke-width="1" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g></svg></span><span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);"> of which have space stations. Its cities (</span><span class="MathJax_SVG" id="MathJax-Element-3-Frame" style="color: rgb(57, 66, 78); font-family: inherit; font-size: 16px; border: 0px; font-variant: inherit; font-stretch: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: normal; word-break: break-word; display: inline-block; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; background-color: rgb(255, 255, 255);"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.007ex" height="1.676ex" style="vertical-align: -0.338ex;" viewbox="0 -576.1 433.5 721.6" role="img" focusable="false"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><path stroke-width="1" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path></g></svg></span><span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);">) are numbered from </span><span class="MathJax_SVG" id="MathJax-Element-4-Frame" style="color: rgb(57, 66, 78); font-family: inherit; font-size: 16px; border: 0px; font-variant: inherit; font-stretch: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: normal; word-break: break-word; display: inline-block; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; background-color: rgb(255, 255, 255);"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.162ex" height="2.176ex" style="vertical-align: -0.338ex;" viewbox="0 -791.3 500.5 936.9" role="img" focusable="false"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><path stroke-width="1" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g></svg></span><span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);"> to </span><span class="MathJax_SVG" id="MathJax-Element-5-Frame" style="color: rgb(57, 66, 78); font-family: inherit; font-size: 16px; border: 0px; font-variant: inherit; font-stretch: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: normal; word-break: break-word; display: inline-block; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; background-color: rgb(255, 255, 255);"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="5.398ex" height="2.176ex" style="vertical-align: -0.338ex;" viewbox="0 -791.3 2323.9 936.9" role="img" focusable="false"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><path stroke-width="1" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path><g transform="translate(822,0)"><path stroke-width="1" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g transform="translate(1823,0)"><path stroke-width="1" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></g></g></svg></span><span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);">, where </span><span class="MathJax_SVG" id="MathJax-Element-6-Frame" style="color: rgb(57, 66, 78); font-family: inherit; font-size: 16px; border: 0px; font-variant: inherit; font-stretch: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: normal; word-break: break-word; display: inline-block; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; background-color: rgb(255, 255, 255);"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.575ex" height="2.509ex" style="vertical-align: -0.338ex;" viewbox="0 -934.9 1108.8 1080.4" role="img" focusable="false"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><path stroke-width="1" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><g transform="translate(345,362)"><path stroke-width="1" transform="scale(0.707)" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><g transform="translate(255,0)"><path stroke-width="1" transform="scale(0.707)" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path></g></g></g></svg></span><span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);"> city is referred to as </span><span class="MathJax_SVG" id="MathJax-Element-7-Frame" style="color: rgb(57, 66, 78); font-family: inherit; font-size: 16px; border: 0px; font-variant: inherit; font-stretch: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: normal; word-break: break-word; display: inline-block; word-spacing: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; background-color: rgb(255, 255, 255);"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.807ex" height="2.009ex" style="vertical-align: -0.671ex;" viewbox="0 -576.1 777.8 865.1" role="img" focusable="false"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><path stroke-width="1" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path><g transform="translate(433,-150)"><path stroke-width="1" transform="scale(0.707)" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></g></g></svg></span><span style="color: rgb(57, 66, 78); font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", "Helvetica Neue", Helvetica, Arial, Ubuntu, sans-serif; font-size: 16px; line-height: 1.5em; background-color: rgb(255, 255, 255);">.</span>

Between each  and  (where ), there exists a bidirectional road  long.

For example, if  and cities  and  have space stations, Flatland would look like this:

For each city, determine its distance to the nearest space station and print the maximum of these distances.

        这道题其实就是给定一个01序列(例如01110),让你求出max(1到最近的0的距离)。

        首先找到1周围最近的0,分为三种情况:

                                 1.两个0之间的1(又分为两种情况:1.偶数个1,就是中间两个数分别到左到右的距离2.奇数个1,就是中间那个数到左或者右的距离)

                                 2.队列头部的1,最近的0就是从开始正数第一个0

                                 3.队列尾部的1,最近的0就是从最后倒数第一个0          

        对应这三种情况再继续对比他们的距离,找最大的距离:

                                 1.两个0之间的最大距离就是中间那个数

                                 2.队头和队尾的最大距离就是第一个数或者最后一个数

     源代码如下:

<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">int main(){</span></div><div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">	int n;</span></div><div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">	int m;</span></div><div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">	cin >> n >> m;</span></div><div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">	int dis;</span></div><div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">	if (n == m){</span></div><div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">		vector<int> c(m);</span></div><div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">		dis = 0;</span></div>	}
	else{
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">		for (int c_i = 0; c_i < m; c_i++){</span></div>		vector<int> a(n, 1);
			cin >> c[c_i];
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">		int lon = 0;</span></div>			int a0 = c[c_i];
			a[a0] = 0;
		}
		int lon1 = 0;
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">			if (a[j] == 1){</span></div>		int d = 0;
		bool end=false;
		for (int j = 0; j<n; j++){
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">					lon1 = lon;</span></div>				lon++;
			}
			else if (a[j] == 0){
				
				if (lon>=lon1){
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">				if (d == 1 && lon > lon1)</span></div>					d++;
				}
				lon = 0;
			}
			if (j == n - 1)
			{
				{
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">				{</span></div>					lon1 = lon;
					end = true;
				}
				else if (d!=1 && lon>lon1/2)
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">				}</span></div>					lon1 = lon;
					end = true;
				}
				else if (d == 1){
					end = true;
			}
		}
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">				dis = (lon1 + 1) / 2;</span></div>		if (!end)
		{
			if (lon1 % 2 == 0){
				dis = lon1 / 2;
			}
			else{
			}
		}
		else
		{
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">}</span></div>			dis = lon1;
		}

	}
	cout << dis << endl;
	system("pause");
<div style="text-align: left;"><span style="font-family: Arial, Helvetica, sans-serif;">	return 0;</span></div>

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值