以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;
}
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:
Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?
The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.
2 2 1
1
3 1 3 2
-1
2 1 2
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;
}
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.
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
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.
6 1
3.500000000000
6 3
4.958333333333
2 2
1.750000000000
Consider the third test example. If you've made two tosses:
- You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
- You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
- You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
- 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;
}
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.
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.
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).
5 3 0 0 0 3 3 0 0 6 4 1 0 6 3 3 0 3 3 3 2
yes yes yes no no
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;
}
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.
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 a1, a2, ..., an (1 ≤ ai ≤ 105).
Print a single integer — the maximum number of points that Alex can earn.
2 1 2
2
3 1 2 3
4
9 1 2 1 3 2 2 2 2 3
10
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.
#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;
}