I. Mancala

time limit per test:3 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Mancala is a traditional board game played in Africa, Middle East and Asia. It is played by two players. This game board consists of two rows of holes, one row for each player. Each row has N holes, and each hole has some non-negative number of stones.

The two players will, in turn, make a move. One move is described as follows:

  • the player chooses one of the holes in his row and takes all the stones from it.
  • he starts to put these stones one in each hole, starting from the next hole and moving in counter-clockwise order, until there are no more stones left in his hands.

eg: given this board:

player1's row: 2 2 3

player2's row: 1 8 2

if player2 starts a move and chooses the middle hole in his row(the one with 8 stones). The board after the move will be like:

player1's row: 3 3 5

player2's row: 2 1 4

you were playing a very important game with your best friend, when suddenly you had a phone call and moved your eyes of the game. Now you lost track of the game and you need to make sure if your friend made a valid move.

You are given the final board configuration and the place where the last stone landed, Your task is to check is your friend's move is invalid, and in case of a valid move, find the state of the board before that move.

You are player1 while your friend is player2.

Input

The input consists of several test cases, each test case starts with three numbers n(1 ≤ n ≤ 10000) (the number of holes in each of the rows), r (1 ≤ r ≤ 2) and k (1 ≤ k ≤ n) (the row and hole number where the last stone was put, respectively). Then 2n numbers follow, ai :1  ≤  i  ≤  n, and bj :1  ≤  j  ≤  n, where ai is the number of stones in the i-th hole in your row. bj is the number of stones in the j-th hole in your friend's row. Initially given ai and bi satisfy that: (0 ≤ ai, bj ≤ 1000000000) while ai and bj are fit in 64 bits in the state of the board before the move (if there is a valid move).

The last test case is followed by three zeros.

Output

For each test case display the case number followed by the word "INVALID" without the quotes if the move is invalid, or 2n numbers representing the original board configuration otherwise.

Examples
Input
3 1 3
3 3 5
2 1 4
4 2 2
1 2 3 4
5 4 3 2
4 2 2
1 2 3 4
1 2 3 4
5 2 3
2 2 2 2 2
2 2 2 2 2
0 0 0
Output
Case 1:
2 2 3
1 8 2
Case 2:
INVALID
Case 3:
0 1 2 3
9 0 2 3
Case 4:
0 0 0 0 0
0 0 20 0 0

题目链接:http://codeforces.com/gym/100952/problem/I

题目大意:
玩家1和玩家2玩一个游戏,每个人有n堆石子
游戏规则为:
玩家取自己的n堆石子中的一堆中全部石子,以顺时针顺序,每个石子堆放一个石子,直达全部放完。
注意:如果得到的答案是在第一行中,视为无效。

题目思路:

1.将石子堆化为一维
2.取其中最小的石子数为minnum,答案一定是在石子数为minnum的石子堆中。
3.判断哪一个最小值为答案,即,距离起点最近的那一个石子堆

以下是AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(ll x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
const int N=;
ll num[N];
vector<int>v;
#define INF 0x3f3f3f3f3f;
int main()
{
int tcase=;
int n,r,c;
while(scanf("%d%d%d",&n,&r,&c)!=EOF)
{
if(n==&&r==&&c==)
break;
v.clear();
ll pos=;
ll minnum=INF;
for(int i=;i<=n;i++)
{
num[i]=read();
minnum=min(minnum,num[i]);
}
for(int i=*n;i>n;i--)
{
num[i]=read();
minnum=min(minnum,num[i]);
}
for(int i=;i<=*n;i++)
{
if(num[i]==minnum)
v.push_back(i);
}
if(r==)
pos=c;
else pos=*n-c+;
ll ans=minnum**n;
ll len=v.size();
ll dis=INF;
for(int i=;i<len;i++)
{
if(v[i]>=pos)
dis=min(dis,v[i]-pos);
else dis=min(dis,*n-pos+v[i]);
}
int ed=(dis+pos)%(*n);
if(ed==)
ed=*n;
printf("Case %d:\n",tcase++);
if(ed<=n)
cout<<"INVALID"<<endl;
else
{
for(int i=;i<=*n;i++)
num[i]-=minnum;
for(int i=pos;i<pos+dis;i++)
{
int ret=i%(*n);
if(ret==)
ret=*n;
num[ret]--;
}
int ret=(pos+dis)%(*n);
if(ret==)
ret=*n;
num[ret]=ans+dis;
cout<<num[];
for(int i=;i<=n;i++)
cout<<" "<<num[i];
cout<<endl;
cout<<num[*n];
for(int i=*n-;i>n;i--)
cout<<" "<<num[i];
cout<<endl;
}
}
return ;
}

Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】的更多相关文章

  1. Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】

    E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...

  2. Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】

    F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...

  3. Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】

    J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...

  4. Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】

    H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  5. Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】

    G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...

  6. Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】

    D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  7. Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】

    C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...

  8. Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】

    B. New Job time limit per test:1 second memory limit per test:64 megabytes input:standard input outp ...

  9. Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】

    A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...

随机推荐

  1. C++各种指针辨析

    1)int *p p与*结合,表明p是一个指针 然后前面int说明p是一个整形的指针 2)int *p[n] 因为[]比*优先级高,所以p先与[]结合,表明p是个数组,然后这个数组在与*结合,说明数组 ...

  2. Foreign websites

    [社交] 1.Twitter. It's what's happening. 2.Telegram Messenger 3.Facebook - Log In or Sign Up 4.Instagr ...

  3. Effective Java 第三版——11. 重写equals方法时同时也要重写hashcode方法

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  4. sql经典试题

    1.一道SQL语句面试题,关于group by表内容:2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2 ...

  5. Linux整合Apache和SVN

    1.安装APR-1.2.7和APR-util-1.2.7  (下载地址:http://apr.apache.org/) #tar zxvf  apr-1.2.7.tar.gz #cd   apr-1. ...

  6. 微信语音红包小程序开发如何提高精准度 红包小程序语音识别精准度 微信小程序红包开发语音红包

    公司最近开发的一个微信语音红包,就是前些时间比较火的包你说红包小程序.如何提高识别的精准度呢. 在说精准度之前,先大概说下整个语音识别的开发流程.前面我有文章已经说到过了.具体我就不谈了.一笔带过. ...

  7. asp.net 未能加载文件或程序集“WebApi”或它的某一个依赖项。试图加载格式不正确的程序。

    http://blog.csdn.net/lingxyd_0/article/details/43155039 一般情况下出现这样的问题是因为.dll文件不存在或者路径不正确.但今天我遇到的情况都不在 ...

  8. Web开发入门学习笔记

    公司web项目终于要启动了,本以为django学习可以在实战中进行,结果最终使用了Drupal框架,好吧,那我们就PHP走起,买了本<细说PHP>,先跟着过一遍Web开发入门. HTTP协 ...

  9. Hibernate学习笔记(5)---Query接口

    Hibernate中具有三种检索方式(HQL,QBC,SQL) Query接口 一个查询接口,用于向数据库中查询对象.并控制执行查询的过程.Query接口内封装了一个HQL查询语句. 举个栗子 //查 ...

  10. 如何在开发时部署和运行前后端分离的JavaWeb项目

    在开发中大型的JavaEE项目时,前后端分离的框架逐渐成为业界的主流,传统的单机部署前后端在同一个项目中的工程项目越来越少.这类JavaWeb项目的后端通常都采用微服务的架构,后端会被分解为诸多个小项 ...