C - 4-adjacent

Time limit : 2sec / Memory limit : 256MB

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≤i≤N−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

Copy

3

1 10 100

Sample Output 1

Copy

Yes

One solution is (1,100,10).

题意

给你一个长度为n的序列,然后让你重排列,使得任意相邻的两个数相乘都是4的倍数

题解

4 = 2^2,那么我们把所有数分为奇数,偶数,4的倍数三种,最后的排列,我们贪心一下可以发现,只要所有偶数全部放在一起,然后奇数和4的倍数交叉放就行。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e5+7;
  4. int flag[maxn];
  5. int a[maxn];
  6. int n;
  7. int main(){
  8. scanf("%d",&n);
  9. for(int i=0;i<n;i++){
  10. cin>>a[i];
  11. if(a[i]%4==0){
  12. flag[2]++;
  13. }else if(a[i]%2==0){
  14. flag[1]++;
  15. }else{
  16. flag[0]++;
  17. }
  18. }
  19. if(flag[1])flag[0]++;
  20. if(flag[0]-1>flag[2]){
  21. cout<<"No"<<endl;
  22. }else{
  23. cout<<"Yes"<<endl;
  24. }
  25. }

D - Grid Coloring

Time limit : 2sec / Memory limit : 256MB

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≤i≤N), there are exactly ai squares painted in Color i. Here, a1+a2+…+aN=HW.

For each i (1≤i≤N), 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≤N≤HW

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.

题意

给你ai表示第i个数有ai个,然后让你摆在一个HW的方阵里面,需要满足同一个数需要四联通放在一起。

题解

这个题目换个意思理解就是蛇形填数

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 105;
  4. int mp[maxn][maxn];
  5. int n,w,h,x,a[100005];
  6. int main(){
  7. scanf("%d%d",&h,&w);
  8. scanf("%d",&n);
  9. for(int i=1;i<=n;i++)
  10. scanf("%d",&a[i]);
  11. x=1;
  12. for(int i=1;i<=h;i++){
  13. if(i%2==1){
  14. for(int j=1;j<=w;j++){
  15. if(a[x]){
  16. mp[i][j]=x;
  17. a[x]--;
  18. }else{
  19. while(a[x]==0)x++;
  20. mp[i][j]=x;
  21. a[x]--;
  22. }
  23. }
  24. }else{
  25. for(int j=w;j>=1;j--){
  26. if(a[x]){
  27. mp[i][j]=x;
  28. a[x]--;
  29. }else{
  30. while(a[x]==0)x++;
  31. mp[i][j]=x;
  32. a[x]--;
  33. }
  34. }
  35. }
  36. }
  37. for(int i=1;i<=h;i++){
  38. for(int j=1;j<=w;j++){
  39. cout<<mp[i][j]<<" ";
  40. }
  41. cout<<endl;
  42. }
  43. }

E - Young Maids

Time limit : 2sec / Memory limit : 256MB

Problem Statement

Let N be a positive even number.

We have a permutation of (1,2,…,N), p=(p1,p2,…,pN). Snuke is constructing another permutation of (1,2,…,N), q, following the procedure below.

First, let q be an empty sequence. Then, perform the following operation until p becomes empty:

Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.

When p becomes empty, q will be a permutation of (1,2,…,N).

Find the lexicographically smallest permutation that can be obtained as q.

Constraints

N is an even number.

2≤N≤2×105

p is a permutation of (1,2,…,N).

Input

Input is given from Standard Input in the following format:

N

p1 p2 … pN

Output

Print the lexicographically smallest permutation, with spaces in between.

Sample Input 1

4

3 2 4 1

Sample Output 1

3 1 2 4

The solution above is obtained as follows:

p q

(3,2,4,1) ()

↓ ↓

(3,1) (2,4)

↓ ↓

() (3,1,2,4)

题意

给你一个长度为n的序列p,你每次需要抽出两个相邻的元素,然后把这两个数按照原来的顺序放在q的前面,直到p数组被抽完。

然后输出字典序最小解。

题解

倒着贪心,最后我们放在最前面的,就是最小的奇数加上最小的偶数。

然后我们放完这段之后,我们发现我们把原来的区间就会砍为三段,然后再在每一段找到最小的两个数即可。

不停的贪心下去就好了。

代码

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. using namespace std;
  5. const int maxn = 2e5+7;
  6. int n,a[maxn],log[maxn],f[2][18][maxn],pos[maxn];
  7. int rmq(int k,int l,int r){
  8. int j = log[r-l+1];
  9. return min(f[k][j][l],f[k][j][r-(1<<j)+1]);
  10. }
  11. pair<int,int> cal(int l,int r){
  12. int x = rmq(l&1,l,r);
  13. int y = rmq((pos[x]+1)&1,pos[x]+1,r);
  14. return {-x,-y};
  15. }
  16. int main(){
  17. scanf("%d",&n);
  18. for(int i=0;i<n;i++){
  19. scanf("%d",&a[i]);
  20. pos[a[i]]=i;
  21. }
  22. for(int i=2;i<=n;i++){
  23. log[i]=log[i>>1]+1;
  24. }
  25. for(int l=0;l<2;l++){
  26. for(int i=0;i<n;i++){
  27. f[l][0][i]=(i%2==l)?a[i]:1<<30;
  28. }
  29. for(int k=1;1<<k<=n;k++){
  30. for(int j=0;j+(1<<k)-1<n;j++){
  31. f[l][k][j]=min(f[l][k-1][j],f[l][k-1][j+(1 << k - 1)]);
  32. }
  33. }
  34. }
  35. priority_queue< pair<pair<int,int> ,pair<int,int> > > Q;
  36. Q.push({cal(0,n-1),{0,n-1}});
  37. while(!Q.empty()){
  38. auto it = Q.top();Q.pop();
  39. int x = -it.first.first;
  40. int y = -it.first.second;
  41. printf("%d %d ",x,y);
  42. int l = it.second.first;
  43. int r = it.second.second;
  44. x = pos[x],y = pos[y];
  45. if(l<x-1){
  46. Q.push({cal(l,x-1),{l,x-1}});
  47. }
  48. if(x+1<y-1){
  49. Q.push({cal(x+1,y-1),{x+1,y-1}});
  50. }
  51. if(y+1<r){
  52. Q.push({cal(y+1,r),{y+1,r}});
  53. }
  54. }
  55. }

F - Prime Flip

Time limit : 2sec / Memory limit : 256MB

Problem Statement

There are infinitely many cards, numbered 1, 2, 3, … Initially, Cards x1, x2, …, xN are face up, and the others are face down.

Snuke can perform the following operation repeatedly:

Select a prime p greater than or equal to 3. Then, select p consecutive cards and flip all of them.

Snuke's objective is to have all the cards face down. Find the minimum number of operations required to achieve the objective.

Constraints

1≤N≤100

1≤x1<x2<…<xN≤107

Input

Input is given from Standard Input in the following format:

N

x1 x2 … xN

Output

Print the minimum number of operations required to achieve the objective.

Sample Input 1

2

4 5

Sample Output 1

2

Below is one way to achieve the objective in two operations:

Select p=5 and flip Cards 1, 2, 3, 4 and 5.

Select p=3 and flip Cards 1, 2 and 3

题意

在1e7的范围,有n个位置为1,其他位置都是0.

每次你可以选择连续的奇数素数长度的数反转(0变1,1变0),问你最少多少次操作,可以使得全部变为0

题解

倒着异或,可以把题目转换为每次我可以选择两个间隔为奇数素数长度的数反转,然后最少多少次操作可以使得全部为0

然后显然就是二分图的最大匹配了。

代码

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<string.h>
  5. using namespace std;
  6. const int maxn = 2e5+7;
  7. const int maxm = 1e7+7;
  8. int n,a[maxm];
  9. vector<int>v[2],E[maxn];
  10. bool bio[maxn];
  11. int conn[maxn];
  12. void init(){
  13. scanf("%d",&n);
  14. for(int i=1;i<=n;i++){
  15. int x;
  16. scanf("%d",&x);
  17. a[x]=1;
  18. }
  19. }
  20. bool prime(int x){
  21. if(x==1)return false;
  22. for(int i=2;i*i<=x;i++)
  23. if(x%i==0)return false;
  24. return true;
  25. }
  26. bool dfs(int x){
  27. if(bio[x])return false;
  28. bio[x]=true;
  29. for(auto it : E[x]){
  30. if(conn[it] == -1 || dfs(conn[it])){
  31. conn[it] = x;
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. int main(){
  38. init();
  39. memset(conn,-1,sizeof(conn));
  40. for(int i=maxm-1;i;i--){
  41. a[i]^=a[i-1];
  42. if(a[i])v[i%2].push_back(i);
  43. }
  44. for(int i=0;i<v[0].size();i++){
  45. for(int j=0;j<v[1].size();j++){
  46. if(prime(abs(v[0][i]-v[1][j]))){
  47. E[i].push_back(j);
  48. }
  49. }
  50. }
  51. int match = 0;
  52. for(int i=0;i<v[0].size();i++){
  53. memset(bio,false,sizeof(bio));
  54. match+=dfs(i);
  55. }
  56. cout<<v[0].size()+v[1].size()-match+(v[0].size()-match)%2;
  57. return 0;
  58. }

AtCoder Regular Contest 080 [CDEF]的更多相关文章

  1. AtCoder Regular Contest 080 E - Young Maids

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_c 题目: E - Young Maids Time limit : 2sec / Memory li ...

  2. AtCoder Regular Contest 080 D - Grid Coloring

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_b 题目: D - Grid Coloring Time limit : 2sec / Memory ...

  3. AtCoder Regular Contest 080 C - 4-adjacent

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_a 题目: C - 4-adjacent Time limit : 2sec / Memory lim ...

  4. AtCoder Regular Contest 080 E:Young Maids

    题目传送门:https://arc080.contest.atcoder.jp/tasks/arc080_c 题目翻译 给你一个\(n\)的排列\(p\),一个空序列\(q\),你每次可以从\(p\) ...

  5. AtCoder Regular Contest 080 (ARC080) E - Young Maids 线段树 堆

    原文链接http://www.cnblogs.com/zhouzhendong/p/8934377.html 题目传送门 - ARC080 E - Young Maids 题意 给定一个长度为$n$的 ...

  6. 【递归】【线段树】【堆】AtCoder Regular Contest 080 E - Young Maids

    给你一个1~n的排列p,n是偶数,每次从中任选一对相邻的数出来,插到排列q的开头,如此循环,问你所能得到的字典序最小的排列q. 我们先确定q开头的两个数q1,q2,q1一定是p的奇数位的最小的数,而q ...

  7. AtCoder Regular Contest 080

    手贱去开了abc,这么无聊.直接arc啊 C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Score : 400 points Prob ...

  8. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  9. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

随机推荐

  1. 解决Windows Server 2008R2通过计划任务定时执行bat文件,显示成功但实际未执行

    前段时间在Windows Server 2008安装了一套基于MySQL数据库的软件,处于数据安全的考虑,希望每天能够自动进行数据库备份.我在别人脚本的基础上自己写了一个数据库备份的bat脚本,双击该 ...

  2. 解开一个疑惑,为什么LVS开放的端口,使用netstat或ss命令,不能查找到其监听的端口呢?

    RT, 这个疑问,本周一直在心里,今天找到一个说法. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 另外LVS规则算是内核方法,用netstat -ntulp也显 ...

  3. linux改权限

    改变文件夹本身权限,不改动子文件(夹) chmod 600 my/ 改变文件夹及子目录下所有文件(夹)权限 chmod -R 777 my/ 统一修改 cd my 修改文件夹权限为755 find - ...

  4. 【转载】Python and Mysql Andy Dustman

    0. http://mysql-python.sourceforge.net/ Python and MySQL: This is a presentation I did a couple year ...

  5. python之多线程 queue 实践 筛选有效url

    0.目录 1.背景 某号码卡申请页面通过省份+城市切换归属地,每次返回10个号码. 通过 Fiddler 抓包确认 url 关键参数规律: provinceCode 两位数字 cityCode 三位数 ...

  6. 一起学Hadoop——二次排序算法的实现

    二次排序,从字面上可以理解为在对key排序的基础上对key所对应的值value排序,也叫辅助排序.一般情况下,MapReduce框架只对key排序,而不对key所对应的值排序,因此value的排序经常 ...

  7. pip安装其他包报错

    pip安装时报错  Unknown or unsupported command 'install 一.是否配置了路径 配置了看下面的方法. 二.有多个pip系统不知道调用哪个. 1.where pi ...

  8. 20165235 祁瑛 2018-4 《Java程序设计》第八周学习总结

    20165235 祁瑛 2018-4 <Java程序设计>第八周学习总结 教材学习内容总结 操作系统与进程 程序是一段静态的代码,它是应用软件执行的蓝本.进程是程序的一次动态执行过程,它对 ...

  9. spring mvc读取properties资源文件夹中文乱码问题

    通过在applicationContext.xml和springmvc.xml中配置 <bean        class="org.springframework.beans.fac ...

  10. Kafka 概念、单机搭建与使用

    目录 Kafka 概念.单机搭建与使用 基本概念介绍 Topic Producer Consumer Kafka单机配置,一个Broker 环境: 配置zookeeper 配置Kafka 使用Kafk ...