欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Codeforces Round #478 (Div. 2) B. Mancala

程序员文章站 2022-05-01 08:02:45
...
题目链接:Mancala
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.

Codeforces Round #478 (Div. 2) B. Mancala

Initially, each hole has ai stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.

Note that the counter-clockwise order means if the player takes the stones from hole i, he will put one stone in the (i+1)-th hole, then in the (i+2)-th, etc. If he puts a stone in the 14-th hole, the next one will be put in the first hole.

After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.

Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.

Input

The only line contains 14 integers a1,a2,,a14 (0ai109) — the number of stones in each hole.

It is guaranteed that for any i(1i14) ai is either zero or odd, and there is at least one stone in the board.

Output

Output one integer, the maximum possible score after one move.

Examples
Input
Copy
0 1 1 0 0 0 0 0 0 7 0 0 0 0
Output
Copy
4
Input
Copy
5 1 1 1 1 0 0 0 0 0 0 0 0 0
Output
Copy
8
Note

In the first test case the board after the move from the hole with 7 stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to 4.


题意:有14个洞,每个洞有一个值啊a[i],从中找一个值大于0的洞,(把这14个洞看成一个环),然后依次给它下一个洞的值+1,并且立刻让自己这个洞的值变为0,(注意只能从中选一个洞进行这个操作,循环到自己本身也是要+1的),最后让你求这些洞值为偶数的和的最大值。
做法基本上是遍历一遍14个洞,然后遍历过程中取一个max,但题目数据ai<=1e9,直接暴力肯定会TLE的,我的一种遍历思路是分两种情况:
1.当a[i]<=14时,直接暴力。
2.a[i]>14时,求一个x=a[i]/14,k=a[i]%14.就可以表示从a[i]开始循环x圈后再走k步,走的那k步总共加了x+1次,其他的只加了x次。这样就可以一次性加完了。
这道题疯狂wa了好多发,哎,还是太菜了啊。

AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
#define IO ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define rep(i,n) for(int i=1;i<=n;i++)
#define m(a,b) memset(a,b,sizeof(a))
typedef pair<int, int> PII;
typedef long long ll;
const int MAXN=1e5+10;

int main(){
	IO;
	ll a[15],b[15],ans=0,cnt;
	for(int i=1;i<=14;i++) cin>>a[i];
	a[0]=0;
	for(int i=1;i<=14;i++){
		cnt=0;
		memcpy(b,a,sizeof(ll)*15);
		if (b[i]>0){
			if (b[i]<=14){
				b[i]=0;
				for(int j=i+1,m=1;m<=a[i];j++,m++){
					if (j>14) j%=14;
					b[j]++;
				}
			}
			else{
				int x=b[i]/14,k=b[i]%14;
				b[i]=0;
				for(int j=i+1,m=1;m<=k;j++,m++){
					if (j>14) j%=14;
					b[j]+=(x+1);
				}
				for(int j=i+k+1,m=1;m<=14-k;j++,m++){
					if (j>14) j%=14;
					b[j]+=x;
				}
			}
		}
		for(int j=1;j<=14;j++)
			if (b[j]%2==0)
				cnt+=b[j];
		ans=max(ans,cnt);
	}
	cout<<ans<<endl;
	return 0;
}



相关标签: cf