Codeforces Round #271 (Div. 2)

本文探讨了在特殊键盘布局下,通过字符错位输入的问题解决方法。介绍了一个盲人角色如何克服键盘操作困难,实现准确的信息输入,并提供了解决方案的算法实现。

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

A. Keyboard

time limit per test   2 seconds

memory limit per test   256 megabytes

Our good friend Mole is trying to code a big message. He is typing on an unusual keyboard with characters arranged in following way:

qwertyuiop
asdfghjkl;
zxcvbnm,./

Unfortunately Mole is blind, so sometimes it is problem for him to put his hands accurately. He accidentally moved both his hands with one position to the left or to the right. That means that now he presses not a button he wants, but one neighboring button (left or right, as specified in input).

We have a sequence of characters he has typed and we want to find the original message.

Input

First line of the input contains one letter describing direction of shifting ('L' or 'R' respectively for left or right).

Second line contains a sequence of characters written by Mole. The size of this sequence will be no more than 100. Sequence contains only symbols that appear on Mole's keyboard. It doesn't contain spaces as there is no space on Mole's keyboard.

It is guaranteed that even though Mole hands are moved, he is still pressing buttons on keyboard and not hitting outside it.

Output

Print a line that contains the original message.

Sample test(s)
Input
R
s;;upimrrfod;pbr
Output
allyouneedislove


AC代码:

<span style="font-size:12px;">#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

char *s = "qwertyuiopasdfghjkl;zxcvbnm,./";
char t[1100];

int main()
{
    char ch;
    cin>>ch>>t;
    for(int i = 0; t[i]; i++)
    {
        int j;
        for(j = 0; s[j] && s[j] != t[i]; j++) ;
        if(ch == 'R') putchar(s[j - 1]);
        else putchar(s[j + 1]);
    }
    return 0;
}</span>

B. Worms

time limit per test   1 second

memory limit per test  256 megabytes

It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.

Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.

Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.

Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.

Input

The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile.

The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot.

The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms.

Output

Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.

Sample test(s)
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
Note

For the sample input:

  • The worms with labels from [1, 2] are in the first pile.
  • The worms with labels from [3, 9] are in the second pile.
  • The worms with labels from [10, 12] are in the third pile.
  • The worms with labels from [13, 16] are in the fourth pile.
  • The worms with labels from [17, 25] are in the fifth pile.

AC代码:

<span style="font-size:12px;">#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

/*
5
2 7 3 4 9
3
1 25 11
*/
const int M = 1e6 + 100;
int a[M];

int main()
{
    int n,m,x,i,cnt = 1,vis = 1;
    cin>>n;
    while(n--)
    {
        scanf("%d",&x);
        for(i = vis; i < vis + x; i++)
            a[i] = cnt;
        cnt++;
        vis = i;
    }
    //for(int i = 1; i <= 25; i++) cout<<a[i]<<endl;
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d",&x);
        printf("%d\n",a[x]);
    }
    return 0;
}</span>

C. Captain Marmot

time limit per test   1 second

memory limit per test   256 megabytes

Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.

Initially, each mole i (1 ≤ i ≤ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible.

Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi).

A regiment is compact only if the position points of the 4 moles form a square with non-zero area.

Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.

Input

The first line contains one integer n (1 ≤ n ≤ 100), the number of regiments.

The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≤ xi, yi, ai, bi ≤ 104).

Output

Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes).

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

In the first regiment we can move once the second or the third mole.

We can't make the second regiment compact.

In the third regiment, from the last 3 moles we can move once one and twice another one.

In the fourth regiment, we can move twice the first mole and once the third mole.


AC代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

#define cosa  0
#define sina  1

using namespace std;
typedef long long ll;
int n;
struct Node
{
    int x,y,a,b;
    void read()
    {
        scanf("%d %d %d %d",&x,&y,&a,&b);
    }
} p[10];

struct New
{
    ll x1,y1;
} ne[10];

void rot(Node a,int i, int x)
{
    if(!i)
    {
        ne[x].x1 = a.x;
        ne[x].y1 = a.y;
    }
    while(i)
    {
        ne[x].x1 = (a.x - a.a) * cosa - (a.y - a.b) * sina + a.a; //点的旋转,刚开始在这里出了问题,傻逼了半天
        ne[x].y1 = (a.x - a.a) * sina + (a.y - a.b) * cosa + a.b;
        a.x = ne[x].x1;
        a.y = ne[x].y1;
        i--;
    }
}

ll dist(New a, New b)
{
    return (a.x1 - b.x1) * (a.x1 - b.x1) + (a.y1 - b.y1) * (a.y1 - b.y1);
}

bool check(New *a)
{
    ll h[10],to,H,he[3];
    to = H = he[0] = he[1] = he[2] = 0;
    for(int i = 0; i < 3; i++)
        for(int j = i + 1; j < 4; j++)
            h[to++] = dist(a[i],a[j]);
    sort(h, h + to);
    for(int i = 1; i < to; i++)
    {
        if(h[i] == h[i - 1]) he[H]++;
        else H++;
    }
    return he[0] == 3 && he[1] == 1;
}

int main()
{

    //freopen("in","r",stdin);
    scanf("%d",&n);
    for(int pos = 0; pos < n; pos++)
    {
        int Min = 1234567;
        for(int i = 0; i < 4; i++)
            p[i].read();
        for(int i = 0; i < 4; i++)
        {
            for(int j = 0; j < 4; j++)
            {
                for(int k = 0; k < 4; k++)
                {
                    for(int o = 0; o < 4; o++)
                    {

                        rot(p[0],i,0);
                        rot(p[1],j,1);
                        rot(p[2],k,2);
                        rot(p[3],o,3);

                        if(check(ne))
                            Min = min(Min,i + j + k + o);
                    }
                }
            }
        }
        printf("%d\n",Min == 1234567 ? -1 : Min);
    }
    return 0;
}

D. Flowers

time limit per test  1.5 seconds

memory limit per test  256 megabytes

We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.

But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.

Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).

Input

Input contains several test cases.

The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases.

The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the i-th test.

Output

Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (109 + 7).

Sample test(s)
Input
3 2
1 3
2 3
4 4
Output
6
5
5
Note
  • For K = 2 and length 1 Marmot can eat (R).
  • For K = 2 and length 2 Marmot can eat (RR) and (WW).
  • For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR).
  • For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR).

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

typedef long long ll;
const int M = 1e5 + 100;
const int mod = 1e9 + 7;
ll dp[M],sum[M];

struct Node
{
    int a,b;
}p[M];

int main()
{
    int n,k,Max;
    scanf("%d %d",&n,&k);
    for(int i = 0; i < n; i++)
    {
        scanf("%d %d",&p[i].a,&p[i].b);
        Max = max(p[i].b,Max);
    }
    for(int i = 0; i <= Max; i++)
    {
        if(i < k) dp[i] = 1;
        else dp[i] = dp[i - 1] + dp[i - k];
        if(dp[i] > mod) dp[i] %= mod;
    }
    for(int i = 1; i <= Max; i++)
        sum[i] = sum[i - 1] + dp[i];
    for(int i = 0; i < n; i++)
    {
        int ua = p[i].a;
        int ub = p[i].b;
        printf("%I64d\n",(sum[ub] - sum[ua - 1]) % mod);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值