Codeforces round #236Div.2--A;B

来源:互联网 发布:淘宝店铺不想开了 编辑:程序博客网 时间:2024/04/30 23:51
A. Nuts
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a nuts and lots of boxes. The boxes have a wonderful feature: if you put x (x ≥ 0) divisors (the spacial bars that can divide a box) to it, you get a box, divided into x + 1 sections.

You are minimalist. Therefore, on the one hand, you are against dividing some box into more than k sections. On the other hand, you are against putting more than v nuts into some section of the box. What is the minimum number of boxes you have to use if you want to put all the nuts in boxes, and you have b divisors?

Please note that you need to minimize the number of used boxes, not sections. You do not have to minimize the number of used divisors.

Input

The first line contains four space-separated integers kabv (2 ≤ k ≤ 10001 ≤ a, b, v ≤ 1000) — the maximum number of sections in the box, the number of nuts, the number of divisors and the capacity of each section of the box.

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
3 10 3 3
output
2
input
3 10 1 3
output
3
input
100 100 1 1000
output
1
Note

In the first sample you can act like this:

  • Put two divisors to the first box. Now the first box has three sections and we can put three nuts into each section. Overall, the first box will have nine nuts.
  • Do not put any divisors into the second box. Thus, the second box has one section for the last nut.

In the end we've put all the ten nuts into boxes.

The second sample is different as we have exactly one divisor and we put it to the first box. The next two boxes will have one section each.

题意:求将核桃全部装进箱子所用的最少箱子数...

思路:一开始没有想全情况,就开始敲代码,越敲越不对,后来代码重新删了再写...用了1:19才把它AC了TT..

    思路:使用循环,每一次都使用最大的d,还要判断d和f的关系..直到a小于等于0为止..

Code:

#include<stdio.h>#include<iostream>#include<string.h>using namespace std;int main(){    int k,a,b,v,n,ans=0,t;    scanf("%d%d%d%d",&k,&a,&b,&v);    if(v>=a)printf("1\n");    else    {        while(a>0)        {            t=b+1;            for(int i=1;i<=b;i++)            {                if(((i+1)*v)>=a)                {                    t=i+1;                    break;                }            }            if(t>k)t=k;            a=a-(t*v);            ans++;            if(a<=v&&a>0)            {                a=a-v;                ans++;            }            b=b-t+1;        }        printf("%d\n",ans);    }    return 0;}

...后来和大神讨论..发现自己这种做法实在笨,,,

   直接把此题当成数学问题...先求出装核桃所需的格子数..再进行计算..容易得多了..

CODE:

#include<stdio.h>#include<string.h>#include<math.h>#include<string>#include<iostream>#include<algorithm>using namespace std;int main(){   // freopen("in.in","r",stdin);    int k,a,b,v;    scanf("%d%d%d%d",&k,&a,&b,&v);    int num=a/v+ (a%v==0?0:1);    int ans=0;    while((b>=k-1)&&num>0)    {        b=b-k+1;        num-=k;        ans++;    }    if(num>0)    {        num-=(b+1);        ans++;    }    if(num>0) ans+=num;    printf("%d\n",ans);     return 0;}


B. Trees in a Row
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i ≤ n) tree from the left has height ai meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 ≤ i < n),ai + 1 - ai = k, where k is the number the Queen chose.

Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes?

Input

The first line contains two space-separated integers: nk (1 ≤ n, k ≤ 1000). The second line contains n space-separated integersa1, a2, ..., an (1 ≤ ai ≤ 1000) — the heights of the trees in the row.

Output

In the first line print a single integer p — the minimum number of minutes the gardener needs. In the next p lines print the description of his actions.

If the gardener needs to increase the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, then print in the corresponding line "+ j x". If the gardener needs to decrease the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, print on the corresponding line "- j x".

If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them.

Sample test(s)
input
4 11 2 1 5
output
2+ 3 2- 4 1
input
4 11 2 3 4
output
0

题意:要求一排树按照高度要求修剪...输出修剪的最少步及步骤..

思路:比赛时忽略了最少步...直接就把第一棵树作为基准去修剪后面的树...加上去便WA了...找出问题时已经time over了..

     把所有的数都遍历一遍...得到修剪的最少步数..记得将基准树记录下来...

CODE:

#include<stdio.h>#include<iostream>#include<math.h>#include<string.h>#include<algorithm>using namespace std;const int INF=0xfffffff;int main(){    //freopen("B.txt","r",stdin);    int n,k,cur;    int a[1005];    while(~scanf("%d%d",&n,&k))    {        int mann=INF,st=1,cur;        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        for(int i=1;i<=n;i++)        {            cur=0;            int t=a[i]-k*(i-1);            if(t<=0) continue;            for(int j=1;j<=n;j++)            {                if(a[j]!=t) cur++;                t+=k;            }            if(mann>cur)            {mann=cur;             st=i;}        }        printf("%d\n",mann);        int t=a[st]-k*(st-1);        for(int i=1;i<=n;i++)        {            if(t-a[i]>0)            {                printf("+ %d %d\n",i,t-a[i]);            }            else if(t-a[i]<0)            {                printf("- %d %d\n",i,a[i]-t);            }            t+=k;        }    }    return 0;}

...现今的水平只到做两道题的份上了...继续加油!!!

0 0
原创粉丝点击