Codeforces Round #378 (Div. 2)(A+B)

本文探讨了两道编程题:一是计算蚂蚱跳跃到字符串末尾所需的最小跳跃能力;二是通过调整军队游行中某列士兵的步伐来最大化队伍整齐度。文中提供了详细的题目描述及解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Grasshopper And the String

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

One day, the Grasshopper was jumping on the lawn and found a piece of paper with a string. Grasshopper became interested what is the minimum jump ability he should have in order to be able to reach the far end of the string, jumping only on vowels of the English alphabet. Jump ability is the maximum possible length of his jump.

Formally, consider that at the begginning the Grasshopper is located directly in front of the leftmost character of the string. His goal is to reach the position right after the rightmost character of the string. In one jump the Grasshopper could jump to the right any distance from 1 to the value of his jump ability.
例一
The picture corresponds to the first example.
The following letters are vowels: ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ and ‘Y’.

Input

The first line contains non-empty string consisting of capital English letters. It is guaranteed that the length of the string does not exceed 100.

Output

Print single integer a — the minimum jump ability of the Grasshopper (in the number of symbols) that is needed to overcome the given string, jumping only on vowels.

Examples

Input
ABABBBACFEYUKOTT

Output
4

Input
AAA

Output
1
题意:蚂蚱(刘大神)只能每次跳只能落在(A,E,I,O,U,Y)上,问最小的跳远能力可以使他到达终点。
题解:模拟题,姿势要优美
代码:

#include <bits/stdc++.h>
using namespace std;
char a[150];
int solo(char a)
{
    if(a=='A'||a=='E'||a=='I'||a=='U'||a=='O'||a=='Y')
        return 1;
    return 0;
}
int main()
{
    scanf("%s",a);
    int ans=0;
    int tmp=-1;
    int len=strlen(a);
    for(int i=0;i<len;i++)
    {
        if(solo(a[i]))
        {
            ans=max(ans,i-tmp);
            tmp=i;
        }
    }
    ans=max(ans,len-tmp);
    printf("%d\n",ans);
}

B. Parade

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.

There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.

The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.

No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.

Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.

Input

The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns.

The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively.

Output

Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.

Consider that columns are numbered from 1 to n in the order they are given in the input data.

If there are several answers, print any of them.

Examples

Input
3
5 6
8 9
10 3

Output
3

Input
2
6 5
5 6

Output
1

Input
6
5 9
1 3
4 8
4 5
23 54
12 32

Output
0

Note

In the first example if you don’t give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.

If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.

It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9。
题意:给你两个长度为n的序列l,r。
问交换序列对使得abs(sum(l)-sum(r))最大。
输出序列对的下标。
题解:模拟题。
代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int l[N],r[N];
int main()
{
    int n,L,R;
    L=R=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&l[i],&r[i]);
        L+=l[i];
        R+=r[i];
    }
    int ans=abs(L-R);
    int tmp=0;
    for(int i=1;i<=n;i++)
    {
        L=L-l[i]+r[i];
        R=R-r[i]+l[i];
        if(abs(L-R)>ans)
        {
            ans=abs(L-R);
            tmp=i;
        }
        L=L+l[i]-r[i];
        R=R+r[i]-l[i];
    }
    cout<<tmp<<endl;
}
### 关于 Codeforces Round 997 Div. 2 的题目及解析 #### A. XOR Mixup 在这个问题中,给定了两个整数 \(a\) 和 \(b\) ,以及一个正整数 \(k\) 。目标是在不超过 \(k\) 步内通过交换 \(a\) 和 \(b\) 中任意一位来使得两者相等。如果可以在指定步数内完成,则返回 "YES";否则返回 "NO"[^1]。 对于这个问题的一个有效解决方案是计算不同位的数量并判断其是否小于等于两倍的 k 值加上 a 和 b 的二进制表示中最右边不同的位置索引之差。这是因为每一步最多能改变一对不匹配的位置状态。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long a, b, k; cin >> a >> b >> k; bitset<32> ba(a), bb(b); int diff = 0; for(int i=0; i<32; ++i){ if(ba[i]!=bb[i])++diff; } cout << ((abs(__builtin_ctzll(a ^ b)) + 2 * k >= diff) ? "YES\n":"NO\n"); } } ``` #### B. Array Shrinking 此题描述了一个数组缩小的过程:允许选取连续子数组并将它们替换为其最大公约数值(GCD),直到整个数组变成单个元素为止。询问最终剩余的那个唯一数字是什么样的最小可能值[^2]? 解决方法涉及到动态规划的思想——维护一个二维表 dp[][],其中dp\[l\]\[r\] 表达的是区间 \([l,r]\) 能够被压缩成的最大 GCD 数字。转移方程基于枚举中间点 m 来分割原区间为更小子区间的组合方式实现更新。 ```cpp const int N = 2e5+7; long long gcd(long long a,long long b){return !b?a:gcd(b,a%b);} vector<int> v(N); unordered_map<long long,int> mp[N]; void solve(){ int n; scanf("%d",&n); for(int i=1;i<=n;++i)v[i]=rand()%N+1; memset(mp,0,sizeof(mp)); for(int len=1;len<=n;++len) for(int l=1;l+len-1<=n;++l){ int r=l+len-1; if(len==1)mp[l][v[l]]=1; else{ unordered_set<long long> st; for(auto &p : mp[l]) if(p.second>=len-1&&gcd(v[r],p.first)==v[r]){ printf("0");exit(0); }else{st.insert(gcd(v[r],p.first));} for(auto x:st)mp[l][x]++; } } puts(to_string(mp[1].begin()->first).c_str()); } signed main(){solve();} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值