复制书稿(book)
时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 1
[提交][状态][讨论版][命题人:quanxing]
题目描述
现在要把m本有顺序的书分给k个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一、第三和第四本书给同一个人抄写。
现在请你设计一种方案,使得复制时间最短。复制时间为抄写页数最多的人用去的时间。
输入
第一行两个整数m,k;(k≤m≤500)
第二行m个整数,第i个整数表示第i本书的页数。
输出
共k行,每行两个整数,第i行表示第i个人抄写的书的起始编号和终止编号。k行的起始编号应该从小到大排列,如果有多解,则尽可能让前面的人少抄写。
样例输入
9 3
1 2 3 4 5 6 7 8 9
样例输出
1 5
6 7
8 9
提示
一开始直接用dp,但是发现这道题具有后效性,不能有dp
典型测试数据:
10 4
1 1 1 1 1 1 1 1 1 1
答案为
1 1
2 4
5 7
8 10
用dp的话答案为:
1 2
3 4
5 7
8 10
因为考虑钱4个时,dp【4】【2】最优就是 1 2 和3 4,但是因为k行的起始编号应该从小到大排列,如果有多解,则尽可能让前面的人少抄写,导致,之后发现最多为3是,为了让1号是,1号应为1 ,第二个人干3个
WA的错误解法:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include<deque> #include<stack> #define inf 0x3f3f3f3f using namespace std; int a[505]; int sum[505]; int dp[505][505]; int main() { int m,k; scanf("%d %d",&m,&k); sum[0]=0; memset(dp,inf,sizeof(dp)); for(int i=1;i<=m;i++) { scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; dp[i][1]=sum[i]; } for(int i=2;i<=m;i++) { for(int j=1;j<=i;j++) { for(int p=1;p<=min(i-1,k-1);p++) { if(dp[i][p+1]>max(dp[j][p],sum[i]-sum[j])) { dp[i][p+1]=max(dp[j][p],sum[i]-sum[j]); } } } } int t=m; int p=k; stack<int>s; while(!s.empty()) s.pop(); s.push(m); while(p!=1) { for(int j=1;j<=t;j++) { if(dp[t][p]==max(dp[j][p-1],sum[t]-sum[j])) { s.push(j+1); s.push(j); t=j; p--; break; } } } s.push (1); for(int i=1;i<=k;i++) { cout<<s.top()<<" "; s.pop(); cout<<s.top()<<endl; s.pop(); } //cout<<dp[m][k]<<endl; return 0; }