BestCoder Round #86题解报告

来源:互联网 发布:lol域名不能备案 编辑:程序博客网 时间:2024/06/05 16:08

题目链接:Contest Problem List  http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=721

                    http://acm.hdu.edu.cn/search.php?field=problem&key=BestCoder+Round+%2386&source=1&searchmode=source

Problem 1001 Price List

Accept: 0    Submit: 0
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit : 262144/131072 K (Java/Others)

BestCoder Round #86题解报告 Problem Description

There are n shops numbered with successive integers from 1 to n in Byteland. Every shop sells only one kind of goods, and the price of the i-th shop's goods is vi.
Every day, Byteasar will purchase some goods. He will buy at most one piece of goods from each shop. Of course, he can also choose to buy nothing. Back home, Byteasar will calculate the total amount of money he has costed that day and write it down on his account book.
However, due to Byteasar's poor math, he may calculate a wrong number. Byteasar would not mind if he wrote down a smaller number, because it seems that he hadn't used too much money.
Please write a program to help Byteasar judge whether each number is sure to be strictly larger than the actual value. 
在Byteland一共有n家商店,编号依次为1到n。每家商店只会卖一种物品,其中第i家商店的物品单价为v​i。
Byteasar每天都会进行一次购物,他会在每家商店购买最多一件物品,当然他也可以选择什么都不买。回家之后,Byteasar会把这一天购物所花的钱的总数记录在账本上。
Byteasar的数学不好,他可能会把花的钱记少,也可能记多。Byteasar并不介意记少,因为这样看上去显得自己没花很多钱。
请写一个程序,帮助Byteasar判断每条记录是否一定记多了。 

BestCoder Round #86题解报告 Input

The first line of the input contains an integer T (1≤T≤10), denoting the number of test cases.
In each test case, the first line of the input contains two integers n,m (1≤n,m≤100000), denoting the number of shops and the number of records on Byteasar's account book.
The second line of the input contains n integers v1,v2,...,vn (1≤vi≤100000), denoting the price of the i-th shop's goods.
Each of the next m lines contains an integer q (0≤q≤10^18), denoting each number on Byteasar's account book. 
输入的第一行包含一个正整数T(1≤T≤10),表示测试数据的组数。
对于每组数据,第一行包含两个正整数n,m(1≤n,m≤100000),表示商店的个数和记录的个数。
第二行包含n个正整数v​i(1≤vi≤100000),依次表示每家商店的物品的单价。
接下来m行,每行包含一个整数q(0≤q≤10^18),表示一条记录。 

BestCoder Round #86题解报告 Output

For each test case, print a line with m characters. If the i-th number is sure to be strictly larger than the actual value, then the i-th character should be '1'. Otherwise, it should be '0'.

对于每组数据,输出一行m个字符,依次回答每个询问。如果一定记多了,请输出'1',否则输出'0'。

BestCoder Round #86题解报告 Sample Input

1
3 3
2 5 4
1
7
10000

BestCoder Round #86题解报告 Sample Output

001

解题思路:

这道题思路很简单,但是输出的时候晕了,“输出一行,依次回答每个询问,我开始还以为是要将结果存在一起输出,还有换行也没注意,第一遍就WA了5次;

刚才试了一下,将结果存在一个数组,依次输出,最后换行也能AC;

但是最后依然被hack了,前面定义的记录使用的__int64,后面的输入用的却是%d。。。。。彻底无语!

AC代码:

#include<stdio.h>int main(){int t,n,m,i;int a;__int64 b[110000];scanf("%d",&t);while(t--){__int64 sum=0;scanf("%d%d",&n,&m);for(i=1;i<=n;i++){scanf("%d",&a);sum=sum+a;}for(i=1;i<=m;i++){scanf("%I64d",&b[i]);if(b[i]>sum)printf("1");elseprintf("0");}printf("\n");}return 0; } 

  Problem 1002 NanoApe Loves Sequence

Accept: 0    Submit: 0
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit : 262144/131072 K (Java/Others)

BestCoder Round #86题解报告 Problem Description

NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination!

In math class, NanoApe picked up sequences once again. He wrote down a sequence with n numbers on the paper and then randomly deleted a number in the sequence. After that, he calculated the maximum absolute value of the difference of each two adjacent remained numbers, denoted as F.

Now he wants to know the expected value of F, if he deleted each number with equal probability.

退役狗 NanoApe 滚回去学文化课啦!

在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n 的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后他计算出了所有相邻两数的差的绝对值的最大值。

他当然知道这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。

BestCoder Round #86题解报告 Input

The first line of the input contains an integer T, denoting the number of test cases.

In each test case, the first line of the input contains an integer n, denoting the length of the original sequence.

The second line of the input contains n integers A1,A2,...,An, denoting the elements of the sequence.

1≤T≤10, 3≤n≤100000, 1≤Ai≤10^9

第一行为一个正整数 T,表示数据组数。

每组数据的第一行为一个整数 n。

第二行为 n 个整数 A​i,表示这个数列。

1≤T≤10, 3≤n≤100000, 1≤A​i≤10^​9

BestCoder Round #86题解报告 Output

For each test case, print a line with one integer, denoting the answer.

In order to prevent using float number, you should print the answer multiplied by n.

对于每组数据输出一行一个数表示答案。

为防止精度误差,你需要输出答案乘上 n 后的值。

BestCoder Round #86题解报告 Sample Input

1
4
1 2 3 4

BestCoder Round #86题解报告 Sample Output

6

解题思路:

这道题刚开始不知道期望是啥意思,百度一下,就是平均值。。。。。。最后要乘n,所以实际上就是求把1-n每个数去掉一编,然后把每次得到的最大相邻两个数的差求和即可;

新序列相对于原来的序列而言,就相当于挖掉了一个数,将该数的左边和右边拼凑成新序列,那我们只需知道左边的最大值与右边的最大值,再和拼接处产生的差值比较一下,就可以知道新序列的最大值即令l[i]表示前i个数两两相邻差值的绝对值的最大值,r[i]表示i到n范围内两两相邻差值的绝对值的最大值。那么枚举删除第i个数,加上删除该数时的序列最大值即可。ans+=max(abs(s[i+1]-s[i-1]),max(l[i-1],r[i+1]))
注意,在删除序列首尾的数时,要特判一下!!!

AC代码:

#include<stdio.h> #include<math.h>#include<string.h>#define  G  1100000  int max(int x,int y){if(x>y)  return x;else  return y; } int s[G];int l[G];int r[G];int main(){    int t,n,i;    __int64 ans;    scanf("%d",&t);    while(t--)    {        ans=0;        memset(l,0,sizeof(l));        memset(r,0,sizeof(r));        scanf("%d",&n);        for(i=0;i<n;i++)            scanf("%d",&s[i]);        for(i=1;i<n;i++)            l[i]=max(abs(s[i]-s[i-1]),l[i-1]);        for(i=n-2;i>=0;i--)            r[i]=max(abs(s[i+1]-s[i]),r[i+1]);        for(i=0;i<n;i++)            if(i==0)                ans+=r[i+1];            else if(i==n-1)                ans+=l[i-1];            else                ans+=max(abs(s[i+1]-s[i-1]),max(l[i-1],r[i+1]));        printf("%I64d\n",ans);    }    return 0;}

  Problem 1003 NanoApe Loves Sequence Ⅱ

Accept: 0    Submit: 0
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit : 262144/131072 K (Java/Others)

BestCoder Round #86题解报告 Problem Description

NanoApe, the Retired Dog, has returned back to prepare for for the National Higher Education Entrance Examination!

In math class, NanoApe picked up sequences once again. He wrote down a sequence with n numbers and a number m on the paper.

Now he wants to know the number of continous subsequences of the sequence in such a manner that the k-th largest number in the subsequence is no less than m.

Note : The length of the subsequence must be no less than k.

退役狗 NanoApe 滚回去学文化课啦!

在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n 的数列,他又根据心情写下了一个数 m。

他想知道这个数列中有多少个区间里的第 k 大的数不小于 m,当然首先这个区间必须至少要有 k 个数啦。

BestCoder Round #86题解报告 Input

The first line of the input contains an integer T, denoting the number of test cases.

In each test case, the first line of the input contains three integers n,m,k.

The second line of the input contains n integers A1,A2,...,An, denoting the elements of the sequence.

1≤T≤10, 2≤n≤200000, 1≤k≤n/2, 1≤m,Ai≤10^9

第一行为一个正整数 T,表示数据组数。
每组数据的第一行为三个整数 n,m,k。
第二行为 nn 个整数 A​i,表示这个数列。
1≤T≤10, 2≤n≤200000, 1≤k≤n/2, 1≤m,A​i≤10^9 
​​ 

BestCoder Round #86题解报告 Output

For each test case, print a line with one integer, denoting the answer.

对于每组数据输出一行一个数表示答案。

BestCoder Round #86题解报告 Sample Input

1
7 4 2
4 2 7 7 6 5 1

BestCoder Round #86题解报告 Sample Output

18

解题思路:

给定一个长度为n的数列,问有多少个区间的第k大数>=m;

第k大,是从大到小排序后第k个(不去重)。
我们只要记录以第i个数作为区间左端点时,右端点延伸到何处能够满足区间内有k个数>=m即可,因为右端点再往右延伸必定满足区间内有k个数>=m;
那么最终结果只需枚举左端点,加上右端点可以取的个数即可

AC代码:

#include<stdio.h> #define G 1100000int s[G];int r[G];int main(){    int t,n,m,k,i,j,c;    __int64 ans;    scanf("%d",&t);    while(t--)    {        ans=0;c=0;        scanf("%d%d%d",&n,&m,&k);        for(i=0;i<n;i++)        {            scanf("%d",&s[i]);            r[i]=n;        }        for(j=i=0;i<n;i++)        {            if(s[i]>=m)                c++;            while(c==k)            {                r[j]=i;                if(s[j]>=m)                    c--;                j++;            }        }        for(i=0;i<n;i++)            ans+=(n-r[i]);        printf("%I64d\n",ans);    }    return 0;}

参考地址:http://www.infocool.net/kb/OtherLanguage/201608/173388.html




0 0
原创粉丝点击