首页 > 其他分享 >Codeforces Round 933 (Div. 3) D 题

Codeforces Round 933 (Div. 3) D 题

时间:2024-07-30 18:27:44浏览次数:11  
标签:ball Codeforces number player counterclockwise 933 Div clockwise throws

D. Rudolf and the Ball Game

原题链接:

https://codeforces.com/contest/1941/problem/D 

Rudolf and Bernard decided to play a game with their friends. n people stand in a circle and start throwing a ball to each other. They are numbered from 1 to nn in the clockwise order.

Let's call a transition a movement of the ball from one player to his neighbor. The transition can be made clockwise or counterclockwise.

Let's call the clockwise (counterclockwise) distance from player y1 to player y2 the number of transitions clockwise (counterclockwise) that need to be made to move from player y1 to player y2. For example, if n=7 then the clockwise distance from 2 to 5 is 3, and the counterclockwise distance from 2 to 5 is 4.

Initially, the ball is with the player number x (players are numbered clockwise). On the ii-th move the person with the ball throws it at a distance of riri (1≤ri≤n−1) clockwise or counterclockwise. For example, if there are 7 players, and the 2nd player, after receiving the ball, throws it a distance of 55, then the ball will be caught by either the 7th player (throwing clockwise) or the 4th player (throwing counterclockwise). An illustration of this example is shown below.

 

 

 

The game was interrupted after mm throws due to unexpected rain. When the rain stopped, the guys gathered again to continue. However, no one could remember who had the ball. As it turned out, Bernard remembered the distances for each of the throws and the direction for some of the throws (clockwise or counterclockwise).

Rudolf asks you to help him and based on the information from Bernard, calculate the numbers of the players who could have the ball after m throws.

Input

The first line of the input contains a single integer t (1≤t≤104) — the number of test cases. Then follow the descriptions of the test cases.

The first line of each test case contains three integers n,m,x (2≤n≤1000, 1≤m≤1000, 1≤x≤n) — the number of players, the number of throws made, and the number of the player who threw the ball first, respectively.

The next mm lines contain information about each throw in order. Each of them contains an integer riri (1≤ri≤n−1) — the distance at which the i-th throw was made, and a symbol ci, equal to '0', '1', or '?':

  • if ci='0', then the ii-th throw was made clockwise,
  • if ci='1', then the ii-th throw was made counterclockwise,
  • if ci='?', then Bernard does not remember the direction and the i-th throw could have been made either clockwise or counterclockwise.

It is guaranteed that the sum n⋅m (n multiplied by m) over all test cases does not exceed 2⋅105.

  

Output

For each test case, output two lines.

In the first line, output the number of players k (1≤k≤n) who could have the ball at the end of the game.

In the next line, output kk numbers bi (1≤bi≤n) — the numbers of the players in increasing order. All numbers must be different.

题意:

 n个人围着坐成了一圈,然后开始的时候球位于第x个人手中,然后要进行m次传递,求m次传递后可能拿球的人。传球有顺时针和逆时针两种传球方式,假设第j个人持有球,如果是顺时针,则为(j+r+n)%n ,  如果是逆时针,则为(j-r+n)%n。

解决方案: 

这道题我们使用动态规划来解决,怎么使用呢,一共不是m次投球嘛,我们让dp数组,代表经过m次投球后可能拿到求的人,如果dp[i]为1的话,此人就可能持有球。dp数组需要经过m次更新,每投一次求,就更新一次,即代表投了j次求后的可能持有球的人。
 

代码实现:

#include<bits/stdc++.h>

using i64=long long;

void solve()
{
    int n,m,x;
    std::cin>>n>>m>>x;
    std::vector<int> dp(n);
    x--;
    dp[x]=1;
    for(int i=0;i<m;i++)
    {
        int r;
        char c;
        std::cin>>r>>c;
        std::vector<int> g(n);
        for(int j=0;j<n;j++)
        {
            if(dp[j])
            {
                if(c!='1')
                {
                    g[(j+r)%n]=1;
                }
                if(c!='0')
                {
                    g[(j-r+n)%n]=1;
                }
            }
        }
        dp=g;
    }
    int ans=std::count(dp.begin(),dp.end(),1);//计算dp数组当中有多少个1
    std::cout<<ans<<"\n";
    for(int i=0;i<n;i++)
    {
        if(dp[i])
        {
            std::cout<<i+1<<" ";
        }
    }
    std::cout<<"\n";
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin>>t;

    while(t--)
    {
        solve();
    }

    return 0;
}


 

标签:ball,Codeforces,number,player,counterclockwise,933,Div,clockwise,throws
From: https://blog.csdn.net/2302_79545206/article/details/140803094

相关文章

  • Codeforces Round 929 (Div. 3)---->E. Turtle vs. Rabbit Race: Optimal Trainings
    https://codeforces.com/contest/1933/problem/E#include<bits/stdc++.h>#definexfirst#defineysecondusingnamespacestd;typedeflonglongll;typedef__int128i128;typedefpair<int,int>pii;constintN=2e5+10,M=110;intn,q;inta[N];ll......
  • Codeforces Round 958 (Div. 2)A(AC)BC(补题)
    这里写目录标题A思维题【AC】B贪心(+双指针)【补题】冗余代码(我的):大佬:双指针代码借鉴后代码C异或问题【补题】A思维题【AC】思路:每次分成k-1个1,1个其他#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;//constintN=2e5+10;v......
  • Pinely Round 4 (Div. 1 + Div. 2) A - E
    A.MaximizetheLastElement答案是奇数位的最大值点击查看代码#include<bits/stdc++.h>#defineFOR(i,j,k)for(inti=(j);i<=(k);i++)#defineROF(i,j,k)for(inti=(j);i>=(k);i--)#definePIIpair<int,int>#defineintlonglong#defineULLunsi......
  • 题解:Codeforces Round 962 (Div. 3) D
    D.Funtimelimitpertest:2secondsmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputCountingisFun!—satyam343Giventwointegers\(n\)and\(x\),findthenumberoftriplets(\(a,b,c\))ofpositiveintegerss......
  • Codeforces Round 918 (Div. 4) 解题
    A.OddOneOut题面翻译ttt组数据。每次给出三个数字a,b......
  • Codeforces Round 962(div 3) E Decode(解码)
    题目链接:https://codeforces.com/contest/1996/problem/E题目描述:为了获得你最喜欢的角色,你不惜一切代价,侵入了游戏的源代码。经过几天的努力,你终于找到了编码游戏gacha系统的二进制字符串。为了解码它,你必须首先解决以下问题。给你一个长度为n的二进制字符串s。对于每对整数......
  • Pinely Round 4 (Div. 1 + Div. 2) 赛后总结
    PinelyRound4(Div.1+Div.2)赛时提交情况:CF1991A.MaximizetheLastElement赛时思路首先,CF判断了足足2min确定我是真人,看到题目时首先想到的是,最后保留的数字之前及之后必然有偶数个数字,且\(n\)为奇数,所以我们可以确定若\(a_i\)是最后保留的数字,\(i\)必然为奇......
  • Pinely Round 4 (Div. 1 + Div. 2)
    Preface难得地有直觉的一场,50min过了前5题后形式大好,然后F也一眼看出了有个斐波那契的上界结果写了个暴力判断交上去一直挂,前面一直以为是一定有解的阈值设错了,没想到挂了好几发后发现暴力漏了一种Case,真是唐完了A.MaximizetheLastElement不难发现只有奇数位置上的......
  • Pinely Round 4 (Div. 1 + Div. 2)(A - F)
    PinelyRound4(Div.1+Div.2)(A-F)A-MaximizetheLastElement解题思路:只有奇数位置能选。偶数位置前后都有奇数个数字,无法删完。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#define......
  • Codeforces Round 961 (Div. 2) 复盘
    第一次打div2的总结div2难度明显比div3要难一些,其实也不是很难前面的签到题,但是给了我一种每道题都可以直接暴力但是就是会超时的感觉,不知道是不是提前就在告诉你要考虑greedythinking。T11995A-Diagonals这道题说实话就是存粹的模拟,除了最长的一个对角线同长度只有一列,其......