组队赛(第七周)

C F是DP
A B D E水题

A - Nth Largest Value

4552 Nth Largest Value
For this problem, you will write a program that prints the N-th largest value in a fixed sized array
of integers. To make things simple, N will be 3 and the array will always be have 10 decimal integer
values.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets
that follow. Each data set consists of a single line containing the data set number, followed by a space,
followed by 10 space separated decimal integers whose values are between 1 and 1000 inclusive.
Output
For each data set, generate one line of output with the following values: The data set number as a
decimal integer, a space, and the 3rd largest value of the corresponding 10 integers.
Sample Input
4
1 1 2 3 4 5 6 7 8 9 1000
2 338 304 619 95 343 496 489 116 98 127
3 931 240 986 894 826 640 965 833 136 138
4 940 955 364 188 133 254 501 122 768 408
Sample Output
1 8
2 489
3 931
4 768
(水题)找第三大的数

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;

typedef long long LL;

int main()
{
    ios::sync_with_stdio(false);

    int t,n,nn;
    int a[1000+10];

    cin>>t;


    while(t--)
    {
        cin>>nn;

        for(int i=0;i<10;i++)
            cin>>a[i];

        sort(a,a+10);

        cout<<nn<<' '<<a[7]<<endl;

    }
}

B - Equal Sum Partitions
4553 Equal Sum Partitions
An equal sum partition of a sequence of numbers is a grouping of the numbers (in the same order
as the original sequence) in such a way that each group has the same sum. For example, the sequence:
2 5 1 3 3 7
may be grouped as:
(2 5) (1 3 3) (7)
to yield an equal sum of 7.
Note: The partition that puts all the numbers in a single group is an equal sum partition with the
sum equal to the sum of all the numbers in the sequence.
For this problem, you will write a program that takes as input a sequence of positive integers and
returns the smallest sum for an equal sum partition of the sequence.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets
that follow. The first line of each data set contains the data set number, followed by a space, followed
by a decimal integer M, (1 ≤ M ≤ 10000), giving the total number of integers in the sequence. The
remaining line(s) in the dataset consist of the values, 10 per line, separated by a single space. The last
line in the dataset may contain less than 10 values.
Output
For each data set, generate one line of output with the following values: The data set number as a
decimal integer, a space, and the smallest sum for an equal sum partition of the sequence.
Sample Input
3
1 6
2 5 1 3 3 7
2 6
1 2 3 4 5 6
3 20
1 1 2 1 1 2 1 1 2 1
1 2 1 1 2 1 1 2 1 1
Sample Output
1 7
2 21
3 2
找这一堆数能够分成几个集合,使得每个集合的所有元素的和是相等的,必须是相邻的数才能划分到一个集合中

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;

typedef long long LL;

int m;
 int a[10000+10];

int judge(int x)
{
    int sum=0;

    for(int i=0;i<m;i++)
    {
        sum+=a[i];

        if(sum==x)
            sum=0;

        else if(sum>x)
            return 0;
    }

    if(sum!=0)
        return 0;
    return 1;
}

int main()
{
    ios::sync_with_stdio(false);

    int t,n;



    int ans=0,sum1=0;

    int flag=0;

    cin>>t;

    while(t--)
    {
        flag=0;
        sum1=0;
        ans=0;

        cin>>n>>m;

        for(int i=0;i<m;i++)
        {
            cin>>a[i];
            sum1+=a[i];
        }

        for(int i=1;i<=sum1;i++)
        {
            if(judge(i)==1)
            {
                ans=i;
                break;
            }
        }

            cout<<n<<' '<<ans<<endl;
    }

}

C - Balls
4554 Balls
The classic Two Glass Balls brain-teaser is often posed as:
“Given two identical glass spheres, you would like to determine the lowest floor in a 100-story
building from which they will break when dropped. Assume the spheres are undamaged
when dropped below this point. What is the strategy that will minimize the worst-case
scenario for number of drops?”
Suppose that we had only one ball. We’d have to drop from each floor from 1 to 100 in sequence,
requiring 100 drops in the worst case.
Now consider the case where we have two balls. Suppose we drop the first ball from floor n. If it
breaks we’re in the case where we have one ball remaining and we need to drop from floors 1 to n − 1
in sequence, yielding n drops in the worst case (the first ball is dropped once, the second at most n − 1
times). However, if it does not break when dropped from floor n, we have reduced the problem to
dropping from floors n + 1 to 100. In either case we must keep in mind that we’ve already used one
drop. So the minimum number of drops, in the worst case, is the minimum over all n.
You will write a program to determine the minimum number of drops required, in the worst case,
given B balls and an M-story building.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that
follow. Each data set consists of a single line containing three (3) decimal integer values: the problem
number, followed by a space, followed by the number of balls B, (1 ≤ B ≤ 50), followed by a space and
the number of floors in the building M, (1 ≤ M ≤ 1000).
Output
For each data set, generate one line of output with the following values: The data set number as a
decimal integer, a space, and the minimum number of drops needed for the corresponding values of B
and M.
Sample Input
4
1 2 10
2 2 100
3 2 300
4 25 900
Sample Output
1 4
2 14
3 24
4 10
比赛时读不懂题意,死活想不出来这是个DP。。。
题意:有一个大楼,他有n层,有m个鸡蛋,如果在第i层扔一个鸡蛋没碎,但是在第i+1层扔一个鸡蛋碎了,那么这个鸡蛋的硬度就是i,问最坏情况下最少扔多少次鸡蛋能得知硬度是多少
思路:对于每一幢大楼,有n层m个鸡蛋,从第k层楼扔下鸡蛋,如果碎了,那么应该从k-1层一下开始扔,dp[k,m]=dp[k-1,m-1]。反之,如果没碎,那就要从k+1层以上扔,那么就是dp[i-k,m],i表示一共几层楼,m表示有多少个鸡蛋,k表示在这幢大楼里是在第几层扔下来的,因为要最坏情况,所以应该取max(dp[k-1,j-1],dp[i-k,j]),因为这一次扔也是一次,所以应该在加1,要取min(dp[i,j],max(dp[k-1,j-1],dp[i-k,j])+1),数组要赋值为INF
离线;

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int dp[1000+50][60];
void f()
{
    memset(dp,INF,sizeof(dp));
    for(int j=0;j<=50;j++)
           dp[0][j]=0;
    for(int i=1;i<=1000;i++)
        for(int j=1;j<=50;j++)
          for(int k=1;k<=i;k++)
            dp[i][j]=min(dp[i][j],max(dp[k-1][j-1],dp[i-k][j])+1);

}
int main()
{
    int t,num,n,m;
    ios::sync_with_stdio(false);
    f();
    cin>>t;
    while(t--)
    {
        cin>>num>>n>>m;
        cout<<num<<" "<<dp[m][n]<<endl;
    }
    return 0;
}

D - Running Median
4555 Running Median
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After
each odd-indexed value is read, output the median (middle value) of the elements received so far.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that
follow. The first line of each data set contains the data set number, followed by a space, followed by an
odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed.
The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space. The
last line in the dataset may contain less than 10 values.
Output
For each data set the first line of output contains the data set number, a single space and the number of
medians output (which should be one-half the number of input values plus one). The output medians
will be on the following lines, 10 per line separated by a single space. The last line may have less than
10 elements, but at least 1 element.
There should be no blank lines in the output.
Sample Input
3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56
Sample Output
1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3
这个题,,,卡了好久的格式
题意很简单,就是输出每个奇数位数上的前面这些数的中位数,10个一换行,,最后不需要另做处理就对了,,试了无数的样例,,最后发现该有最后一个换行,WA了五次,哭唧唧

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;

typedef long long LL;

int main()
{
    LL t,n,m,x,y;

    LL a[10000+10];
    LL ans[10000];
    LL flag=0;
    LL sum=0;

    scanf("%lld",&t);
    LL d=0;
    while(t--)
    {
        scanf("%lld %lld",&n,&m);
        d=0;
        for(LL i=1; i<=m; i++)
        {
            scanf("%lld",&a[i]);
            if(i%2!=0)
            {


                sort(a+1,a+i+1);
                ans[++d]=a[i/2+1];
            }
        }

        printf("%lld %lld\n",n,m/2+1);

        LL b=0;
        if(d==10)
        {

            for(LL i=1; i<=d; i++)
            {

                if((i-1)%10==0)
                    printf("%lld",ans[i]);
                else
                    printf(" %lld",ans[i]);
            }

                printf("\n");

        }
        else
        {
            for(LL i=1; i<=d; i++)
            {
                b++;
                if((i-1)%10==0)
                    printf("%lld",ans[i]);
                else
                    printf(" %lld",ans[i]);
                if(b%10==0&&b!=d)
                    printf("\n");
            }

                printf("\n");
        }
    }

}

E - The Next Permutation
4556 The Next Permutation
For this problem, you will write a program that takes a (possibly long) string of decimal digits, and
outputs the permutation of those decimal digits that has the next larger value (as a decimal number)
than the input number. For example:
123 -> 132
279134399742 -> 279134423799
It is possible that no permutation of the input digits has a larger value. For example, 987.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that
follow. Each data set is a single line that contains the data set number, followed by a space, followed
by up to 80 decimal digits which is the input value.
Output
For each data set there is one line of output. If there is no larger permutation of the input digits,
the output should be the data set number followed by a single space, followed by the string ‘BIGGEST’.
If there is a solution, the output should be the data set number, a single space and the next larger
permutation of the input digits.
Sample Input
3
1 123
2 279134399742
3 987
Sample Output
1 132
2 279134423799
3 BIGGEST

找出仅仅比这个数大的下一个数,如果没有就输出BIGGEST
没有用过next_permutation这个函数,就硬生生的写的
如果每一个数都小于等于前一个数,那么就是BIGGEST,如果不是就从最后一个开始往前找,找到一个比后一个数小的数,就记录下来位置,然后往后找那堆数里比这个位置大的并且在那一堆里最小的那个数交换她俩的位置,剩下的数从小到大排,依次输出

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f
const int MAX=1e6;
using namespace std;
char a[100];
int s[100];
int main()
{
    int t;
    cin>>t;
    int n,flag,d,k,kk,minn=58;
    char nn;
    while(t--)
    {
        d=0;
        minn=58;
        memset(s,0,sizeof(s));
        for(int i=0;i<80;i++)
            a[i]='0';
        cin>>n;
        flag=0;
        scanf("%s",a);
        int len=strlen(a);
        for(int i=1; i<len; i++)
            if(a[i]>a[i-1])
            {
                flag=1;
                break;
            }
        if(flag==0)
        {
            cout<<n<<" BIGGEST"<<endl;
            continue;
        }
        for(int i=len-1; i>=0; i--)
        {
            if(a[i-1]>=a[i])
                continue;
            else
            {
                k=i-1;

                for(int j=k+1; j<len; j++)
                    if(a[j]<minn&&a[j]>a[k])
                       kk=j,minn=a[j];
                nn=minn,minn=a[k],a[k]=nn;
                a[kk]=minn;
                for(int j=k+1; j<len; j++)
                    s[++d]=a[j]-'0';
                sort(s+1,s+d+1);
//                 for(int j=1;j<=d;j++)
//                    cout<<s[j];
//                 cout<<endl;
                d=0;
                int j;
                for( j=k+1; j<len; j++)
                    a[j]=s[++d]+'0';
                a[j]='\0';
                break;
            }

        }
        printf("%d %s\n",n,a);
    }
    return 0;
}

另一种就是赛后补得用STL里的那个函数

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
char s[100];
char c[100];
int main()
{
    int t,n,flag;
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        flag=0;
        cin>>n>>s;
        strcpy(c,s);
        do
        {
            if(strcmp(s,c)>0)
            {
                printf("%d %s\n",n,s);
                flag=1;
                break;
            }

        }while(next_permutation(s,s+strlen(s)));
        if(flag==0)
            printf("%d BIGGEST\n",n);
    }

    return 0;
}

F - Adjacent Bit Counts
4557 Adjacent Bit Counts
For a string of n bits x1, x2, x3,…, xn, the adjacent bit count of the string (AdjBC(x)) is given by
x1 ∗ x2 + x2 ∗ x3 + x3 ∗ x4 + . . . + xn−1 ∗ xn
which counts the number of times a 1 bit is adjacent to another 1 bit. For example:
AdjBC(011101101) = 3
AdjBC(111101101) = 4
AdjBC(010101010) = 0
Write a program which takes as input integers n and k and returns the number of bit strings x of n
bits (out of 2
n
) that satisfy AdjBC(x) = k. For example, for 5 bit strings, there are 6 ways of getting
AdjBC(x) = 2:
11100, 01110, 00111, 10111, 11101, 11011
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that
follow. Each data set is a single line that contains the data set number, followed by a space, followed by
a decimal integer giving the number (n) of bits in the bit strings, followed by a single space, followed by
a decimal integer (k) giving the desired adjacent bit count. The number of bits (n) will not be greater
than 100 and the parameters n and k will be chosen so that the result will fit in a signed 32-bit integer.
Output
For each data set there is one line of output. It contains the data set number followed by a single space,
followed by the number of n-bit strings with adjacent bit count equal to k.
Sample Input
10
1 5 2
2 20 8
3 30 17
4 40 24
5 50 37
6 60 52
7 70 59
8 80 73
9 90 84
10 100 90
Sample Output
1 6
2 63426
3 1861225
4 168212501
5 44874764
6 160916
7 22937308
8 99167
9 15476
10 23076518

比赛的时候一直想的是组合数学,原来也是个DP
题意:一共有n个数,每一位只能是0或者1,权值是k,计算权值的方法是x1 ∗ x2 + x2 ∗ x3 + x3 ∗ x4 + . . . + xn−1 ∗ xn,要想满足这个条件有多少种方式
可以用三维数组来表示,dp[i,j,k] i表示一共有i位数,j表示权值,k表示末位的数是0还是1,那么,dp[i,j,0]=dp[i-1,j,0]+dp[i-1,j,1]
dp[i,j,1]=dp[i-1,j-1,1]+dp[i-1,j,0]
初始化n个数构成权值为0的时候只需dp[i,0,0]=dp[i-1,0,1]+dp[i-1,0,0]
dp[i,0,1]=dp[i-1,0,0]
CODE:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int dp[105][105][3];
void f()
{
    dp[1][0][0]=dp[1][0][1]=1;
    for(int i=2;i<=100;i++)
    {
        dp[i][0][0]=dp[i-1][0][0]+dp[i-1][0][1];
        dp[i][0][1]=dp[i-1][0][0];
        for(int j=1;j<i;j++)
        {
            dp[i][j][1]=dp[i-1][j][0]+dp[i-1][j-1][1];
            dp[i][j][0]=dp[i-1][j][0]+dp[i-1][j][1];
        }
    }
}
int main()
{
    int t,num,n,k;
    ios::sync_with_stdio(false);
    cin>>t;
    f();
    while(t--)
    {
        cin>>num>>n>>k;
        cout<<num<<" "<<dp[n][k][0]+dp[n][k][1]<<endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值