Atcoder ABC 069 C - 4-adjacent D - Grid Coloring

C - 4-adjacent



Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

We have a sequence of length N, a=(a1,a2,…,aN). Each ai is a positive integer.

Snuke‘s objective is to permute the element in a so that the following condition is satisfied:

  • For each 1≤iN?1, the product of ai and ai+1 is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

  • 2≤N≤105
  • ai is an integer.
  • 1≤ai≤109

Input

Input is given from Standard Input in the following format:

N
a1 a2  aN

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.


Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1,100,10).


Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.


Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.


Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes1~n-1之间保证a[i]*a[i+1]%4==0
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime>
    #include <map>
    #include <set>
    using namespace std;
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>y?x:y)
    #define min(x,y) (x<=y?x:y)
    #define MAX 100000000000000000
    #define MOD 1000000007
    #define pi acos(-1.0)
    #define ei exp(1)
    #define PI 3.141592653589793238462
    #define ios() ios::sync_with_stdio(false)
    #define INF 1044266558
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    int a,b,c,x,n;
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            a=b=c=0;
            for(int i=0;i<n;i++)
            {
                scanf("%d",&x);
                if(!(x%4)) a++;
                else if(x&1) b++;
                else c++;
            }
            if(!c) b--;
            puts(a>=b?"Yes":"No");
        }
        return 0;
    }

D - Grid Coloring



Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

We have a grid with H rows and W columns of squares. Snuke is painting these squares in colors 1, 2, , N. Here, the following conditions should be satisfied:

  • For each i (1≤iN), there are exactly ai squares painted in Color i. Here, a1+a2+…+aN=HW.
  • For each i (1≤iN), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.

Find a way to paint the squares so that the conditions are satisfied. It can be shown that a solution always exists.

Constraints

  • 1≤H,W≤100
  • 1≤NHW
  • ai≥1
  • a1+a2+…+aN=HW

Input

Input is given from Standard Input in the following format:

H W
N
a1 a2  aN

Output

Print one way to paint the squares that satisfies the conditions. Output in the following format:

c11  c1W
:
cH1  cHW

Here, cij is the color of the square at the i-th row from the top and j-th column from the left.


Sample Input 1

2 2
3
2 1 1

Sample Output 1

1 1
2 3

Below is an example of an invalid solution:

1 2
3 1

This is because the squares painted in Color 1 are not 4-connected.


Sample Input 2

3 5
5
1 2 3 4 5

Sample Output 2

1 4 4 4 3
2 5 4 5 3
2 5 5 5 3

Sample Input 3

1 1
1
1

Sample Output 3

1h*w的网格,填充颜色,颜色种类为n,a[i]*****a[n],为每种颜色的个数,保证所填充相等颜色之间必须联通,蛇形填充就行。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int g[110][110];
int h,w,n,x,k;
int main()
{
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
        mem(g);
        k=-1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            while(x--) k++,g[(k/h)&1?(h-1-(k%h)):k%h][k/h]=i;
        }
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                if(j) printf(" ");
                printf("%d",g[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
				
时间: 2024-10-09 16:30:45

Atcoder ABC 069 C - 4-adjacent D - Grid Coloring的相关文章

AtCoder ABC 129F Takahashi&#39;s Basics in Education and Learning

题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_f 题目大意 给定一个长度为 L ,首项为 A,公差为 B 的等差数列 S,将这 L 个数拼起来,记作 N,求 N % M. 分析 设 bit(i) 为第 i 项所需要进行的十进制位移. 则 $N = S_0 * 10^{bit(0)} + S_1 * 10^{bit(1)} + \dots + S_{L - 1} * 10^{bit(L - 1)}$. 一项一项地算是肯定要超时的,不过注意

Atcoder ABC 141

Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; char ch[50]; int main() { scanf("%s",ch+1); if(ch[1] == 'S') puts("Cloudy

题解 [Atcoder ABC 161] A,B,C

题解 [Atcoder ABC 161] A,B,C A: 水题,按题意模拟即可. code: #include<bits/stdc++.h> #define ft(i,l,r) for(register int i=l;i<=r;i++) #define fd(i,r,l) for(register int i=r;i>=l;i--) using namespace std; int a,b,c; int main() { cin>>a>>b>>

Atcoder ABC 071 C,D

C - Make a Rectangle Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement We have N sticks with negligible thickness. The length of the i-th stick is Ai. Snuke wants to select four different sticks from these sticks and form

AtCoder ABC 127F Absolute Minima

题目链接:https://atcoder.jp/contests/abc127/tasks/abc127_f 题目大意 初始状态下$f(x) = 0$,现在有 2 种模式的询问,第一种以“1 a b”的形式,需要进行操作$f(x) = f(x) + |x - a| + b$:第二种以“2”的形式,求使得 f(x) 取得最小值的 x 取值和 f(x) 值,如果有多个 x,输出任意一个即可. 分析 考虑第一种询问已经出现了 k 次,现在遇到第二种询问.此时$f(x) = \sum_{i = 1}^k

AtCoder ABC 130F Minimum Bounding Box

题目链接:https://atcoder.jp/contests/abc130/tasks/abc130_f 题目大意 给定地图上 N 个点的坐标和移动方向,它们会以每秒 1 个单位的速度移动,设 Ans(t) 为在 t 时刻,$(x_{max} - x_{min}) * (y_{max} - y_{min})$的值,求 Ans(t) 的最小值.(最小值可能不是一个整数) 分析 稍加思考可以发现,不是所有点的所有坐标都对答案有影响,很多点完全可以忽略不计,下面以 Y 坐标为例,讨论影响$(y_{

AtCoder ABC 154E Almost Everywhere Zero

题目链接:https://atcoder.jp/contests/abc154/tasks/abc154_e 题目大意 给定一个整数N($1 \leq N \leq 10^{100}$)和K($1 \leq K \leq 3$),求[1, N]区间内数位上只有K个非零数的整数个数. 分析 找一下规律即可,详情见代码注释. 代码如下 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 /*-------------------Define

AtCoder ABC 155D Pairs

题目链接:https://atcoder.jp/contests/abc155/tasks/abc155_d 题目大意 给定$N$个整数$A_1, A_2, \dots, A_N$, 求集合$S = \{A_i * A_j | 1 \leq i, j \leq N 且 i \neq j\}$的第$K$小. 分析 首先,通过把$N$个数分为正数,负数,零,再排下序,我们可以计算$S$中所有的正数,负数以及零的数目,进而可以判断出第$K$小的数是正数,是负数,还是零. 如果第$K$小的数是零,无需多

AtCoder ABC 155F Perils in Parallel

题目链接:https://atcoder.jp/contests/abc155/tasks/abc155_f 题目大意 分析 代码如下 原文地址:https://www.cnblogs.com/zaq19970105/p/12340031.html