NYOJ 791 Color the fence

来源:互联网 发布:大数据修炼系统123 编辑:程序博客网 时间:2024/05/16 05:06

Color the fence

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

Tom has fallen in love with Mary. Now Tom wants to show his love and write a number on the fence opposite to 

Mary’s house. Tom thinks that the larger the numbers is, the more chance to win Mary’s heart he has.

Unfortunately, Tom could only get V liters paint. He did the math and concluded that digit i requires ai liters paint. 

Besides,Tom heard that Mary doesn’t like zero.That’s why Tom won’t use them in his number.

Help Tom find the maximum number he can write on the fence.

输入
There are multiple test cases.
Each case the first line contains a nonnegative integer V(0≤V≤10^6).
The second line contains nine positive integers a1,a2,……,a9(1≤ai≤10^5).
输出
Printf the maximum number Tom can write on the fence. If he has too little paint for any digit, print -1.
样例输入
5
5 4 3 2 1 2 3 4 5
2
9 11 1 12 5 8 9 10 6
样例输出
55555
33

题目意思:给定v升的颜料和9个需要花费a升的颜料,花费a升的颜料意味着得到第d个数字,现在要求在所有的花费不超过v升的情况下,使得这些数字组合起来是最大的。

一开始直接从最小花费的颜料着手,如果花费的颜料是相同的,就转到从d(也就是位数)最大贪心。1。比较位数

1、先找到用最小颜料既数字d,假设它价值为c2、先构造出s 求出剩余颜料r = v mod c3、从最高位扫描s,从大到小枚举可替换数字d' >= d,假设价值为c':若c'-c <= r,则替换当前位置的d为d', r -= c' - c4、最后得出替换后的s, s'即为所求

注意:价值即一个数字要使用的颜料量

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring> #include <cstdlib>using namespace std; const int maxn = 1e6 + 5;struct Node{int x;//数字; int y;//染料       }a[maxn],b[maxn];bool cmp(Node a,Node b){   if(a.y!=b.y)return a.y<b.y;//先保证染料数最小;    else return a.x>b.x;     //在保证数字最大;      }int s[maxn];// 用于保存结果 int main(){   int n,k;    while(~scanf("%d",&n))    {int j=0;      for(int i=0;i<9;i++)     {       scanf("%d",&k);        if(k<=n)        {int L=j++;        a[L].x=i+1;a[L].y=k;       b[L].x=i+1; b[L].y=k; //b用于保存为排序前的序列,以便于下面从枚举;            }          }int N=j;     sort(a,a+j,cmp);    if(n<a[0].y)printf("-1\n"); // n比最少花费的颜料更少                         else     { int count=n/a[0].y;        if(n%a[0].y==0){for(int i=0;i<count;i++){printf("%d",a[0].x);} printf("\n");}       else        {         for(int i=0;i<count;i++){s[i] = a[0].x;}  // 先得出目前来说最长的数字,但可能不是最终结果         int r=n%a[0].y; int rr=r;         for(int i=0;i<count;i++)         {           for(int v=N-1;v>=0;v--)// 从最大的位数开始枚举           {              if(b[v].y-a[0].y<=r&&b[v].x>a[0].x)                {                    s[i] = b[v].x;                    r = r - (b[v].y - a[0].y); // 余数要有所减少                    break;                                                    }                     }                        }                 if(rr == r)    // 如果根本没有可替换的数,那么就和刚好除尽的是同一种情况         {for (int i = 0; i < count; i++)printf("%d", a[0].x);          printf("\n");         }         else         {for (int i = 0; i < count; i++)printf("%d", s[i]);   // 否则有替换的就输出新的最大数字          printf("\n");          }                                 }              }                        }  system("pause");                       return 0;   }
 
第二种解法:真心不懂,是贪心吗??????
 

#include<iostream>using namespace std;int main(){   int v,a[9],min;   while(cin>>v)   {       min=1000000;     for(int i=0;i<9;i++)     {      cin>>a[i];      if(a[i]<=min)                 min=a[i];       //找到染料最小值        }           if(min>v){cout<<-1<<endl;}     int k=v/min; //确保最大位数     for(int i=k-1;i>=0;i--) //     {      for(int j=8;j>=0;j--)      {      if(v>=a[j]&&(v-a[j])/min>=i) //找出最高位的最大值      {       cout<<j+1;       v-=a[j];       break;       }                           }                  }cout<<endl;      }return 0;}

 

0 0
原创粉丝点击