CSUFTACM2017寒假习题解析(第4—6题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39360985/article/details/79140377

第四题:

Jenny likes balls. He has some balls and he wants to arrange them in a row on the table.
Each of those balls can be one of three possible colors: red, yellow, or blue. More precisely, Jenny has R red balls, Y yellow balls and B blue balls. He may put these balls in any order on the table, one after another. Each time Jenny places a new ball on the table, he may insert it somewhere in the middle (or at one end) of the already-placed row of balls.
Additionally, each time Jenny places a ball on the table, he scores some points (possibly zero). The number of points is calculated as follows:
1.For the first ball being placed on the table, he scores 0 point.
2.If he places the ball at one end of the row, the number of points he scores equals to the number of different colors of the already-placed balls (i.e. expect the current one) on the table.
3.If he places the ball between two balls, the number of points he scores equals to the number of different colors of the balls before the currently placed ball, plus the number of different colors of the balls after the current one.
What’s the maximal total number of points that Jenny can earn by placing the balls on the table?

Input:

There are several test cases, please process till EOF.
Each test case contains only one line with 3 integers R, Y and B, separated by single spaces. All numbers in input are non-negative and won’t exceed 10 9.

Output:

For each test case, print the answer in one line.

Sample Input:

2 2 2
3 3 3
4 4 4

Sample Output:

15
33
51

此题大意解读:

有三种颜色的球若干,每次向桌子上放一个球,保证是一条序列,每次放球的得分为当前放入序列的球的前面有多少种不同的颜色a,后面的有多少种不同的颜色b,a+b。问给定球的数量后,最大得分为多少。

具体代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;

int main()
{
    long long r,y,b;
    while(~scanf("%lld%lld%lld",&r,&y,&b))
    {
        long long k=0;
        long long ans=0;
        if(r>2)
        {
            k+=r-2;
            r=2;
        }
        if(y>2)
        {
            k+=y-2;
            y=2;
        }
        if(b>2)
        {
            k+=b-2;
            b=2;
        }
        long long sum=0;
        sum=r+y+b;
        if(sum==0)
        {
            ans+=0;
            y=0;
        }
        if(sum==1)
        {
            ans+=0;
            y=0;
        }
        if(sum==2)
        {
            ans+=1;
            y=2;
        }
        if(sum==3)
        {
            ans+=3;
            y=3;
        }
        if(sum==4)
        {
            ans+=6;
            y=4;
        }
        if(sum==5)
        {
            ans+=10;
            y=5;
        }
        if(sum==6)
        {
            ans+=15;
            y=6;
        }
        ans+=k*y;
        printf("%lld\n",ans);
    }
}

第五题:

ZZX has a sequence of boxes numbered 1,2,…,n. Each box can contain at most one ball.
You are given the initial configuration of the balls. For 1≤i≤n, if the i-th box is empty then a[i]=0, otherwise the i-th box contains exactly one ball, the color of which is a[i], a positive integer. Balls with the same color cannot be distinguished.

He will perform m operations in order. At the i-th operation, he collects all the balls from boxes l[i],l[i]+1,…,r[i]-1,r[i], and then arbitrarily put them back to these boxes. (Note that each box should always contain at most one ball)

He wants to change the configuration of the balls from a[1..n] to b[1..n] (given in the same format as a[1..n]), using these operations. Please tell him whether it is possible to achieve his goal.

Input:

First line contains an integer t. Then t testcases follow.
In each testcase: First line contains two integers n and m. Second line contains a[1],a[2],…,a[n]. Third line contains b[1],b[2],…,b[n]. Each of the next m lines contains two integers l[i],r[i].

1<=n<=1000,0<=m<=1000, sum of n over all testcases <=2000, sum of m over all testcases <=2000.

0<=a[i],b[i]<=n.

1<=l[i]<=r[i]<=n.

Output:

For each testcase, print “Yes” or “No” in a line.

Sample Input:

5
4 1
0 0 1 1
0 1 1 1
1 4
4 1
0 0 1 1
0 0 2 2
1 4
4 2
1 0 0 0
0 0 0 1
1 3
3 4
4 2
1 0 0 0
0 0 0 1
3 4
1 3
5 2
1 1 2 2 0
2 2 1 1 0
1 3
2 4

Sample Output:

No
No
Yes
No
Yes

题意解读:

有n个盒子,每个盒子只能放一个球,球的种类用1~n表示,0为没有球。给出m次操作,每次把l到r的所有球取出来再随机放回去,问有没有可能转化为给定的状态。

具体代码:

#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#define MAX 100000
using namespace std;

int ans[MAX];

struct node
{
    int x,y;     //x表示颜色,y表示相应颜色小球的初始位置
}a[MAX],b[MAX];

bool comp(node p,node q)
{
    if(p.x==q.x)
        return p.y<q.y;
    return p.x<q.x;
}

int main()
{
    int T,i,j,n,m,x,y;
    cin>>T;
    while(T--)
    {
        int flag=0;
        cin>>n>>m;
        for(i=1;i<=n;i++)
        {
            cin>>a[i].x;
            a[i].y=i;
        }
        sort(a+1,a+n+1,comp);
        for(i=1;i<=n;i++)
        {
            cin>>b[i].x;
            b[i].y=i;
        }
        sort(b+1,b+n+1,comp);
        for(i=1;i<=n;i++)
        {
            if(a[i].x==b[i].x)
                ans[a[i].y]=b[i].y;
            else
                flag=1;
        }
        for(i=1;i<=m;i++)
        {
            cin>>x>>y;
            sort(ans+x,ans+y+1);
        }
        for(i=1;i<=n;i++)
        {
            if(ans[i]!=i)
            {
                flag=1;
                break;
            }
        }
        if(flag)
            cout<<"No"<<endl;
        else
            cout<<"Yes"<<endl;
    }
    return 0;
}

第六题:

n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?

Input:

第一行包含两个正整数n (0 接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。

Output:

一行一个整数表示最少需要的独木舟数。

Sample Input:

3 6
1
2
3

Sample Output:

2

具体代码:

#include <iostream>
#include <algorithm>
using namespace std;

int a[10005];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    int j=0,k=n-1,ans=0;
    while(j<=k)
    {
        if(a[j]+a[k]<=m)
            j++;
        k--;
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39360985/article/details/79140377
今日推荐