2018-08-22 测试8

  • A  -- Raising Bacteria

  • Description

You are a lover of bacteria. You want to raise some bacteria in a box.

Initially, the box is empty. Each morning, you can put any number of bacteria into the box. And each night, every bacterium in the box will split into two bacteria. You hope to see exactly x bacteria in the box at some moment.

What is the minimum number of bacteria you need to put into the box across those days?

  • Input

The only line containing one integer x (1 ≤ x ≤ 109).

  • Output

The only line containing one integer: the answer.

  • Sample Input

5

  • Sample Output

2

  • 题目理解

细菌是以指数形式增长,由于对时间没有要求所以一个细菌可以得到\small 2^0\small 2^1、...、\small 2^n个细菌,所以最小的情况应该是由一个细菌分裂而来,我们知道任意一个数都能由一个二进制数表示,得到一个串中1的个数就是最后的最小细菌数目

#include<cstdio>
int main()
{
    int x,ans;
    scanf("%d",&x);
    ans=0;
    while(x){
        ans+=(x&1);
        x=x>>1;
    }
    printf("%d\n",ans);
    return 0;
}

 

  • B  -- A strange lift

  • Description

There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?

  • Input

The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.

  • Output

For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".

  • Sample Input

5 1 5

3 3 1 2 5

0

  • Sample Output

3

  • 题目理解

\small bfs求最短路,通过当前点去松弛还未到达的点。为了减小时间复杂度,对步数递增,只有当前步数的点用来进行松弛,如果到达的楼层已经在之前到达过没有必要更新值,不能再用来松弛其他的点,当到达的楼层都不在能够更新最小值(类似队列空)说明已经到达了所有能够到达的楼层,在此之前如果已经到达目的楼层输出保存的最小代价,否则输出-1

#include<cstdio>
#include<cstring>
const int maxn=205;
int k[maxn],vis[maxn];
int main()
{
    int n,a,b,down,up,cur;
    while(~scanf("%d",&n)&&n){
        scanf("%d%d",&a,&b);
        memset(vis,-1,sizeof(vis));
        for(int i=1;i<=n;++i)
            scanf("%d",k+i);
        vis[a]=0;
        cur=0;
        bool flag=true;
        while(flag){
            flag=false;
            for(int i=1;i<=n;++i){
                //printf("####%d  %d %d\n",i,vis[i],cur);
                if(vis[i]==cur){
                   // printf("!!!cur  %d\n",cur);
                    down=i-k[i];
                    up=i+k[i];
                    if(down>=1&&vis[down]==-1){
                        flag=true;
                        vis[down]=vis[i]+1;
                      //printf("down  %d\n",down);
                    }
                    if(up<=n&&vis[up]==-1){
                        flag=true;
                        vis[up]=vis[i]+1;
                        //printf("up  %d\n",up);
                    }
                }
            }++cur;
            //printf("!!!!%d\n!!%d\n",vis[b],flag);
            if(vis[b]!=-1)break;
        }
        printf("%d\n",vis[b]);
    }
    return 0;
}

 

  • D  -- Largest Rectangle in a Histogram

  • Description

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:


Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

  • Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

  • Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

  • Sample Input

7 2 1 4 5 1 3 3

4 1000 1000 1000 1000

0

  • Sample Output

8

4000

  • 题目理解

任意一段的最小值乘以它的长度然后取最大值,通过预处理得到左右值大于当前值的界限,得到当前值为最小值的区间左右边界,然后扫一遍\small n长度的数组得到其中最大乘积

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
const int maxn=1e5+5;
ll a[maxn],l[maxn],r[maxn];
int main()
{
    int n,cur;
    while(scanf("%d",&n)!=EOF){
        if (n==0) break;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        l[1]=1;r[n]=n;
        for(int i=2;i<=n;i++){
            int t=i;
            while(t>1&&a[i]<=a[t-1])
                t=l[t-1];
            l[i]=t;
        }
        for(int i=n-1;i>=1;i--){
            int t=i;
            while(t<n&&a[i]<=a[t+1])
                t=r[t+1];
            r[i]=t;
        }
        ll ans=0;
        for(int i=1;i<=n;i++)
            ans=max(ans,(r[i]-l[i]+1)*a[i]);
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • E  -- Cow Contest

  • Description

N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ AN; 1 ≤ BN; AB), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

  • Input

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

  • Output

* Line 1: A single integer representing the number of cows whose ranks can be determined

  • Sample Input

5 5

4 3

4 2

3 2

1 2

2 5

  • Sample Output

2

  • 题目理解

对于一头牛而言如果和其他所有牛都有关系那么就能够确定下它的排名,所以在求关系的时候需要具有传递性。\small d(i,j)表示\small i排名高于\small j,通过插点法将关系传递下去,然后再进行统计得到能够确定关系的牛的数目

#include<cstdio>
#include<cstring>
const int maxn=105;
int n,m;
int d[maxn][maxn];
int Floyd()
{
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
              d[i][j]=d[i][j]||(d[i][k]&&d[k][j]);
}
int main()
{
    while(~scanf("%d%d",&n,&m)){
        int a,b;
        memset(d,0,sizeof(d));
        for(int i=1;i<=m;i++){
            scanf("%d%d",&a,&b);
            d[a][b]=1;
        }
        Floyd();
        int ans=0;
        for(int i=1;i<=n;i++){
            int sum=0;
            for(int j=1;j<=n;j++)
                if(d[i][j]||d[j][i])    
                    sum++;
            if(sum==n-1)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值