cf五道题

A. Little Pony and Crystal Mine
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n × n matrix with a diamond inscribed into it.

You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character "D". All other cells of the matrix should be represented by character "*". Look at the examples to understand what you need to draw.

Input

The only line contains an integer n (3 ≤ n ≤ 101n is odd).

Output

Output a crystal of size n.

Sample test(s)
input
3
output
*D*
DDD
*D*
input
5
output
**D**
*DDD*
DDDDD
*DDD*
**D**
input
7
output
***D***
**DDD**
*DDDDD*
DDDDDDD
*DDDDD*
**DDD**
***D***

以D的个数为计数标志就很容易得出规律,根据上下镜面对称,只需要把i倒序就行了
#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cin>>n;
    for(i=1;i<=n;i+=2)
    {
        for(j=1;j+j<=n-i;j++)
        cout<<'*';
        for(j=1;j<=i;j++)
        cout<<'D';
        for(j=1;j+j<=n-i;j++)
        cout<<'*';
        cout<<endl;
    }
    for(i=n-2;i>=1;i-=2)
    {
        for(j=1;j+j<=n-i;j++)
        cout<<'*';
        for(j=1;j<=i;j++)
        cout<<'D';
        for(j=1;j+j<=n-i;j++)
        cout<<'*';
        cout<<endl;
    }
    return 0;
}



B. Little Pony and Sort by Shift
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:

a1, a2, ..., an → an, a1, a2, ..., an - 1.

Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?

Input

The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.

Sample test(s)
input
2
2 1
output
1
input
3
1 3 2
output
-1
input
2
1 2
output
0
只有原本有序的序列可以,然后想成一个环,最小的那个点在的位置到最后一个数的个数就是答案
#include<cstdio>
int a[100005];
int main()
{
    int i,n,ok,s;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    ok=0,s=0;
    for(i=0;i<n-1;i++)
    {
        if(a[i]>a[i+1])
        ok++;
        if(ok)
        s++;
        if(ok>1)
        break;
    }
    if(a[n-1]<=a[0]&&ok==1||ok==0)
    printf("%d\n",s);
    else
    printf("-1\n");
    return 0;
}


C. Little Pony and Expected Maximum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.

The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains mdots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.

Input

A single line contains two integers m and n (1 ≤ m, n ≤ 105).

Output

Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10  - 4.

Sample test(s)
input
6 1
output
3.500000000000
input
6 3
output
4.958333333333
input
2 2
output
1.750000000000
Note

Consider the third test example. If you've made two tosses:

  1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
  2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
  3. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
  4. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.

The probability of each outcome is 0.25, that is expectation equals to:

You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value

数学期望公式:P = m - (1/m)^n - (2/m)^n - ... -((m-1)/m)^n

#include <cstdio>
#include <cstring>
#include <cmath>

int main()
{
    int m, n;
    scanf("%d%d", &m, &n);
    double ans = m;
    for(int i = 1; i < m; ++i)
        ans -= pow(i*1.0/m, n);
    printf("%.12f\n", ans);
    return 0;
}
C. Predict Outcome of the Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played.

You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these kgames. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.

You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?

Note that outcome of a match can not be a draw, it has to be either win or loss.

Input

The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105).

Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case.

Output

For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).

Sample test(s)
input
5
3 0 0 0
3 3 0 0
6 4 1 0
6 3 3 0
3 3 3 2
output
yes
yes
yes
no
no
Note

Sample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.

Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".

Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).

根据公式分四种情况讨论
//A + B + C = k ---(1)
//|A - B| = d1  ---(a)
//|B - C| = d2  ---(b)
#include<cstdio>
#include<iostream>

using namespace std;

#define LL long long
LL n,k,d1,d2,ave,x;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>k>>d1>>d2;
        if(n % 3)//如果n无法整除3,则无法达到三个平局
        {
            puts("no") ;
            continue ;
        }
        ave = n/3;
        bool flag = false ;
        //A>B&&B>C时,A-B=d1,B-C=d2
        //最小的是C,最大的是A
        //通过式子连立可求出C= (k-d1-2*d2)/3
        if( (k-d1-2*d2) % 3 == 0 && (k-d1-2*d2) >= 0)//C必须是整数并且大于等于0
        {
            x = (k-d1-2*d2)/3;
            if(x+d1+d2<=ave)//三者之中最大的是A,只要A小于等于平均值,BC都会满足
                flag = true;
        }
        //A>B&&B<C时,A-B=d1,B-C=-d2
        //最小的是B,AC无法辨大小
        //通过式子连立可求出B = (k-d1-d2)/3
        if( (k-d1-d2) % 3 == 0 && (k-d1-d2) >= 0)
        {
            x = (k-d1-d2)/3;
            if(x+d1<=ave&&x+d2<=ave)//AC比B大,只要判断AC是不是都小于平均数即可
                flag = true;
        }
        //A<B&&B<C时,A-B=-d1,B-C=-d2
        //最小的是A,C最大
        //通过式子连立可求出A = (k-2*d1-d2)/3
        if( (k-2*d1-d2) % 3 == 0 && (k-2*d1-d2) >= 0)
        {
            x = (k-2*d1-d2)/3;
            if(x+d1+d2 <= ave)
                flag = true;
        }
        //A<B&&B>C时,A-B=-d1,B-C=d2
        //最大的是B,AC无法辨大小
        //通过式子连立可求出A = (k-2*d1+d2)/3,C=(k+d1-2*d2)
        if( (k+d2-2*d1)%3==0 && (k+d2-2*d1)>=0 && (k+d1-2*d2)%3==0 && (k+d1-2*d2)>=0)
        {
            x = (k+d2-2*d1)/3;
            if(x+d1 <= ave)
                flag = true;
        }
        if(flag) puts("yes");
        else puts("no");
    }
    return 0;
}
C. Boredom
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.

Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player.

Alex is a perfectionist, so he decided to get as many points as possible. Help him.

Input

The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex's sequence.

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 105).

Output

Print a single integer — the maximum number of points that Alex can earn.

Sample test(s)
input
2
1 2
output
2
input
3
1 2 3
output
4
input
9
1 2 1 3 2 2 2 2 3
output
10
Note

Consider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points.

题意: 给出n个数,每次可以选择消除一个值为ai的数,那么所有ai-1,ai+1的数也会被消掉,同时会获得值ai,问最多可以获得多少?
这应该是一个dp问题,首先,哈希一下,存下每个数的个数放在p中,消除一个数i,会获得p[i]*i的值(因为可以消除p[i]次),如果从0的位置开始向右消去,那么,消除数i时,i-1可能选择了消除,也可能没有,如果消除了i-1,那么i值就已经不存在,dp[i] = dp[i-1],如果没有被消除,那么dp[i] = dp[i-2]+ p[i]*i。
#include <cstdio>
#include <algorithm>
using namespace std;
long long dp[100005],a[100005];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;++i)
	{
		int x;
		scanf("%d",&x);
		a[x]++;
	}
	dp[1]=a[1];
	for(int i=2;i<=100000;i++)
		dp[i]=max(dp[i-1],dp[i-2]+i*a[i]);
	printf("%I64d\n",dp[100000]);
	return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值