2014牡丹江区域赛题解

181 篇文章 0 订阅
173 篇文章 3 订阅


A题:

Link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5373


Average Score

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Bob is a freshman in Marjar University. He is clever and diligent. However, he is not good at math, especially in Mathematical Analysis.

After a mid-term exam, Bob was anxious about his grade. He went to the professor asking about the result of the exam. The professor said:

"Too bad! You made me so disappointed."

"Hummm... I am giving lessons to two classes. If you were in the other class, the average scores of both classes will increase."

Now, you are given the scores of all students in the two classes, except for the Bob's. Please calculate the possible range of Bob's score. All scores shall be integers within [0, 100].

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N (2 <= N <= 50) and M (1 <= M <= 50) indicating the number of students in Bob's class and the number of students in the other class respectively.

The next line contains N - 1 integers A1A2, .., AN-1 representing the scores of other students in Bob's class.

The last line contains M integers B1B2, .., BM representing the scores of students in the other class.

Output

For each test case, output two integers representing the minimal possible score and the maximal possible score of Bob.

It is guaranteed that the solution always exists.

Sample Input
2
4 3
5 5 5
4 4 3
6 5
5 5 4 5 3
1 3 2 2 1
Sample Output
4 4
2 4

Author:  JIANG, Kai
Source:  The 2014 ACM-ICPC Asia Mudanjiang Regional Contest


AC code:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int A[55],B[55];
int sum1,avg1,sum2,avg2;
int main()
{
    int i,T,j,n,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        sum1=0;
        sum2=0;
        for(i=1;i<=n-1;i++)
        {
            scanf("%d",&A[i]);
            sum1+=A[i];
        }
        avg1=sum1/(n-1);
        if(avg1*(n-1)==sum1)
            avg1--;
        for(i=1;i<=m;i++)
        {
            scanf("%d",&B[i]);
            sum2+=B[i];
        }
        avg2=sum2/m+1;
        printf("%d %d\n",avg2,avg1);
    }
    return 0;
}



I题:


Information Entropy

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about information entropy.

Entropy is the average amount of information contained in each message received. Here, a message stands for an event, or a sample or a character drawn from a distribution or a data stream. Entropy thus characterizes our uncertainty about our source of information. The source is also characterized by the probability distribution of the samples drawn from it. The idea here is that the less likely an event is, the more information it provides when it occurs.

Generally, "entropy" stands for "disorder" or uncertainty. The entropy we talk about here was introduced by Claude E. Shannon in his 1948 paper "A Mathematical Theory of Communication". We also call it Shannon entropy or information entropy to distinguish from other occurrences of the term, which appears in various parts of physics in different forms.

Named after Boltzmann's H-theorem, Shannon defined the entropy Η (Greek letter Η, η) of a discrete random variable X with possible values {x1, x2, ..., xn} and probability mass function P(X) as:

H(X)=E(ln(P(x)))

Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as

H(X)=i=1nP(xi)log b(P(xi))

Where b is the base of the logarithm used. Common values of b are 2, Euler's number e, and 10. The unit of entropy is bit for b = 2, nat for b = e, and dit (or digit) for b = 10 respectively.

In the case of P(xi) = 0 for some i, the value of the corresponding summand 0 logb(0) is taken to be a well-known limit:

0log b(0)=limp0+plog b(p)

Your task is to calculate the entropy of a finite sample with N values.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 100) and a string S. The string S is one of "bit", "nat" or "dit", indicating the unit of entropy.

In the next line, there are N non-negative integers P1P2, .., PNPi means the probability of the i-th value in percentage and the sum of Pi will be 100.

Output

For each test case, output the entropy in the corresponding unit.

Any solution with a relative or absolute error of at most 10-8 will be accepted.

Sample Input
3
3 bit
25 25 50
7 nat
1 2 4 8 16 32 37
10 dit
10 10 10 10 10 10 10 10 10 10
Sample Output
1.500000000000
1.480810832465
1.000000000000

Author:  ZHOU, Yuchen
Source:  The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

编程思想:模拟。

AC code:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
double p[111];
char s[5];
double h;
int main()
{
    int T,i,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%s",&n,s);
        for(i=1;i<=n;i++)
        {
            scanf("%lf",&p[i]);
        }
        h=0;
        if(strcmp(s,"bit")==0)
        {
            for(i=1;i<=n;i++)
            {
                if(p[i]!=0)
                h+=p[i]*0.01*(log(p[i]*0.01)/log(2.0));
            }
            h*=-1;
        }
        else if(strcmp(s,"nat")==0)
        {
            for(i=1;i<=n;i++)
            {
                if(p[i]!=0)
                h+=p[i]*0.01*(log(p[i]*0.01));
            }
            h*=-1;
        }
        else
        {
            for(i=1;i<=n;i++)
            {
                if(p[i]!=0)
                h+=p[i]*0.01*(log10(p[i]*0.01));
            }
            h*=-1;
        }
        printf("%.12f\n",h);
    }
    return 0;
}






Known Notation

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in Marjar University. He is learning RPN recent days.

To clarify the syntax of RPN for those who haven't learnt it before, we will offer some examples here. For instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand. The arithmetic expression written "3 - 4 + 5" in conventional notation would be written "3 4 - 5 +" in RPN: 4 is first subtracted from 3, and then 5 added to it. Another infix expression "5 + ((1 + 2) × 4) - 3" can be written down like this in RPN: "5 1 2 + 4 × + 3 -". An advantage of RPN is that it obviates the need for parentheses that are required by infix.

In this problem, we will use the asterisk "*" as the only operator and digits from "1" to "9" (without "0") as components of operands.

You are given an expression in reverse Polish notation. Unfortunately, all space characters are missing. That means the expression are concatenated into several long numeric sequence which are separated by asterisks. So you cannot distinguish the numbers from the given string.

You task is to check whether the given string can represent a valid RPN expression. If the given string cannot represent any valid RPN, please find out the minimal number of operations to make it valid. There are two types of operation to adjust the given string:

  1. Insert. You can insert a non-zero digit or an asterisk anywhere. For example, if you insert a "1" at the beginning of "2*3*4", the string becomes "12*3*4".
  2. Swap. You can swap any two characters in the string. For example, if you swap the last two characters of "12*3*4", the string becomes "12*34*".

The strings "2*3*4" and "12*3*4" cannot represent any valid RPN, but the string "12*34*" can represent a valid RPN which is "1 2 * 34 *".

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is a non-empty string consists of asterisks and non-zero digits. The length of the string will not exceed 1000.

Output

For each test case, output the minimal number of operations to make the given string able to represent a valid RPN.

Sample Input
3
1*1
11*234**
*
Sample Output
1
0
2




编程思想:贪心。

AC code:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
char s[1010];
int len;
int ans;
int num,star;
int main()
{
    int T,i,n;
    scanf("%d",&T);
    while(T--)
    {
        //scanf("%s",s);
        cin>>s;
        star=0;
        num=0;
        len=strlen(s);
        for(i=0;i<len;i++)
        {
            if(s[i]=='*')
                star++;
            else
                num++;
        }
        ans=0;
        if(num<star+1)
            ans+=(star+1-num);//将数字补在前面
        int left_num=ans;
        for(i=0;i<len;i++)
        {
            if(s[i]=='*')
            {
                if(left_num<2)
                {
                    ans++;//交换加一
                    left_num++;//交换后增加了一个数
                }
                else
                {
                    left_num--;//两个数加一个运算符变成一个数
                }
            }
            else
            {
                left_num++;//左边的数加一
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值