poj 2433 Landscaping 贪心
来源:互联网 发布:北京 气象站数据 编辑:程序博客网 时间:2024/05/16 08:40
很想知道这个题目有没人用dp做的。因为我一开始想到的就是dp,但是没设计出有效的解法。
但是其实这个题目应该还是比较明显的符合贪心的性质的,那么直接贪一下就好了。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=1e3+9;int a[maxn];int n,k;int work(){ int ans=1e9,front,end; int flag=1; for(int i=1;i<=n;i++) { if(flag&&a[i]>a[i+1]) { int t=flag,s=i; while(t>=1&&a[t]>=a[t-1]) t--; while(s<=n&&a[s]>=a[s+1]) s++; if(t>=1||s<=n) { int sum=0,tmp=max(a[t],a[s]); for(int j=t+1;j<s;j++) if(a[j]>tmp) sum+=a[j]-tmp; if(sum<ans) { ans=sum; front=t+1; end=s-1; } } } if(a[i+1]<a[i]) flag=false; if(a[i+1]>a[i]) flag=i+1; } int tmp=max(a[front-1],a[end+1]); for(int j=front;j<=end;j++) if(a[j]>tmp) a[j]=tmp; return ans;}int main(){// freopen("in.txt","r",stdin); while(scanf("%d %d",&n,&k)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); a[n+1]=0; int sum=0; bool flag=true; for(int i=1;i<=n;i++) { if(flag&&a[i]>a[i+1]) sum++; if(a[i+1]<a[i]) flag=false; if(a[i+1]>a[i]) flag=true; } int ans=0; for(int i=sum;i>k;i--) ans+=work(); cout<<ans<<endl; } return 0;}
- poj 2433 Landscaping 贪心
- POJ 2433 Landscaping
- Landscaping Gym
- TOJ 3874: Landscaping -- DP
- BZOJ4586: [Usaco2016 Open]Landscaping
- bzoj4586: [Usaco2016 Open]Landscaping
- poj 1017 Packets【贪心】
- POJ 1017 Packets(贪心)
- POJ 1323 简单贪心
- POJ 1065 (贪心)
- POJ 1328 贪心
- POJ 1018 贪心思想
- POJ 1089 贪心算法
- poj 1017 Packets(贪心)
- poj 1017 贪心算法
- poj 1328 贪心
- POJ 3544 贪心
- POJ-1456 Supermarket【贪心】
- 《数学之美》书摘
- 南阳OJ105
- leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】
- c/c++ 复习基础要点01-const指针、指针函数 函数指针、new/delete与malloc/free区别与联系
- awk之行与列的count & sum命令总汇
- poj 2433 Landscaping 贪心
- VC6操作XLS
- Uninstall GOM Player with WindowsUninstaller.Org Removal Tips
- 【cocos2d-x】公告栏的实现-文字左右移动
- 史上最强最全VC6.0到VS2013全部版本下载地址
- 【cocos2d-x】无限重复动作-新手引导箭头上下移动
- android -- WatchDog看门狗分析
- ArrayList/List 泛型集合
- amoeba安装