Delicious Apples
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 321 Accepted Submission(s): 95
Problem Description
There are ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006E.png?rev=2.4-beta-2)
apple
trees planted along a cyclic road, which is ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004C.png?rev=2.4-beta-2)
metres
long. Your storehouse is built at position ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
on
that cyclic road.
The![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0069.png?</p><p>rev=2.4-beta-2)
th
tree is planted at position ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0078.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?rev=2.4-beta-2)
,
clockwise from position ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
.
There are ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?rev=2.4-beta-2)
delicious
apple(s) on the ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0069.png?</p><p>rev=2.4-beta-2)
th
tree.
You only have a basket which can contain at most![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004B.png?</p><p>rev=2.4-beta-2)
apple(s).
You are to start from your storehouse, pick all the apples and carry them back to your storehouse using your basket. What is your minimum distance travelled?
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006B.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0035.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2265.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002B.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0032.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002B.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002E.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002B.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/006E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0035.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004C.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0039.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0078.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/005B.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0069.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/005D.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004C.png?rev=2.4-beta-2)
There are less than 20 huge testcases, and less than 500 small testcases.
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
The
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0069.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0078.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0069.png?</p><p>rev=2.4-beta-2)
You only have a basket which can contain at most
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004B.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006B.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0035.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2265.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002B.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0032.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002B.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002E.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002B.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/006E.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0035.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004C.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0031.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/283/0039.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/0030.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0078.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/005B.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0069.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/005D.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/2264.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004C.png?rev=2.4-beta-2)
There are less than 20 huge testcases, and less than 500 small testcases.
Input
First line: ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0074.png?rev=2.4-beta-2)
,
the number of testcases.
Then![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0074.png?rev=2.4-beta-2)
testcases
follow. In each testcase:
First line contains three integers,![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004C.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006E.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004B.png?rev=2.4-beta-2)
.
Next![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006E.png?</p><p>rev=2.4-beta-2)
lines,
each line contains ![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0078.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?</p><p>rev=2.4-beta-2)
.
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0074.png?rev=2.4-beta-2)
Then
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0074.png?rev=2.4-beta-2)
First line contains three integers,
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004C.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006E.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/004B.png?rev=2.4-beta-2)
Next
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/006E.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0078.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?</p><p>rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Main/Regular/400/002C.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/400/0061.png?rev=2.4-beta-2)
![](http://acm.hdu.edu.cn/js/MathJax/fonts/HTML-CSS/TeX/png/Math/Italic/283/0069.png?</p><p>rev=2.4-beta-2)
Output
Output total distance in a line for each testcase.
Sample Input
2 10 3 2 2 2 8 2 5 1 10 4 1 2 2 8 2 5 1 0 10000
Sample Output
18 26
解题思路:
注意到,最多仅仅有一次会绕整个圈走一次。因此,先贪心的处理左半环和右半环。然后枚举绕整圈的时候从左側摘得苹果和从右側摘得苹果的数目。
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <vector> #include <queue> #include <stack> #include <cmath> #include <algorithm> #define LL long long using namespace std; const int MAXN = 100000 + 10; int L, N, K; LL x[MAXN]; LL ld[MAXN], rd[MAXN]; vector<LL>l, r; int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d%d", &L, &N, &K); l.clear(); r.clear(); int pos, num, m = 0; for(int i=1;i<=N;i++) { scanf("%d%d", &pos, &num); for(int i=1;i<=num;i++) x[++m] = (LL)pos; } for(int i=1;i<=m;i++) { if(2 * x[i] < L) l.push_back(x[i]); else r.push_back(L - x[i]); } sort(l.begin(), l.end()); sort(r.begin(), r.end()); int lsz = l.size(), rsz = r.size(); memset(ld, 0, sizeof(ld)); memset(rd, 0, sizeof(rd)); for(int i=0;i<lsz;i++) ld[i + 1] = (i + 1 <= K ? l[i] : ld[i + 1 - K] + l[i]); for(int i=0;i<rsz;i++) rd[i + 1] = (i + 1 <= K ? r[i] : rd[i + 1 - K] + r[i]); LL ans = (ld[lsz] + rd[rsz]) * 2; for(int i=0;i<=lsz&&i<=K;i++) { int p1 = lsz - i; int p2 = max(0, rsz-(K-i)); ans = min(ans, 2*(ld[p1] + rd[p2]) + L); } cout << ans << endl; } return 0; }