Codeforces Round #613 (Div. 2)A-C

在这里插入图片描述

A - Mezo Playing Zoma

题目

Today, Mezo is playing a game. Zoma, a character in that game, is initially at position x=0. Mezo starts sending n commands to Zoma. There are two possible commands:

‘L’ (Left) sets the position x:=x−1;
‘R’ (Right) sets the position x:=x+1.
Unfortunately, Mezo’s controller malfunctions sometimes. Some commands are sent successfully and some are ignored. If the command is ignored then the position x doesn’t change and Mezo simply proceeds to the next command.

For example, if Mezo sends commands “LRLR”, then here are some possible outcomes (underlined commands are sent successfully):

“LRLR” — Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position 0;
“LRLR” — Zoma recieves no commands, doesn’t move at all and ends up at position 0 as well;
“LRLR” — Zoma moves to the left, then to the left again and ends up in position −2.
Mezo doesn’t know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at.

Input
The first line contains n (1≤n≤105) — the number of commands Mezo sends.

The second line contains a string s of n commands, each either ‘L’ (Left) or ‘R’ (Right).

Output
Print one integer — the number of different positions Zoma may end up at.

Example
Input
4
LRLR
Output
5
Note
In the example, Zoma may end up anywhere between −2 and 2.

题意

给你一个长度为n的字符串,L代表往左走,R代表往右走。可以选择忽略其中的任意个数的LR ,问最多可以到达多少坐标。

思路

既然可以忽略任意,那就先把R全忽略了记录L最多多少,然后L忽略了记录R最多多少,最后加个1就是最终答案。

代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,l=0,r=0;
    cin>>n;
    string s;
    cin>>s;
    int len = s.size();
    for(int i=0;i<len;i++)
    if(s[i]=='L')
    l++;
    else if(s[i]=='R')
    r++;
    cout<<r+l+1;
}

B - Just Eat It!

题目

Today, Yasser and Adel are at the shop buying cupcakes. There are n cupcake types, arranged from 1 to n on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type i is an integer ai. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.

Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.

On the other hand, Adel will choose some segment [l,r] (1≤l≤r≤n) that does not include all of cupcakes (he can’t choose [l,r]=[1,n]) and buy exactly one cupcake of each of types l,l+1,…,r.

After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice.

For example, let the tastinesses of the cupcakes be [7,4,−1]. Yasser will buy all of them, the total tastiness will be 7+4−1=10. Adel can choose segments [7],[4],[−1],[7,4] or [4,−1], their total tastinesses are 7,4,−1,11 and 3, respectively. Adel can choose segment with tastiness 11, and as 10 is not strictly greater than 11, Yasser won’t be happy 😦

Find out if Yasser will be happy after visiting the shop.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains n (2≤n≤105).

The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109), where ai represents the tastiness of the i-th type of cupcake.

It is guaranteed that the sum of n over all test cases doesn’t exceed 105.

Output
For each test case, print “YES”, if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice. Otherwise, print “NO”.

Example
Input
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5
Output
YES
NO
NO
Note
In the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.

In the second example, Adel will choose the segment [1,2] with total tastiness 11, which is not less than the total tastiness of all cupcakes, which is 10.

In the third example, Adel can choose the segment [3,3] with total tastiness of 5. Note that Yasser’s cupcakes’ total tastiness is also 5, so in that case, the total tastiness of Yasser’s cupcakes isn’t strictly greater than the total tastiness of Adel’s cupcakes.

题意

有两个人 Yasser 和 Adel
给你n个数,Yasser 的权值是把他们全加起来,Adel 的权值是从n个数里面任选,但是不能选1-n这个区间。
如果Adel 的最大值大于等于Yasser 的(全加一起)就是NO,否则是YES(Yasser 高兴)

思路

既然不能选1-n 那就从2-n和1-n-1里面选,这样就避免了选1-n的情况(刚开始我选了1-n然后想办法去掉这种情况结果越写越复杂 比赛就wa了3发 然后想明白改了一下又wa了几发才过的,tcl)
对于1-n-1 和2-n跑最大连续子区间和 转移方程就是 dp1[i]=max(dp1[i-1]+a[i],a[i]); 如果选上下一个更大的话就选上,如果选上下一个还没有直接取下一个大的话就取下一个eg:第一个是-1 第二个是2,如果选上的话是1,如果不选直接取2的话是2 那么果断取2

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100000+100;
ll a[maxn],dp1[maxn],dp2[maxn];

int main()
{
    ios::sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        ll sum=0,ans=0,temp=0;
        for(int i=1;i<=n;i++)
        cin>>a[i],sum+=a[i];
        int flag=0;
        for(int i=1;i<n;i++)
	    {
            // dp1[i]=max(dp1[i-1]+a[i],a[i]);
            temp=max(temp+a[i],a[i]);
            if(temp>=sum)
            flag=1;
	    }
        temp=0;
        for(int i=n;i>1;i--)
	    {
            // dp2[i]=max(dp2[i+1]+a[i],a[i]);
            temp=max(temp+a[i],a[i]);
            if(temp>=sum)
            flag=1;
	    }
        if(flag)
        cout<<"NO\n";
        else
        cout<<"YES\n";
    }
}

C - Fadi and LCM

Today, Osama gave Fadi an integer X, and Fadi was wondering about the minimum possible value of max(a,b) such that LCM(a,b) equals X. Both a and b should be positive integers.

LCM(a,b) is the smallest positive integer that is divisible by both a and b. For example, LCM(6,8)=24, LCM(4,12)=12, LCM(2,3)=6.

Of course, Fadi immediately knew the answer. Can you be just like Fadi and find any such pair?

Input
The first and only line contains an integer X (1≤X≤1012).

Output
Print two positive integers, a and b, such that the value of max(a,b) is minimum possible and LCM(a,b) equals X. If there are several possible such pairs, you can print any.

Examples
Input
2
Output
1 2
Input
6
Output
2 3
Input
4
Output
1 4
Input
1
Output
1 1

题意

找x的两个因子a,b 使 m a x ( a , b ) max(a,b) max(a,b)最小

思路

从2到sqrt(x)找因子就可以包含所有的情况(过了sqrt(x)和前面的对称)。
找因子,假设因子是a 那么 x/a 一定也是x的因子,如果a和x/a互质(gcd(a,x/a)=1) 那么就更新最小值
这里互质的原因是如果这两个数不互质的话求lcm就不等于x了 而是等于 x / g c d ( a , x / a ) x/gcd(a,x/a) x/gcd(a,x/a)
举个例子 24 : 2 ∗ 12 = 24 但 是 l c m ( 2 , 12 ) = 12 而 3 ∗ 8 = 24 24 :2*12 = 24 但是 lcm(2,12)=12 而 3*8 = 24 24:212=24lcm(2,12)=1238=24 由于他俩互质,所以lcm(3,8)就等于24 = x

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n;
    cin>>n;
    ll m = sqrt(n);
    ll minn = n;
    for(ll i=2;i<=m;i++)
    {
        if(n%i)
        continue;
        if(__gcd(i,n/i)==1)
        minn = min(minn,n/i);
    }
    cout<<n/minn<<" "<<minn<<"\n";
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值