266 C:
You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.
More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that .
The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1], a[2], ..., a[n] (|a[i]| ≤ 109) — the elements of array a.
Print a single integer — the number of ways to split the array into three parts with the same sum.
5 1 2 3 0 3
2
4 0 1 -1 0
1
2 4 1
0
给你N个数,问是否能分成三份使得sum相等,可以输出方法数:
思路:模拟,对1/3点和2/3点进行讨论,1/3点处,方法数可以增1,在2/3点处,可以累加上当前的方法数。
鑫神给出了另一种思路:统计1/3点出连续的0和2/3点处之后连续的零,但是貌似细节有一点点问题,方法目测也行,因为的确就是到了1/3点处和2/3点处,只有碰到连续0才能够使方法数增加。
#include
#include
#include
#include
#include
using namespace std;
const long long maxn=(long long )(500005);
long long a[maxn];
long long n;
int main()
{
long long sum=0;
cin>>n;
for(long long i=0;i>a[i];
sum+=a[i];
}
if(sum%3)
{
cout<<0<
The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.
Given a sequence of n integers p1, p2, ..., pn. You are to choose k pairs of integers:
in such a way that the value of sum is maximal possible. Help George to cope with the task.
The first line contains three integers n, m and k (1 ≤ (m × k) ≤ n ≤ 5000). The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ 109).
Print an integer in a single line — the maximum possible value of sum.
5 2 1 1 2 3 4 5
9
7 1 3 2 10 7 18 5 33 0
61
要求区间元素和,当然想求出数列的前缀和来了。
思路:二维dp,dp[i][j]表示在取到第i个元素的时候取j个m长度的区间之和的最大值。
dp[i][j]可由dp[i-1][j]不添加区间得到的,或者由dp[i-m][j-1]添加一个区间得到。
遂有状态转移方程:dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+s[i]-s[i-m]),其中s是前缀和。
#include
#include
#include
#include
#include
using namespace std;
long long a[5005];
long long dp[5005][5005];
long long sum[5005];
int main()
{
int n,m,k;
cin>>n>>m>>k;
sum[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
memset(dp,0,sizeof(dp));
for(int i=m;i<=n;i++)
{
dp[i][0]=max(dp[i][0],dp[i-1][0]);
for(int j=1;j<=k;j++)
{
dp[i][j]=max(dp[i][j],dp[i-1][j]);
dp[i][j]=max(dp[i][j],dp[i-m][j-1]+sum[i]-sum[i-m]);
}
}
cout<
Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game.
Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote them a and b, erase them from the sequence, and append to the sequence either a + b, or a - b, or a × b.
After n - 1 steps there is only one number left. Can you make this number equal to 24?
The first line contains a single integer n (1 ≤ n ≤ 105).
If it's possible, print "YES" in the first line. Otherwise, print "NO" (without the quotes).
If there is a way to obtain 24 as the result number, in the following n - 1 lines print the required operations an operation per line. Each operation should be in form: "a op b = c". Where a and b are the numbers you've picked at this operation; op is either "+", or "-", or "*"; c is the result of corresponding operation. Note, that the absolute value of c mustn't be greater than 1018. The result of the last operation must be equal to 24. Separate operator sign and equality sign from numbers with spaces.
If there are multiple valid answers, you may print any of them.
1
NO
8
YES 8 * 7 = 56 6 * 5 = 30 3 - 4 = -1 1 - 2 = -1 30 - -1 = 31 56 - 31 = 25 25 + -1 = 24利用1-N各一次进行加减乘除凑24点。。。。
思路:分奇偶讨论,偶数时1-4相乘得24 然后剩下N-4个数相邻两个数大减小得1,再乘上先前得到的24即可。奇数数,1-5通过某种形式凑到24,然后N-5个数又是偶数个数,同前面处理方式。。。
#include
#include
#include
#include
#include
using namespace std;
int n;
void slove(int n)
{
if(n%2==0)
{
printf("4 * 3 = 12\n");
printf("12 * 2 = 24\n");
printf("24 * 1 = 24\n");
for(int i=5;i<=n;i+=2)
{
printf("%d - %d = 1\n",i+1,i);
printf("24 * 1 = 24\n");
}
}
else
{
printf("4 * 5 = 20\n");
printf("2 + 3 = 5\n");
printf("20 + 5 = 25\n");
printf("25 - 1 = 24\n");
for(int i=6;i<=n;i+=2)
{
printf("%d - %d = 1\n",i+1,i);
printf("24 * 1 = 24\n");
}
}
}
int main ()
{
cin>>n;
if(n<=3)
cout<<"NO"<
C. MUH and House of Cards
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck of n playing cards. Let's describe the house they want to make:
The house consists of some non-zero number of floors.
Each floor consists of a non-zero number of rooms and the ceiling. A room is two cards that are leaned towards each other. The rooms are made in a row, each two adjoining rooms share a ceiling made by another card.
Each floor besides for the lowest one should contain less rooms than the floor below.
Please note that the house may end by the floor with more than one room, and in this case they also must be covered by the ceiling. Also, the number of rooms on the adjoining floors doesn't have to differ by one, the difference may be more.
While bears are practicing to put cards, Horace tries to figure out how many floors their house should consist of. The height of the house is the number of floors in it. It is possible that you can make a lot of different houses of different heights out of n cards. It seems that the elephant cannot solve this problem and he asks you to count the number of the distinct heights of the houses that they can make using exactly n cards.
Input
The single line contains integer n (1 ≤ n ≤ 1012) — the number of cards.
Output
Print the number of distinct heights that the houses made of exactly n cards can have.
Sample test(s)
Input
13
Output
1
Input
6
Output
0
没图不太好解释,大概就是要恰好用上N个卡片搭房子,然后问搭出满足条件的层数有多少种,还有个限制条件是每层房子的个数从上到下得是递增的。
首先看每个屋顶的规律,假设从上到下分别有a1 a2 a3 a4...ak的房屋,那么共需要卡片数为3*a1-1+3*a2-1+...3*ak-1=n有(a1+a2+...ak)=(n-k)/3那么显然这个数得是一个整数才能符合条件,第二由于是递增的房屋个数所以a1>=1,a2>=2....ak>=k又有(a1+a2+..ak)>=(k+1)*k/2,满足这两个条件,那么必然能够搭出一种满足题目要求的房子数,然后暴力枚举统计即可。
#include
#include
#include
#include
#include
using namespace std;
int main()
{
long long n;
long long cnt=0;
cin>>n;
for(long long i=1;i
270C:
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: .
The first line contains an integer n (1 ≤ n ≤ 105) — the number of people.
The next n lines each contains two strings. The i-th line contains strings fi and si (1 ≤ |fi|, |si| ≤ 50) — the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p1, p2, ..., pn (1 ≤ pi ≤ n).
If it is possible, output "YES", otherwise output "NO".
3 gennady korotkevich petr mitrichev gaoyuan chen 1 2 3
NO
3 gennady korotkevich petr mitrichev gaoyuan chen 3 1 2
YES
2 galileo galilei nicolaus copernicus 2 1
YES
10 rean schwarzer fei claussell alisa reinford eliot craig laura arseid jusis albarea machias regnitz sara valestin emma millstein gaius worzel 1 2 3 4 5 6 7 8 9 10
NO
10 rean schwarzer fei claussell alisa reinford eliot craig laura arseid jusis albarea machias regnitz sara valestin emma millstein gaius worzel 2 4 9 6 5 7 1 3 8 10
YES
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright.
思路:首先因为要是一个递增的顺序,所以考虑该人的名和姓时,要让字符串小的放在前面作为序号,这样可以使得更多的人可能能继续添加,然后将当前的字符串设为此人的较小字符串,然后新串来时,看是否能够更新,如果能则能继续放置,否则必然是不符合条件了,全部符合出来后YES即可,否则里面就直接NOreturn 0
用string可以直接比大小好方便诶ORZZZZZ。
#include
#include
#include
#include
#include
#include
using namespace std;
string a[100005],b[100005];
string c="";
int n;
int p[100005];
int main()
{
cin>>n;
for(int i=0;i>a[i]>>b[i];
}
for(int i=0;i>p[i];
p[i]--;
}
int t;
for(int i=0;ib[t])
{
swap(a[t],b[t]);
}
if(c