专题4:模拟

1、倒推模拟

问题 B: Ice Rink Game

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

An adult game master and N children are playing a game on an ice rink. The game consists of K rounds. In the i-th round, the game master announces:
Form groups consisting of Ai children each!
Then the children who are still in the game form as many groups of Ai children as possible. One child may belong to at most one group. Those who are left without a group leave the game. The others proceed to the next round. Note that it's possible that nobody leaves the game in some round.
In the end, after the K-th round, there are exactly two children left, and they are declared the winners.
You have heard the values of A1, A2, ..., AK. You don't know N, but you want to estimate it.
Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.
Constraints
1≤K≤105
2≤Ai≤109
All input values are integers.

输入

Input is given from Standard Input in the following format:

K
A1 A2 … AK

输出

Print two integers representing the smallest and the largest possible value of N, respectively, or a single integer −1 if the described situation is impossible.

样例输入 Copy

4
3 4 3 2

样例输出 Copy

6 8

提示

For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.
In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.
In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.
In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.
The last 2 children are declared the winners.

 

思路

One child may belong to at most one group这句话很重要

题意:  每次保证总人数>=a[i] ,并且一人只能分在一组,(不然找最大值就行了),每次每组人数一样,组数不一定相同;

突破口:当下一次比这次小,其实很简单,每组去掉一定人数即可,进行下一关的人减少了,但是总用过的人,并没有影响,所以我们只需要考虑与之相反的情况。

其实求的是用过的人数,并非,此时总人数。

例子:

目前用过的人数为maxnsum=11,minsum=9    此时a[i]=4 ,a[i+1];

maxnsum= (num/4+1)*a[i] -1 ;

而此时,没有必要更新minsum

当出现minsum%a[i]==0时

minnsum=(minnsum/a[i]+1)*a[i];


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e5+5;
ll a[mod];
int main()
{
    ll n,minn=2,maxn=2;
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    for(int i=n;i>=1;i--)
    {
        if(minn%a[i]!=0) minn=(minn/a[i]+1)*a[i];
        maxn=(maxn/a[i]+1)*a[i]-1;
    }
    if(maxn<minn) printf("-1");
     else
    printf("%lld %lld",minn,maxn);
    return 0;
}
 
 
/**************************************************************
    Problem: 6419
    User: 2019UPC110
    Language: C++
    Result: 正确
    Time:28 ms
    Memory:2804 kb
****************************************************************/

 

问题2:

问题 A: Move and Win

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

A game is played on a strip consisting of N cells consecutively numbered from 1 to N.
Alice has her token on cell A. Borys has his token on a different cell B.
Players take turns, Alice moves first. The moving player must shift his or her token from its current cell X to the neighboring cell on the left, cell X−1, or on the right, cell X+1. Note that it's disallowed to move the token outside the strip or to the cell with the other player's token. In one turn, the token of the moving player must be shifted exactly once.
The player who can't make a move loses, and the other player wins.
Both players want to win. Who wins if they play optimally?

Constraints
2≤N≤100
1≤A<B≤N
All input values are integers.

输入

Input is given from Standard Input in the following format:

N A B

输出

Print Alice if Alice wins, Borys if Borys wins, and Draw if nobody wins.

样例输入 Copy

5 2 4

样例输出 Copy

Alice

提示

Alice can move her token to cell 3. After that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5. Then, Alice moves her token to cell 4. Borys can't make a move and loses.

 思路:

一共三种走法:

① 相向行:距离-2;

②反向行:距离+2;

③同向行:距离不变;

所以距离奇偶性不变,所以任意考虑其中一种情况即可。

算是一种模拟吧。。。

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int n,a,b;
   scanf("%d%d%d",&n,&a,&b);
   int gap=b-a-1;
   if(gap%2==0)
    printf("Borys");
   else
    printf("Alice");
    return 0;
}
 
 
/**************************************************************
    Problem: 6418
    User: 2019UPC110
    Language: C++
    Result: 正确
    Time:1 ms
    Memory:2024 kb
****************************************************************/

问题3: 

 

请小伙伴们对自己AC的题目进行标记,注意每人只能标记一次!不知道的不要标记,恶意标记者将回收账号!!!

问题 C: Matrix Transformation

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

You have an integer matrix A, with R rows and C columns.  That means it has R rows with each row containing C integers.  Two integers are adjacent if their container cells share an edge.  For example, in the following grid  

 20200228152959_44488.jpguploading.4e448015.gif转存失败重新上传取消

 (0, 1), (4, 5), (1, 4), (5, 2) are adjacent but (0, 4), (2, 6), (5, 7) are not adjacent. 
 
You are allowed to do only one kind of operation in the matrix.  In each step you will select two adjacent  cells  and  increase  or  decrease  those  two  adjacent  values  by  1,  i.e.,  both  values  are  increased by 1 or both values are decreased by 1. 
Given a matrix, determine whether it is possible to transform it to a zero matrix by applying the allowed operations.  A zero matrix is the one where each of its entries is zero.  
 

输入

The first input line contains a positive integer, n, indicating the number of matrices.  Each matrix starts with a line containing R (2 ≤ R ≤ 30) and C (2 ≤ C ≤ 30) separated by a single space.  Each  of the next R lines contains C integers.  Each of these integers is between -20 and +20 inclusive.  
Assume that each input matrix will have at least one non-zero value.  

输出

For each matrix (test case), output a single integer on a line by itself indicating whether or not it can be transformed to a zero matrix.  Output the integer 0 (zero) if the matrix can be transformed to a zero matrix and 1 (one) if it cannot. 

样例输入 Copy

6 
3 3 
-2 2 2 
1 1 0 
2 -2 -2 
3 3 
-1 0 1 
-2 -1 1 
0 1 2 
3 3 
-1 0 1 
0 2 -1 
-1 1 2 
3 3 
-1 2 1 
-1 -1 -3 
1 1 -1 
2 3 
0 -2 3 
1 3 1 
2 3 
3 1 1 
2 0 1 

思路 :

看到这道题以为要用搜索一个一个,实际上就是一个模拟,

注意题上的一句话:意思是只对相邻的进行同时+/-1

You are allowed to do only one kind of operation in the matrix.  In each step you will select two adjacent  cells  and  increase  or  decrease  those  two  adjacent  values  by  1,  i.e.,  both  values  are  increased by 1 or both values are decreased by 1. 

这个时候, 你就应该明白了,不就是消消乐嘛,把 上、下、左、右 四个方位变成   上和右,就行了呀意思就是把每个方格的数往右下角排;对于一个3*3中的2*2的方格,不论采取那种手段,其实都是将那个2*2的左上角那个方格变为0。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=2e4+5;
int A[35][35];
int main()
{
   int n;
   scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {
       int row,column;
       scanf("%d%d",&row,&column);
       for(int j=1;j<=row;j++)
       {
           for(int k=1;k<=column;k++)
           {
               scanf("%d",&A[j][k]);
           }
       }
       for(int j=1;j<=row;j++)
       {
           for(int k=1;k<=column;k++)
           {
               if(k<column)
               {if(j==1||A[j-1][k]==0)
               {

                   int temp=0-A[j][k];
                   A[j][k]=0;
                   A[j][k+1]+=temp;
               }}
               else
               {
                   int temp=0-A[j-1][k];
                   A[j-1][k]=0;
                   A[j][k]+=temp;
               }
           }
       }
       int f=1;
       for(int j=1;j<=row;j++)
       {
           for(int k=1;k<=column;k++)
           {
               if(A[j][k]!=0)
               {
                   f=0;
                   A[j][k]=0;
               }
           }
       }
       if(f==0)
        printf("1");
       else
        printf("0");
       if(i<n) printf("\n");

   }

   return 0;
}

 

题目:

问题 L: Guess The Number

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.
·The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)
·The si-th digit from the left is ci. (i=1,2,⋯,M)
Constraints
·All values in input are integers.
·1≤N≤3
·0≤M≤5
·1≤si≤N
·0≤ci≤9

输入

Input is given from Standard Input in the following format:

N M
s1 c1

sM cM

 

输出

Print the answer.

样例输入 Copy

【样例1】
3 3
1 7
3 2
1 7
【样例2】
3 2
2 1
2 3
【样例3】
3 1
1 0

样例输出 Copy

【样例1】
702
【样例2】
-1
【样例3】
-1

提示

样例1解释
702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

几组很坑的样例

【1】

1 0

==>0

【2】

2 0

==>10

【3】

3 0

==>100

【4】

2 1

3 0

==>-1

【5】

2 1

1 0

==》-1

代码:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int,int> pp;
const int N=1e5+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double pi=acos(-1);
int A[15];
bool cmp(int x,int y)
{
    return x>y;
}
int main()
{
  int n,m;
  scanf("%d %d",&n,&m);
  int f=1;
  for(int i=1;i<=n;i++)
    A[i]=-1;
  for(int i=1;i<=m;i++)
  {
      int a,b;
      scanf("%d %d",&a,&b);
      if(A[a]==-1)
        A[a]=b;
      else
      {
          if(A[a]!=b) f=0;
      }
      if(a>n) f=0;
  }
  if(n==1&&m==0)
  {
      printf("0");
      return 0;
  }
  int num=0;
  if(A[1]==0&&n>1)
    f=0;
 if(f==1)
 {
     for(int i=1;i<=n;i++)
     {
         if(A[i]==-1)
         {
             if(i==1) A[i]=1;
             else
             A[i]=0;
         }
         num=num*10+A[i];
     }
 }
  if(f==1) printf("%d",num);
  else printf("-1");
    return 0;
}

问题:

问题 C: Jumping Frog

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

Freddy the frog is trying to go get a bite to eat.  The problem is Freddy lives far away from all the restaurants he likes.  Freddy is a capable frog, able to jump over a large number of cells. 
Unfortunately, on a given day, Freddy may be too hungry to jump very far.  On a given day, he can only jump over at most d cells (note that he can also jump over fewer cells). 

Another complication for Freddy is the path he jumps across is always under construction.  Some of  the  cells  are  blocked  off!    Freddy  doesn’t  want  to  land  in  a  cell  under  construction  but  is allowed to jump over them. 

Freddy starts off in the first cell and must travel to the last cell where his destination restaurant resides.  He would like to know if he can reach the last cell and how quickly he can reach it. Freddy always jumps towards his destination. 
 
Given a description of cells, determine the minimum number of jumps Freddy can make to reach the restaurant. 

输入

The first input line contains a positive integer, n, indicating the number of days to check.  Each day is represented by two lines.  The first line of each day contains two integers, c (2 ≤ c ≤ 50) and d (0 ≤ d ≤ 50), representing (respectively) the number of cells in the path from Freddy’s home to the restaurant (including his home and the restaurant) and the maximum number of cells Freddy can jump over in a single jump on that day.  The second line is a string consisting of c characters containing only ‘.’ and ‘X’ characters where ‘.’ represents that the cell is okay for Freddy to occupy and ‘X’ represents that the cell is blocked by construction.  The first and last characters  of  the  string  represent  Freddy’s  home  and  the  restaurant,  respectively;  these  two locations will never be blocked. 

输出

For each day, first output the heading “Day #d”, where d is the day number, starting with 1.  Then,  print  the  input  values  exactly  as  they  appear  in  the  input.    Following  the  header  info, output the minimum number of jumps it takes Freddy to reach the restaurant.  If it is not possible to reach the restaurant on a given day, print the number 0 instead. 
 
Leave a blank line after the output for each data set.  Follow the format illustrated in Sample Output. 

样例输入 Copy

4
8 3
.XX.X.X.
3 50
...
8 1
...XX...
10 4
..XXXX.XX.

样例输出 Copy

Day #1
8 3
.XX.X.X.
2

Day #2
3 50
...
1

Day #3
8 1
...XX...
0

Day #4
10 4
..XXXX.XX.
3

 

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int,int> pp;
const int N=2e5+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double pi=acos(-1);
int A[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        A[N]=0;
        int a,b;
        scanf("%d%d",&a,&b);
        char str[105];
        scanf("%s",str);
        int len=strlen(str),p=0;
         printf("Day #%d\n",i);
        printf("%d %d\n",a,b);
        printf("%s\n",str);
        for(int j=0; j<len; )
        {
            if(str[j]=='.')
            {
                p=j;
                if(j-b-1<=0)
                    A[j]=1;
                else if(j-b-1>0)
                {
                    A[j]=A[j-1]+1;
                     for(int k=j;k>=j-b-1;k--)
                      if(str[k]=='.') A[j]=min(A[k]+1,A[j]);
                }
                j++;
            }
            int cnt=0;
            while(str[j]=='X')
            {
                A[j]=A[p];
                j++;
                cnt++;
            }

            if(cnt>b)
            {
               A[len-1]=0;break;
            }
        }

        if(A[len-1]==0)  //*************
            printf("0\n");
        else
            printf("%d\n",A[len-1]);
        puts("");
    }

    return 0;
}

 

题目:

问题 L: Christmas

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

In some other world, today is Christmas.
Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L  burger (L is an integer greater than or equal to 0) is the following thing:
·A level-0 burger is a patty.
·A level-L burger (L≥1) is a bun, a level-(L−1) burger, a patty, another level-(L−1) burger and another bun, stacked vertically in this order from the bottom.
For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.
The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

Constraints
1≤N≤50
1≤X≤( the total number of layers in a level-N burger )
N and X are integers.

输入

Input is given from Standard Input in the following format:

N X

输出

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.
 

样例输入 Copy

2 7

样例输出 Copy

4

 

感谢滕哥哥的帮助~ 

//p的个数F[X]=F[X-1]*2+1

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int,int> pp;
ll len[60],p[60];
int main()
{
    ll n,m;
    scanf("%lld %lld",&n,&m);
    len[1]=5;
    p[1]=3;
    p[0]=3;
    for(int i=2; i<=n; i++)
    {
        p[i]=p[i-1]*2+1;
    }
    for(int i=2; i<=n; i++)
    {
        len[i]=len[i-1]*2+3;
    }
    ll ans=0;
    while(m)
    {
        if(n==1)
        {
            if(m>=5)
                {
                    ans+=3;
                }
            else
            {
                ans+=m-1;
            }
            break;
        }
        if(m>=(len[n]-1)/2+1)
        {
            m-=(len[n]-1)/2+1;
            ans+=p[n-1]+1;
            n--;
        }
        else
        {
            m--;
            n--;
        }
    }
     printf("%lld",ans);
    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值