Game of the Rows CodeForces - 839B

Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}{3, 4}{4, 5}{5, 6} or{7, 8}.

  A row in the airplane

Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats.

Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied.

Input

The first line contains two integers n and k (1 ≤ n ≤ 100001 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively.

The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where aidenotes the number of soldiers in the i-th group.

It is guaranteed that a1 + a2 + ... + ak ≤ 8·n.

Output

If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes).

You can choose the case (lower or upper) for each letter arbitrary.

Example
Input
2 2
5 8
Output
YES
Input
1 2
7 1
Output
NO
Input
1 2
4 4
Output
YES
Input
1 4
2 2 1 2
Output
YES
Note

In the first sample, Daenerys can place the soldiers like in the figure below:

In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group.

In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats.

In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).


比较坑的一道CF题目,重点是贪心的方法,代码不是很重要;

贪心策略:

1:先安排中间的四个座位,然后剩下的两侧的座位就可以随便坐了。

2:中间的座位先安排一组的人坐一起,就是a[i]>=4的组;

3:a[i]==1的组在中间安排的时候竖着排,到n之后再竖着排。

4:比较坑的是安排中间四个座位的时候a[i]=2的情况,两组横着,一组竖着。

解释不清了,自己理解吧。

用这个题做个纪念,重要的还是自己解得过程。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[110];
int main()
{
	int n,k;
	cin >> n >> k;
	int sum = 0;
	int f1 = 1;
	int ok = 1;
	for(int i=1; i<=k; i++)
	{
	   cin >> a[i]; 
	   if(a[i]>=4 && f1)
	   {
	   	 sum += a[i] - a[i]%4;
	   	 if(sum > n*4)
		 {
		   sum -= a[i] - a[i]%4;
		   int w = a[i]/4;
		   for(int k=1; k<=w; k++)
		   {
		      if(sum + 4 > n*4)break;
			  sum += 4; a[i] -= 4; 
		   }
		   f1 = 0; continue;
		 } 
		 a[i] = a[i]%4; 
	   }
    }
    int sum1 = sum;
    int sum2 = sum;
    if(sum == n*4)
    {
       for(int i=1; i<=k; i++)
	  {
	    sum1 += a[i] - a[i]%2; 
		if(a[i]%2==1) sum1 += 2;
	  } 
	  if(sum1 > 8*n) ok = 0;
    }
    else
    {
       int yu = n - sum/4;
	   for(int i=1; i<=k; i++)	
       {
       	  if(yu<=0)break;
       	  if(a[i]==3) {sum2 += 4; a[i]=0; yu--;}
	   }
	   if(yu > 0)
	   {
		 int yi = 0;
		 for(int i=1; i<=k; i++)
	   	 {
	   	   if(a[i]==1) {yi++;}
	     }
	     sum2 += yi*2;
	     int mmp = 0;
	     if(yi >= yu*2)
	     {  
		    yu = 0;
	     	yi -= yu*2;
		 }
	     else if(yi < yu*2)
	     {
	       if(yi<=yu)
	       {
	       	 mmp = yi;
			 yu -= yi; yi = 0;
		   }
		   else if(yi>yu)
		   {
		   	    mmp = yu-yi+yu;
				yu = 0;
		   }
		 }
		 int er = 0;
		 for(int i=1; i<=k; i++)
		 {
		 	if(a[i]==2) ++er;
		 }
		 //cout << er << endl;
		 /*for(int i=1; i<=k; i++)
	     {
	     	if(a[i]!=2)continue;
			if(yu>0)
	     	{
	     		sum2 += 4; yu--;
			}
			else sum2 += 2;
		 }*/
		 if(mmp>=er)
		 {
		 	sum2 += er*2;
		 }
		 else if(mmp < er)
		 {
		 	sum2 += mmp*2;
			 er -= mmp;
			 if(yu > 0)
			 {
		       int pw = er/3*2;
			   if(yu>=pw)
			   {
			   	 sum2 += er/3*8;
			   	 er = 0;
			   }
			   else if(yu<pw)
			   {
			   	  sum2 += yu/2*8;
			   	  er -= yu/2*3;
			   	  yu = yu%2;
			   	  sum2 += yu*4;
			   	  er--;
			   	  sum2 += er*2;
			   } 	
			 }else if(yu==0)
			 {
			 	sum2 += er*2;
			 }
		 }
	   }
	   else if(yu==0) // 四连坐没有了 
	   {
	     	for(int i=1; i<=k; i++)
	     	{
	     		if(a[i]==3){sum2 += 4;continue;}
				 if(a[i]!=0) sum2 += 2;
			}
	   } 
	   if(sum2 > 8*n )ok = 0;
	}
    //cout << sum2 << endl;
	if(!ok) cout << "NO" << endl;
	else cout << "YES\n" << endl;
  return 0;	
} 
水波.




以下是一个简单的C++实现Game of Life的示例代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int ROWS = 20; const int COLS = 60; // 初始化格子 void initializeGrid(bool grid[][COLS]) { srand(time(NULL)); for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { int random = rand() % 10; if (random < 3) { grid[row][col] = true; } else { grid[row][col] = false; } } } } // 显示格子 void displayGrid(bool grid[][COLS]) { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (grid[row][col]) { cout << "*"; } else { cout << " "; } } cout << endl; } } // 计算每个格子周围活细胞的数量 int countNeighbors(bool grid[][COLS], int row, int col) { int count = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int neighborRow = row + i; int neighborCol = col + j; if (neighborRow >= 0 && neighborRow < ROWS && neighborCol >= 0 && neighborCol < COLS) { if (i != 0 || j != 0) { if (grid[neighborRow][neighborCol]) { count++; } } } } } return count; } // 更新格子 void updateGrid(bool grid[][COLS]) { bool newGrid[ROWS][COLS]; for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { int neighbors = countNeighbors(grid, row, col); if (grid[row][col]) { if (neighbors == 2 || neighbors == 3) { newGrid[row][col] = true; } else { newGrid[row][col] = false; } } else { if (neighbors == 3) { newGrid[row][col] = true; } else { newGrid[row][col] = false; } } } } for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { grid[row][col] = newGrid[row][col]; } } } // 主函数 int main() { bool grid[ROWS][COLS]; initializeGrid(grid); while (true) { system("cls"); // 清屏 displayGrid(grid); updateGrid(grid); } return 0; } ``` 以上代码使用二维布尔数组来表示格子的状态,并且使用函数来完成初始化、显示、计算和更新等操作。在主函数中,我们先使用 `initializeGrid` 函数来随机初始化格子的状态,然后使用 `displayGrid` 函数来显示格子,最后使用 `updateGrid` 函数来更新格子的状态。由于Game of Life是一个无限大的世界,因此我们在这里使用了一个简单的循环来模拟它。 需要注意的是,由于在Windows系统中,清屏需要使用 `system("cls")` 命令,因此以上代码只能在Windows系统中运行。如果你想在其他操作系统中运行该代码,需要使用相应的清屏命令。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值