Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】
2017-07-25 12:20
579 查看
I. Mancala
time limit per test:3 secondsmemory 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=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } inline void write(ll x) { if(x<0) { putchar('-'); x=-x; } if(x>9) write(x/10); putchar(x%10+'0'); } const int N=100010; ll num ; vector<int>v; #define INF 0x3f3f3f3f3f; int main() { int tcase=1; int n,r,c; while(scanf("%d%d%d",&n,&r,&c)!=EOF) { if(n==0&&r==0&&c==0) break; v.clear(); ll pos=0; ll minnum=INF; for(int i=1;i<=n;i++) { num[i]=read(); minnum=min(minnum,num[i]); } for(int i=2*n;i>n;i--) { num[i]=read(); minnum=min(minnum,num[i]); } for(int i=1;i<=2*n;i++) { if(num[i]==minnum) v.push_back(i); } if(r==1) pos=c; else pos=2*n-c+1; ll ans=minnum*2*n; ll len=v.size(); ll dis=INF; for(int i=0;i<len;i++) { if(v[i]>=pos) dis=min(dis,v[i]-pos); else dis=min(dis,2*n-pos+v[i]); } int ed=(dis+pos)%(2*n); if(ed==0) ed=2*n; printf("Case %d:\n",tcase++); if(ed<=n) cout<<"INVALID"<<endl; else { for(int i=1;i<=2*n;i++) num[i]-=minnum; for(int i=pos;i<pos+dis;i++) { int ret=i%(2*n); if(ret==0) ret=2*n; num[ret]--; } int ret=(pos+dis)%(2*n); if(ret==0) ret=2*n; num[ret]=ans+dis; cout<<num[1]; for(int i=2;i<=n;i++) cout<<" "<<num[i]; cout<<endl; cout<<num[2*n]; for(int i=2*n-1;i>n;i--) cout<<" "<<num[i]; cout<<endl; } } return 0; }
相关文章推荐
- Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】
- Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
- Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
- Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】
- Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】
- Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】
- Gym - 101572G -(set&细节)|(模拟&理解)|树状数组&好题-Galactic Collegiate Programming Contest
- CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A.水题 100952B.水题 100952C.回文字符串 100952D.杨辉三角处理组合数 其他题目待续。。。
- CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A-Who is the winner?
- Codeforces Gym 2015 ACM Amman Collegiate Programming Contest
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest
- Sudoku - The 2015 China Collegiate Programming Contest && UESTC 1222
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
- 【Codeforces】2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) A Adjoin the Netwo
- Codeforces gym 2014 Nordic Collegiate Programming Contest(最初版,有过题再更新)
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)