一些无脑的C题,CODEFORCE DIV2 266-270

266 C:

C. Number of Ways
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 .

Input

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.

Output

Print a single integer — the number of ways to split the array into three parts with the same sum.

Sample test(s)
Input
5
1 2 3 0 3
Output
2
Input
4
0 1 -1 0
Output
1
Input
2
4 1
Output
0

给你N个数,问是否能分成三份使得sum相等,可以输出方法数:

思路:模拟,对1/3点和2/3点进行讨论,1/3点处,方法数可以增1,在2/3点处,可以累加上当前的方法数。

鑫神给出了另一种思路:统计1/3点出连续的0和2/3点处之后连续的零,但是貌似细节有一点点问题,方法目测也行,因为的确就是到了1/3点处和2/3点处,只有碰到连续0才能够使方法数增加。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
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<n;i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    if(sum%3)
    {
        cout<<0<<endl;
        return 0;
    }
    sum=sum/3;
    long long ans=0;
    long long cnt=0;
    long long t=0;
    for(int i=0;i<n-1;i++)
    {
        ans+=a[i];
        if(ans==2*sum)
        {
            cnt+=t;
        }
        if(ans==sum)
        {
            t++;
        }
    }
    cout<<cnt<<endl;
    return 0;
}

267C:

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:

[l1, r1], [l2, r2], ..., [lk, rk] (1 ≤ l1 ≤ r1 < l2 ≤ r2 < ... < lk ≤ rk ≤ nri - li + 1 = m), 

in such a way that the value of sum is maximal possible. Help George to cope with the task.

Input

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).

Output

Print an integer in a single line — the maximum possible value of sum.

Sample test(s)
Input
5 2 1
1 2 3 4 5
Output
9
Input
7 1 3
2 10 7 18 5 33 0
Output
61
题意:给你n个数p1~pn,要你求出这个数列中k段长为m的区间的元素之和的最大值。

要求区间元素和,当然想求出数列的前缀和来了。


思路:二维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<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
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<<dp[n][k]<<endl;
   return 0;
}

268C:

A. 24 Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

Input

The first line contains a single integer n (1 ≤ n ≤ 105).

Output

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.

Sample test(s)
Input
1
Output
NO
Input
8
Output
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<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
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"<<endl;
    else
    {
        cout<<"YES"<<endl;
        slove(n);
    }
    return 0;
}

269C:

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<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    long long n;
    long long cnt=0;
    cin>>n;
    for(long long i=1;i<n;i++)
    {
        if((n+i)%3!=0)continue;
         if((n+i)/3<(i*(i+1)/2))break;
            cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}
270C:

C. Design Tutorial: Make It Nondeterministic
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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: .

Input

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).

Output

If it is possible, output "YES", otherwise output "NO".

Sample test(s)
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
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
Output
NO
Input
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
Output
YES
Note

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.

题目的意思是每一个人 都有frist name 和 last name。 从每一个人的名字中任意选择first name 或者 last name 作为这个人的编号,通过对编号的排序,得到每一个人的最终顺序,比较中的序列能否得到给定输出的序列一致。字符串处理题。

思路:首先因为要是一个递增的顺序,所以考虑该人的名和姓时,要让字符串小的放在前面作为序号,这样可以使得更多的人可能能继续添加,然后将当前的字符串设为此人的较小字符串,然后新串来时,看是否能够更新,如果能则能继续放置,否则必然是不符合条件了,全部符合出来后YES即可,否则里面就直接NOreturn 0

用string可以直接比大小好方便诶ORZZZZZ。

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
string a[100005],b[100005];
string c="";
int n;
int p[100005];
int main()
{
    cin>>n;

    for(int i=0;i<n;i++)
    {
         cin>>a[i]>>b[i];

    }
     for(int i=0;i<n;i++)
    {
         cin>>p[i];
        p[i]--;
    }
    int t;
    for(int i=0;i<n;i++)
    {
        t=p[i];
        if(a[t]>b[t])
        {
            swap(a[t],b[t]);
        }
        if(c<a[t])
        {
            c=a[t];
        }
        else if(c<b[t])
        {
            c=b[t];
        }
        else
        {
            cout<<"NO"<<endl;
            return 0;
        }
    }
    cout<<"YES"<<endl;
    return 0;
}

题目都很水,权当锻炼思维和练手速了,坑开不动就开发下思维吧,别生锈了ORZZZZ,听说几个基友都把今年的最后一个赛区当做旅游赛区啥的,一点压力都没有,尼玛为什么我还是忧愁万分呢T T

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值