20150321BC

 BC的界面貌似变得很漂亮的样子(暴露了多久没打BC了。。。)
写在之前:
这学期遇到了点事情,所以心情很受影响,代码也没怎么敲,课程本来就紧,再加上转专业的一堆破事,所以有些不爽...
影响心情的事情虽然没有被完全解决,不过暂时被冻结了, 大家都冷静一段时间,冷静下来,也就自然而然得解决了。
代码有一阵没敲了,脑子也开始生锈了呢....
 A:买团体票,有不同的规则,问哪家比较合适,水。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
      int m,n,i;
      int a[120],b[120];
      int ans=0;
      int cost[120];
    while (scanf("%d %d",&n,&m)!=EOF)
    {
         memset(a,0,sizeof(a));
         memset(b,0,sizeof(b));
         memset(cost,0,sizeof(cost));
        for (int i=1;i<=m;i++)
            scanf("%d %d",&a[i],&b[i]);
        for (int i=1;i<=m;i++)
        {
            cost[i]=n/a[i]*b[i];
            if (%a[i]!=0)
                cost[i]+=b[i];
        }
        ans=999999999;
        for (int i=1;i<=m;i++)
            if (cost[i]<ans)
             ans=cost[i];
        printf("%d\n",ans);
    }
    return 0;
}
 

B:要求连续W个堆的高度都为H,问最少调整多少个块,如果不能得到就-1.
   容易想到,如果块的总数不小于W*H,问题一定有解。
   我们想要找到连续的W个堆,使它最接近最优值。
如果每个堆的高度为h[i]
通过数据发现,当W=4,H=6时,6 6 6 6 >6 6 6 7 > 4 4 8 8 >4 4 4 5 >4 4 4 4
我们容易发现,一段连续区间的好坏是由(h[i]-H)和abs(h[i]-H)共同决定的

 
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{    int n,W,H;
     int h[50001];
     int sum;
     int now;
     int m;
     int ans;
    while (scanf("%d %d %d",&n,&W,&H)!=EOF)
    {   sum=0;
         for (int i=1;i<=n;i++)
         {
            scanf("%d",&h[i]);
            sum=sum+h[i];
         }
         if (sum<W*H)
         {
            printf("-1\n");
            continue;
         }
          now=0;
          m=99999999;
          int cost=0;
          int ave;
         for (int i=1;i<=W;i++)
         {
            now=now+(h[i]-H);
            cost=cost+abs(h[i]-H);
         }
            ave=(abs(now)+cost)/2;
         if (ave<m)
         {
             m=ave;
             ans=1;
         }
         for (int i=W+1;i<=n;i++)
         {
             now=now+h[i]-h[i-W];
             cost=cost+abs(h[i]-H)-abs(h[i-W]-H);
             ave=(abs(now)+cost)/2;
             if (ave<m)
             {
                 m=ave;
                 ans=i;
             }
         }
         printf("%d\n",m);
    }
    return 0;
}
 
posted @ 2015-03-23 16:08  111qqz  阅读(161)  评论(0编辑  收藏  举报