POJ1505:Copying Books(贪心+二分枚举)

来源:互联网 发布:歌词字幕软件 编辑:程序博客网 时间:2024/05/04 23:39
Copying Books 
Description 
Before the invention of book-printing, it was very hard to make copy of book. All the contents had to be re-written by hand by so called scribers. The scriber had been given book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.  
 
Once upon time, there was theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have books (numbered 12 ... m) that may have different number of pages (p1, p2 ... pm) and you want to make one copy of each of them. Your task is to divide these books among scribes, <= m. Each book can be assigned to single scriber only, and every scriber must get continuous sequence of books. That means, there exists an increasing succession of numbers 0 b0 b1 b2, ... bk-1 <= bk such that i-th scriber gets sequence of books with numbers between bi-1+1 and bi. The time needed to make copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to single scriber. Your task is to find the optimal assignment.  
 
Input 
 
The input consists of cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integers and k, 1 <= <= <= 500At the second line, there are integers p1, p2, ... pm separated by spaces. All these values are positive and less than 10000000 
Output 
 
For each caseprint exactly one line. The line must contain the input succession p1, p2, ... pm divided into exactly parts such that the maximum sum of single part should be as small as possible. Use the slash character ('/'to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.  
 
If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.  
Sample Input 
 
2 
9 3 
100 200 300 400 500 600 700 800 900 
5 4 
100 100 100 100 100 
Sample Output 
 
100 200 300 400 500 600 700 800 900 
100 100 100 100 100 
Source 
 
Central Europe 1998 

MYCode:

#include 
#include 
#include 
#define MAX 510 
typedef long long ll; 
using namespace std; 
int v[MAX]; 
ll sum; 
int id[MAX]; 
int n,m; 
int most; 
bool check(ll lim) 

    int i; 
    ll s=0
    int ct=0
    for(i=n;i>=1;i--)//note 
    
        if(s+v[i]>lim) 
        
            s=v[i]; 
            ct++; 
        
        else 
        s+=v[i]; 
    
    ct++; 
    if(ct<=m) 
    return true
    return false

 
ll b_search() 

    ll lt,rt; 
    lt=most,rt=sum; 
    while(lt
    
        ll mid=(lt+rt)/2
        if(check(mid)) 
        
            rt=mid; 
        
        else 
        lt=mid+1
    
    return rt; 

int main() 

    int t; 
    scanf("%d",&t); 
    while(t--) 
    
        scanf("%d%d",&n,&m); 
        sum=0
        int i; 
        most=-1
        for(i=1;i<=n;i++) 
        
            scanf("%d",&v[i]); 
            if(v[i]>most) 
            most=v[i]; 
            sum+=v[i]; 
        
        ll ans=b_search(); 
        //cout<<"ans="<<ans<<endl; 
        int s=0
        int ct=1
        for(i=n;i>=1;i--) 
        
            if(s+v[i]>ans) 
            
                ct++; 
                id[i]=ct; 
                s=v[i]; 
            
            else 
            
                s+=v[i]; 
                id[i]=ct; 
            
        
        if(ct
        
            int add=m-ct; 
            int tp=10000
            for(i=2;i<=n && add>0;i++) 
            
                if(id[i]==id[i-1]) 
                
                    id[i-1]=tp++; 
                    add--; 
                
            
        
        printf("%d ",v[1]); 
        for(i=2;i<=n;i++) 
        
            if(id[i]!=id[i-1]) 
            printf("/ "); 
            printf("%d ",v[i]); 
        
        printf("\n"); 
 
    
}
 这道题的2种贪心的思路:
第一种:
每次选取和最小的2个包进行合并
第二种:
二分枚举最大包的容量,判断一个容量是否可行的时候用贪心
第一种思路不妥:
比如下面这组数据:
7 3
2 2 2 1 1 1 2
采用第一种思路得到的结果是错的.
二分枚举+贪心的思路如下:
枚举最大容量,判断一个容量是否可行采用贪心的策略.注意要从后往前面贪心,因为题目要求有多个最佳答案时要使前面的数尽量小
如果一个容量是可行的最小容量,此时对应的分组如果小于规定的分组,就再从前往后将可以拆的包都拆掉,凑足m个
注意二分枚举的下界是最大的单本书的页数.所有书的总页数之和是LL类型.
#include <stdio.h>  
__int64 a[510];  
__int64 n,k;  
  
__int64 get(__int64 m)  
{  
    __int64 num = 0,i;  
    __int64 sum = 0;  
    for(i = n; i >= 1; i--)  
    {  
        if(sum + a[i] > m)  
        {  
            sum = a[i];  
            num++;  
        }  
        else  
        {  
            sum += a[i];  
        }  
    }  
    return num + 1;  
}  
int main()  
{  
    __int64 t,i,l,r;  
    scanf("%I64d",&t);  
    while(t--)  
    {  
        l = 0;  
        r = 0;  
        scanf("%I64d %I64d",&n,&k);  
        for(i = 1; i <= n; i++)  
        {  
            scanf("%I64d",&a[i]);  
            if(a[i] > l)  
                l = a[i];  
            r += a[i];  
        }  
        while(l < r)  
        {  
            int mid = (l + r) /2;  
            int m = get(mid);  
            if(m <= k)  
            {  
                r = mid;  
            }  
            else  
            {  
                l = mid + 1;  
            }     
        }  
    //printf("%I64d\n",r);  
        __int64 num = 1;  
        __int64 sum = 0;  
        for(i = n; i >= 1; i--)  
        {  
            if(sum + a[i] > r)  
            {  
                sum = a[i];  
                a[i] *= -1;  
                num++;  
            }  
            else  
            {  
                sum += a[i];  
            }  
        }  
        for(i = 1;i <= n && num < k; i++)  
        {  
            if(a[i] > 0)  
            {  
                a[i] *= -1;  
                num++;  
            }  
        }  
          
        for(i = 1;i <= n; i++)  
        {  
            if(i == 1)  
            {  
                if(a[1] < 0)  
                {  
                    printf("%I64d",-a[i]);  
                        if(i != n)  
                    printf(" /");  
                }  
                else  
                    printf("%I64d",a[1]);  
            }  
            else  
            {  
                if(a[i] < 0)  
                {  
                    printf(" %I64d",-a[i]);  
                        if(i != n)  
                    printf(" /");  
                }  
                else  
                    printf(" %I64d",a[i]);  
            }  
        }  
        puts("");     
    }  
    return 0;  
}
0 0