HDU 5303 Delicious Apples
来源:互联网 发布:Linux kill掉某个进程 编辑:程序博客网 时间:2024/05/22 16:15
Delicious Apples
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Problem Description
There are n apple trees planted along a cyclic road, which is L metres long. Your storehouse is built at position 0 on that cyclic road.
Thei th tree is planted at position xi , clockwise from position 0 . There are ai delicious apple(s) on the i th tree.
You only have a basket which can contain at mostK 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?
1≤n,k≤105,ai≥1,a1+a2+...+an≤105
1≤L≤109
0≤x[i]≤L
There are less than 20 huge testcases, and less than 500 small testcases.
The
You only have a basket which can contain at most
There are less than 20 huge testcases, and less than 500 small testcases.
Input
First line: t , the number of testcases.
Thent testcases follow. In each testcase:
First line contains three integers,L,n,K .
Nextn lines, each line contains xi,ai .
Then
First line contains three integers,
Next
Output
Output total distance in a line for each testcase.
Sample Input
210 3 22 28 25 110 4 12 28 25 10 10000
Sample Output
1826
刚看这个题的时候头都大了,感觉贪心起来好复杂啊。情况太多,不是很好处理。但是多列几种情况就会发现,绕圈的话最多只需要绕一次,而且还必须是左半圈的苹果数加上右半圈的苹果数小于K时才有可能需要绕圈(自己举两个例子就明白了)。所以对所有需要绕圈情况枚举判断看看是不是更优即可。因为苹果总数不超过1e5,所以可以将苹果离散化,这样更好处理。用dpl[]和dpr[]分别表示取到某个苹果所需要的距离,这里需要用到贪心,想一想再不考虑其他情况下怎么取苹果才最优。例如两个苹果树1 k+1和2 k, 明显是要优先将远处的苹果取为一篮子,依据这个规则更新dp数组即可。代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <bitset>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <iostream>#include <algorithm>#include <functional>#define maxn 100010#define ll long longusing namespace std;long long dpl[maxn],dpr[maxn],dis[maxn];vector<int> vl,vr;int main(){ ios::sync_with_stdio(false); int t; cin>>t; while(t--) { int l,n,k,cnt=1; vl.clear(); vr.clear(); cin>>l>>n>>k; for(int i=0;i<n;i++) { int a,b; cin>>a>>b; for(int i=0;i<b;i++) { dis[cnt++]=a; if(a<=(l/2)) { vl.push_back(a); } else { vr.push_back(l-a); } } } sort(vl.begin(),vl.end()); sort(vr.begin(),vr.end()); dpl[0]=0; dpr[0]=0; for(int i=0;i<vl.size();i++) { if(i+1<=k) { dpl[i+1]=vl[i]; } else { dpl[i+1]=vl[i]+dpl[i+1-k]; } } for(int i=0;i<vr.size();i++) { if(i+1<=k) { dpr[i+1]=vr[i]; } else { dpr[i+1]=vr[i]+dpr[i+1-k]; } } long long ans=dpl[vl.size()]*2+dpr[vr.size()]*2; for(int i=0;i<=k&&i<=vl.size();i++) { if(dpl[vl.size()-i]*2+dpr[max(0,(int)vr.size()-(k-i))]*2+l<ans) { ans=dpl[vl.size()-i]*2+dpr[max(0,(int)vr.size()-(k-i))]*2+l; } } cout<<ans<<endl; } return 0;}
0 0
- hdu 5303 Delicious Apples
- 【HDU】5303 Delicious Apples
- HDU 5303 Delicious Apples
- HDU 5303 Delicious Apples(dp)
- hdu 5303 Delicious Apples(dp)
- Delicious Apples (hdu 5303 贪心+枚举)
- HDU 5303 Delicious Apples(思维题)
- hdu 5303 Delicious Apples (DP+枚举)
- HDU 5303 Delicious Apples(DP + 贪心)
- hdu 5303 Delicious Apples(贪心)
- HDU 5303 Delicious Apples (类似背包)
- HDU 5303 Delicious Apples (贪心 枚举 好题)
- hdu 5303 Delicious Apples(2015 Multi-University Training Contest 2)
- [多校2015.02.1004 dp] hdu 5303 Delicious Apples
- HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)
- Hdu 5303 Delicious Apples 2015 Multi-University Training Contest 2
- HDU 5303(Delicious Apples- 环上折半dp+贪心)
- hdu 5303 Delicious Apples 2015 Multi-University Training Contest 2
- Cogs 56. 质数取石子(博弈)
- [数据结构]二叉树
- C语言基础 for语句判断奇数
- 基本排序算法
- html文件改jsp文件技巧
- HDU 5303 Delicious Apples
- 关于初学python的一点问题总结(二)
- 详解应对平台高并发的分布式调度框架TBSchedule
- Java 8 中的 Streams API 详解
- 重拾J2EE(001-web.xml)
- JavaScript —— new一个类的对象以及对象的一些操作
- React Native工作小技巧及填坑记录
- C语言之printf函数
- C#设计模式---抽象工厂(Abstract Factory)