【codeforces #292(div 1)】ABC题解

A. Drazil and Factorial

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Drazil is playing a math game with Varda.

Let‘s define  for
positive integer x as a product of factorials of its digits. For example, .

First, they choose a decimal number a consisting of n digits
that contains at least one digit larger than 1. This number may possibly start with leading zeroes. Then they should find maximum positive number x satisfying
following two conditions:

1. x doesn‘t contain neither digit 0 nor digit 1.

2.  = .

Help friends find such number.

Input

The first line contains an integer n (1?≤?n?≤?15)
— the number of digits in a.

The second line contains n digits of a. There
is at least one digit in a that is larger than 1. Number a may
possibly contain leading zeroes.

Output

Output a maximum possible integer satisfying the conditions above. There should be no zeroes and ones in this number decimal representation.

Sample test(s)

input

4
1234

output

33222

input

3
555

output

555

Note

In the first case, 

贪心:

首先把原数x计算F(x)后所含有的123456789都统计出来。

然后把9拆成3*3;8拆成2*2*2;6拆成2*3;4拆成2*2;剩下的5和7不能拆,只能直接算阶乘。

最后就剩下了许多个3和许多个2了,此时3一定比2少。

最后从大到小依次输出7,5,3,2即可。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
char s[20];
int ans[10],cnt[10],a[10];
int main()
{
	int n;
        scanf("%d",&n);
	scanf("%s",s);
	for (int i=0;i<n;i++)
	{
		int x=s[i]-'0';
		for (int j=2;j<=x;j++)
			cnt[j]++;
	}
	a[1]=7,a[2]=5;
	for (int i=1;i<=2;i++)
		if (cnt[a[i]])
		{
			ans[a[i]]=cnt[a[i]];
			for (int j=2;j<=a[i];j++)
				cnt[j]-=ans[a[i]];
		}
	cnt[3]+=(cnt[9]*2);
	cnt[2]+=(cnt[8]*3);
	cnt[2]+=cnt[6],cnt[3]+=cnt[6];
	cnt[2]+=(cnt[4]*2);
	ans[3]=cnt[3],cnt[2]-=cnt[3];
	ans[2]=cnt[2];
	a[1]=7,a[2]=5,a[3]=3,a[4]=2;
	for (int i=1;i<=4;i++)
		for (int j=1;j<=ans[a[i]];j++)
			cout<<a[i];
	cout<<endl;
	return 0;
}

B. Drazil and Tiles

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Drazil created a following problem about putting 1?×?2 tiles into an n?×?m grid:

"There is a grid with some cells that are empty and some cells that are occupied. You should use 1?×?2 tiles to cover all empty cells and no two tiles should cover
each other. And you should print a solution about how to do it."

But Drazil doesn‘t like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique?
Otherwise contestant may print ‘Not unique‘ ".

Drazil found that the constraints for this task may be much larger than for the original task!

Can you solve this new problem?

Note that you should print ‘Not unique‘ either when there exists no solution or when there exists several different solutions for the original task.

Input

The first line contains two integers n and m (1?≤?n,?m?≤?2000).

The following n lines describe the grid rows. Character ‘.‘
denotes an empty cell, and the character ‘*‘ denotes a cell that is occupied.

Output

If there is no solution or the solution is not unique, you should print the string "Not unique".

Otherwise you should print how to cover all empty cells with 1?×?2 tiles. Use characters "<>"
to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.

Sample test(s)

input

3 3
...
.*.
...

output

Not unique

input

4 4
..**
*...
*.**
....

output

<>**
*^<>
*v**
<><>

input

2 4
*..*
....

output

*<>*
<><>

input

1 1
.

output

Not unique

input

1 1
*

output

*

Note

In the first case, there are indeed two solutions:

<>^
^*v
v<>

and

^<>
v*^
<>v

so the answer is "Not unique".

第一眼看到这道题心想:这不是染色+二分图匹配吗?

然后就开始想怎么快速判断是否二分图的完全匹配是否唯一??最后光荣的在第36个数据上T了。。(谁有快速的方法。。求告知vv)

看题解才知道,其实和二分图匹配一点关系都没有。。

有点类似于拓扑排序:能够用同一个纸片覆盖的格子连一条边,把两个格子的度数+1(两个格子之间最多一条边)。

扫描一次所有格子的度数,度数为1的加入队列,那么这些格子所对应的与他们一块被覆盖的格子是唯一的,出队之后把队列中对应格子的对应格子度数-1,度数为1的再次入队。

如果队列为空之后,所有格子都被覆盖好了,那么此图有且仅有一个解;如果有格子没有被覆盖好,直接输出"Not unique",可能无解,也可能多解(是哪种就不用管了)

代码中Judge注释掉的部分是暴力用二分图来判定的;输出答案的部分直接用二分图来做了。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
int b,w,xx,yy,n,m,t,ti,v[2005][2005],fx[10][3],in[2005][2005],a[2005][2005],p[2005][2005];
char x[10],s[2005];
struct data
{
	int x,y;
}my[2005][2005];
queue<data> q;
bool dfs(int x,int y)
{
	for (int i=1;i<=4;i++)
	{
		int nx=x+fx[i][1],ny=y+fx[i][2];
		if (nx<1||ny<1||nx>n||ny>m||a[nx][ny]!=1||v[nx][ny]==t) continue;
		v[nx][ny]=t;
		if (!my[nx][ny].x||dfs(my[nx][ny].x,my[nx][ny].y))
		{
			my[nx][ny].x=x,my[nx][ny].y=y;
			return true;
		}
	}
	return false;
}
bool dfs2(int x,int y,int k)
{
	for (int i=1;i<=4;i++)
	{
		int nx=x+fx[i][1],ny=y+fx[i][2];
		if (nx==xx&&ny==yy&&!k) continue;
		if (nx<1||ny<1||nx>n||ny>m||a[nx][ny]!=1||v[nx][ny]==t) continue;
		v[nx][ny]=t;
		if (!my[nx][ny].x||dfs2(my[nx][ny].x,my[nx][ny].y,1))
		{
			my[nx][ny].x=x,my[nx][ny].y=y;
			return true;
		}
	}
	return false;
}
bool Judge()
{
	/*
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			if (!a[i][j])
			{
				t++;
				for (int k=1;k<=4;k++)
				{
					int nx=i+fx[k][1],ny=j+fx[k][2];
		            if (my[nx][ny].x==i&&my[nx][ny].y==j) xx=nx,yy=ny;
	            }
				if (in[xx][yy]==1) continue;
	            my[xx][yy].x=0;
				if (dfs2(i,j,0)) return false;
	            my[xx][yy].x=i,my[xx][yy].y=j;
			}
	return true;*/
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
		{
			v[i][j]=0;
			if (in[i][j]==1)
			{
				data x;
				x.x=i,x.y=j;
				q.push(x);
			}
		}
	while (!q.empty())
	{
		data x=q.front();
		q.pop();
		if (v[x.x][x.y]) continue;
		v[x.x][x.y]=1;
		for (int i=1;i<=4;i++)
		{
			int nx=x.x+fx[i][1],ny=x.y+fx[i][2];
			if (nx<1||ny<1||nx>n||ny>m||v[nx][ny]||a[nx][ny]==-1) continue;
			v[nx][ny]=1;
			for (int j=1;j<=4;j++)
			{
				int nxx=nx+fx[j][1],nyy=ny+fx[j][2];
				in[nxx][nyy]--;
				if (in[nxx][nyy]==1)
				{
					data X;
					X.x=nxx,X.y=nyy;
					q.push(X);
				}
			}
		}
	}
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			if (a[i][j]!=-1&&!v[i][j]) return false;
	return true;
}
void Print()
{
	x[1]='*',x[2]='<',x[3]='>',x[4]='^',x[5]='v';
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			if (a[i][j]==1)
			{
				int x=my[i][j].x,y=my[i][j].y;
				if (x==i)
				{
					p[x][min(j,y)]=2,p[x][max(j,y)]=3;
				}
				else
				{
					p[min(x,i)][j]=4,p[max(x,i)][j]=5;
				}
			}
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++)
		{
			if (!p[i][j]) printf("*");
			else printf("%c",x[p[i][j]]);
		}
		printf("\n");
	}
}
int main()
{
	fx[1][1]=fx[2][1]=0,fx[1][2]=1,fx[2][2]=-1;
	fx[3][2]=fx[4][2]=0,fx[3][1]=1,fx[4][1]=-1;
    scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%s",s);
		for (int j=0;j<m;j++)
		{
			if (s[j]=='*') a[i][j+1]=-1;
			else a[i][j+1]=1&(i+j+1);
		}
	}
	b=0,w=0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
		{
			if (a[i][j]==1)
			{
				b++;
				for (int k=1;k<=4;k++)
				{
					int nx=i+fx[k][1],ny=j+fx[k][2];
					if (nx<1||ny<1||nx>n||ny>m) continue;
					if (a[nx][ny]==0)in[i][j]++,in[nx][ny]++;
				}
			}
			if (!a[i][j]) w++;
		}
	if (w!=b)
	{
		puts("Not unique");
		return 0;
	}
	int ans=0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			if (!a[i][j])
			{
				t++;
				if (dfs(i,j))
					ans++;
			}
	if (ans!=w)
	{
		puts("Not unique");
		return 0;
	}
	if (Judge())
	{
		Print();
	}
	else
	{
		puts("Not unique");
	}
	return 0;
}

C. Drazil and Park

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Drazil is a monkey. He lives in a circular park. There are n trees around the park. The distance between the i-th
tree and (i?+?1)-st trees is di,
the distance between the n-th tree and the first tree is dn.
The height of the i-th tree is hi.

Drazil starts each day with the morning run. The morning run consists of the following steps:

  • Drazil chooses two different trees
  • He starts with climbing up the first tree
  • Then he climbs down the first tree, runs around the park (in one of two possible directions) to the second tree, and climbs on it
  • Then he finally climbs down the second tree.

But there are always children playing around some consecutive trees. Drazil can‘t stand children, so he can‘t choose the trees close to children. He even can‘t stay close to those trees.

If the two trees Drazil chooses are x-th and y-th,
we can estimate the energy the morning run takes to him as 2(hx?+?hy)?+?dist(x,?y).
Since there are children on exactly one of two arcs connecting x and y,
the distance dist(x,?y) between trees x and yis
uniquely defined.

Now, you know that on the i-th day children play between ai-th
tree and bi-th
tree. More formally, if ai?≤?bi,
children play around the trees with indices from range [ai,?bi],
otherwise they play around the trees with indices from .

Please help Drazil to determine which two trees he should choose in order to consume the most energy (since he wants to become fit and cool-looking monkey) and report the resulting amount of energy for each day.

Input

The first line contains two integer n and m (3?≤?n?≤?105, 1?≤?m?≤?105),
denoting number of trees and number of days, respectively.

The second line contains n integers d1,?d2,?...,?dn (1?≤?di?≤?109),
the distances between consecutive trees.

The third line contains n integers h1,?h2,?...,?hn (1?≤?hi?≤?109),
the heights of trees.

Each of following m lines contains two integers ai and bi (1?≤?ai,?bi?≤?n)
describing each new day. There are always at least two different trees Drazil can choose that are not affected by children.

Output

For each day print the answer in a separate line.

Sample test(s)

input

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

output

12
16
18

input

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

output

17
22
11

线段树/RMQ

首先复制一次,变环为链。

说说我的线段树做法:

对于一个区间,维护三个值:

ma: “ |____|”的最大值

L_:“|__”的最大值

_R:“__|”的最大值

然后进行区间合并什么的就可以了。

题解给出的是RMQ算法:

如果要求x-y这一段的值:(h[y]*2+d[1]+d[2]+d[3]+...d[y-1])+(h[x]*2-d[1]-d[2]-d[3]-...-d[x-1])

对于第一部分记为A[y],第二部分记为B[x],只要用RMQ求区间最大的A[]和B[]相加即可(可以证明顺序一定不会反过来)

我的线段树代码(一开始数组开到20w会RE...)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define LL long long
#define M 2000000+5
using namespace std;
struct data
{
	int l,r;
	LL ma,l_,_r;
}a[M*2];
int n,m;
LL h[M],s[M],d[M],inf;
data Push_up(data a,data b)
{
	data c;
	c.l=a.l,c.r=b.r;
	c.ma=max(a.l_+b._r+d[a.r],max(a.ma,b.ma));
	c.l_=max(b.l_,a.l_+s[b.r-1]-s[a.r-1]);
	c._r=max(a._r,b._r+s[a.r]-s[a.l-1]);
	return c;
}
void Build(int x,int l,int r)
{
	a[x].l=l,a[x].r=r;
	if (l==r)
	{
		a[x].ma=-inf;
		a[x].l_=a[x]._r=h[l];
		return;
	}
	int m=(l+r)>>1;
	Build(x<<1,l,m);
	Build((x<<1)+1,m+1,r);
	a[x]=Push_up(a[x<<1],a[(x<<1)+1]);
}
data Get(int x,int l,int r)
{
	if (l<=a[x].l&&r>=a[x].r) return a[x];
	int m=(a[x].l+a[x].r)>>1;
	if (r<=m)
		return Get(x<<1,l,r);
	if (l>m)
		return Get((x<<1)+1,l,r);
	return Push_up(Get(x<<1,l,r),Get((x<<1)+1,l,r));
}
int main()
{
	inf=(LL)1e18;
        scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
		cin>>d[i],d[n+i]=d[i];
	for (int i=1;i<=n*2;i++)
		s[i]=s[i-1]+d[i];
	for (int i=1;i<=n;i++)
		cin>>h[i],h[i]*=2LL,h[n+i]=h[i];
	Build(1,1,n*2);
	for (int i=1;i<=m;i++)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		if (l<=r) cout<<Get(1,r+1,l-1+n).ma<<endl;
		else cout<<Get(1,r+1,l-1).ma<<endl;
	}
	return 0;
}

感悟:

定式思维太可怕。。。

时间: 2024-08-05 08:27:16

【codeforces #292(div 1)】ABC题解的相关文章

Codeforces Round #312 (Div. 2) ABC题解

[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地.拉拉土地是一个非常美丽的国家,位于坐标线.拉拉土地是与著名的苹果树越来越随处可见. 拉拉土地恰好n苹果树.树数i位于位置xi和具有人工智能的苹果就可以了增长.阿姆鲁希望从苹果树收集苹果. AMR目前维持在X =0的位置.在开始的时候,他可以选择是否去左边或右边.他会在他的方向继续下去,直到他遇见一棵苹果树,他之前没有参观.他会采取所有的苹果,然后扭转他的方向,继续走这

Codeforces Round #247 (Div. 2) ABC

Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431 代码均已投放:https://github.com/illuz/WayToACM/tree/master/CodeForces/431 A - Black Square 题目地址 题意: Jury玩别踩白块,游戏中有四个区域,Jury点每个区域要消耗ai的卡路里,给出踩白块的序列,问要消耗多少卡路里. 分析: 模拟水题.. 代码: /* * Author: illuz

# Codeforces Round #529(Div.3)个人题解

Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Repeating Cipher 传送门 题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么 题解:1.2.3.4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可 代码: #include<bits/stdc++.h> using namespace s

Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓扑排序)

题目地址:codeforces 292 B 用队列维护度数为1的点,也就是可以唯一确定的点,然后每次找v1,v2,并用v2来更新与之相连的点,如果更新后的点度数为1,就加入队列.若最后还有为"."的,说明无解或解不唯一. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #i

#292 (div.2) D.Drazil and Tiles (贪心+bfs)

Description Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tiles should

CF#247(Div. 2)部分题解

引言: 在软件项目中,Maven提供了一体化的类库管理系统,非常实用.但是,如果新增的类库jar在网络上无法获取到,如何在本地按照Maven的规则添加进来呢?本文将通过一个小例子展示新增过程. 背景介绍: 一个Maven管理的Java项目,提供一个系统级别的POM.xml,其中定义了整个项目使用的类库. 需求: 需要添加一个自定义的类库到当前项目中.假定当前的类库文件名为:abc.jar.. 如何将类库添加进来? 1.  找到当前Maven的Repository类库位置 一般默认情况下,在win

TopCoder SRM 634 Div.2[ABC]

TopCoder SRM 634 Div.2[ABC] ACM 题目地址: TopCoder SRM 634 赛后做的,感觉现场肯定做不出来Orz,简直不能多说. Level One-MountainRanges[水题] 题意: 问序列中有几个完全大于旁边的峰. 分析: 傻逼题,不多说. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: one.cpp * Create Date: 2014-09-26 21:01:23 * Desc

Codeforces #258 Div.2 E Devu and Flowers

大致题意: 从n个盒子里面取出s多花,每个盒子里面的花都相同,并且每个盒子里面花的多数为f[i],求取法总数. 解题思路: 我们知道如果n个盒子里面花的数量无限,那么取法总数为:C(s+n-1, n-1) = C(s+n-1, s). 可以将问题抽象成:x1+x2+...+xn = s, 其中0<=xi <= f[i],求满足条件的解的个数. 两种方法可以解决这个问题: 方法一:这个问题的解可以等价于:mul = (1+x+x^2+...+x^f[1])*(1+x+x^2+...+x^f[2]

Codeforces A. Valera and X 题解

判断二维字符串是否满足下面条件: on both diagonals of the square paper all letters are the same; all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. Help Valera, write the progra