Codeforces Round #254 (Div. 2)

水题...


A. DZY Loves Chessboard
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

DZY loves chessboard, and he enjoys playing with it.

He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.

You task is to find any suitable placement of chessmen on the given chessboard.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100).

Each of the next n lines contains a string of m characters: the j-th character of the i-th string is either "." or "-". A "." means that the corresponding cell (in the i-th row and the j-th column) is good, while a "-" means it is bad.

Output

Output must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell.

If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.

Sample test(s)
input
1 1
.
output
B
input
2 2
..
..
output
BW
WB
input
3 3
.-.
---
--.
output
B-B
---
--B
Note

In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.

In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.

In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are.


 

import java.util.*;

public class Main
{
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(),m=in.nextInt();
		char[][] mp=new char[n+10][m+10];
		for(int i=0;i<n;i++)
			mp[i]=in.next().toCharArray();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(mp[i][j]=='.')
				{
					if((i+j)%2==0)
						mp[i][j]='B';
					else mp[i][j]='W';
				}
			}
			System.out.println(mp[i]);
		}
	}
}

dfs

B. DZY Loves Chemistry
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

DZY loves chemistry, and he enjoys mixing chemicals.

DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.

Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.

Find the maximum possible danger after pouring all the chemicals one by one in optimal order.

Input

The first line contains two space-separated integers n and m .

Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xiwill react with the chemical yi. Each pair of chemicals will appear at most once in the input.

Consider all the chemicals numbered from 1 to n in some order.

Output

Print a single integer — the maximum possible danger.

Sample test(s)
input
1 0
output
1
input
2 1
1 2
output
2
input
3 2
1 2
2 3
output
4
Note

In the first sample, there's only one way to pour, and the danger won't increase.

In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.

In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring).


import java.util.*;

public class Main
{	
	static int[][] reactor=new int[100][100];
	static boolean[] vis=new boolean[100];
	static int n,m;
	static long ans;
	
	static void dfs(int u)
	{
		vis[u]=true;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]==true||reactor[u][i]==0) continue;
			ans=ans*2;
			dfs(i);
		}
	}
	
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		n=in.nextInt();m=in.nextInt();
		ans=1;
		while(m-->0)
		{
			int a=in.nextInt(),b=in.nextInt();
			reactor[a][b]=reactor[b][a]=1;
		}
		for(int i=1;i<=n;i++)
		{
			if(!vis[i]) dfs(i);
		}
		System.out.println(ans);
	}
}

坑爹贪心....

别人的证明:

here's the proof.

say we have an edge of weight  connecting nodes with values  and  and another edge of weight connecting nodes with values  and .

  • let us assume that  and also  (i.e. taking both edges together will lead to better density than taking either single edge).
  • cross multiplying the two gives  and .
  • adding  to both sides of first equation gives .
  • combining second and third equations, we get , which means that .

this is a contradiction because problem statement says that all node values and edge weights are positive. so our assumption was wrong, i.e. single edge is always the right answer.


C. DZY Loves Physics
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

DZY loves Physics, and he enjoys calculating density.

Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows:

where  v is the sum of the values of the nodes,  e is the sum of the values of the edges.

Once DZY got a graph G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G' is as large as possible.

An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies:

  • ;
  • edge  if and only if , and edge ;
  • the value of an edge in G' is the same as the value of the corresponding edge in G, so as the value of a node.

Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected.

Input

The first line contains two space-separated integers n (1 ≤ n ≤ 500). Integer n represents the number of nodes of the graph Gm represents the number of edges.

The second line contains n space-separated integers xi (1 ≤ xi ≤ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n.

Each of the next m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 103), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges.

Output

Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9.

Sample test(s)
input
1 0
1
output
0.000000000000000
input
2 1
1 2
1 2 1
output
3.000000000000000
input
5 6
13 56 73 98 17
1 2 56
1 3 29
1 4 42
2 3 95
2 4 88
3 4 63
output
2.965517241379311
Note

In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1.

In the second sample, choosing the whole graph is optimal.


import java.util.*;
import java.math.*;

public class Main
{
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(),m=in.nextInt();
		double ans=0.;
		double[] v=new double[n+10];
		for(int i=1;i<=n;i++)
		{
			v[i]=in.nextDouble();
		}
		for(int i=0;i<m;i++)
		{
			int a=in.nextInt(),b=in.nextInt();
			double c=in.nextDouble();
			ans=Math.max(ans,(v[a]+v[b])/c);
		}
		System.out.print(ans);
	}
}


随机化

A,B都是随机生成的,c[i]=max(....)有很大概率是靠近n的,设一个界限s,逐个枚举n-s~n之间的数并检查可能性,如果找不到那就暴力计算.



D. DZY Loves FFT
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

DZY loves Fast Fourier Transformation, and he enjoys using it.

Fast Fourier Transformation is an algorithm used to calculate convolution. Specifically, if ab and c are sequences with length n, which are indexed from 0 to n - 1, and

We can calculate c fast using Fast Fourier Transformation.

DZY made a little change on this formula. Now

To make things easier, a is a permutation of integers from 1 to n, and b is a sequence only containing 0 and 1. Given a and b, DZY needs your help to calculate c.

Because he is naughty, DZY provides a special way to get a and b. What you need is only three integers ndx. After getting them, use the code below to generate a and b.

//x is 64-bit variable;
function getNextX() {
    x = (x * 37 + 10007) % 1000000007;
    return x;
}
function initAB() {
    for(i = 0; i < n; i = i + 1){
        a[i] = i + 1;
    }
    for(i = 0; i < n; i = i + 1){
        swap(a[i], a[getNextX() % (i + 1)]);
    }
    for(i = 0; i < n; i = i + 1){
        if (i < d)
            b[i] = 1;
        else
            b[i] = 0;
    }
    for(i = 0; i < n; i = i + 1){
        swap(b[i], b[getNextX() % (i + 1)]);
    }
}

Operation x % y denotes remainder after division x by y. Function swap(x, y) swaps two values x and y.

Input

The only line of input contains three space-separated integers n, d, x (1 ≤ d ≤ n ≤ 100000; 0 ≤ x ≤ 1000000006). Because DZY is naughty, x can't be equal to 27777500.

Output

Output n lines, the i-th line should contain an integer ci - 1.

Sample test(s)
input
3 1 1
output
1
3
2
input
5 4 2
output
2
2
4
5
5
input
5 4 3
output
5
5
5
5
4
Note

In the first sample, a is [1 3 2]b is [1 0 0], so c0 = max(1·1) = 1c1 = max(1·0, 3·1) = 3c2 = max(1·0, 3·0, 2·1) = 2.

In the second sample, a is [2 1 4 5 3]b is [1 1 1 0 1].

In the third sample, a is [5 2 1 4 3]b is [1 1 1 1 0].


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=1100000;

typedef long long int LL;

int a[maxn],b[maxn],q[maxn],to[maxn];
int n,d;
LL x;

int getNextX()
{
    x = (x * 37 + 10007) % 1000000007;
    return x;
}
void initAB()
{
    int i;
    for(i = 0; i < n; i = i + 1)
    {
        a[i] = i + 1;
    }
    for(i = 0; i < n; i = i + 1)
    {
        swap(a[i], a[getNextX() % (i + 1)]);
    }
    for(i = 0; i < n; i = i + 1)
    {
        if (i < d)
            b[i] = 1;
        else
            b[i] = 0;
    }
    for(i = 0; i < n; i = i + 1)
    {
        swap(b[i], b[getNextX() % (i + 1)]);
    }
}

int main()
{
    scanf("%d%d%I64d",&n,&d,&x);
    initAB();
    for(int i=0;i<n;i++)
       if(b[i]) q[++q[0]]=i;
    for(int i=0;i<n;i++)
        to[a[i]]=i;
    int s=30;
    for(int i=0;i<n;i++)
    {
        int j,flag=0;
        for(j=n;j>=n-s;j--)
        {
            if(j>0&&to[j]<=i&&b[i-to[j]])
            {
                printf("%d\n",j);
                flag=1;
                break;
            }
        }
        if(flag) continue;
        int ans=0;
        for(j=1;j<=q[0]&&q[j]<=i;j++)
        {
            ans=max(ans,a[i-q[j]]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

线段树延迟更新....

C. DZY Loves Colors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

DZY loves colors, and he enjoys painting.

On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of the i-th unit of the ribbon is i at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first.

DZY loves painting, we know. He takes up a paintbrush with color x and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit i currently is y. When it is painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit increases by |x - y|.

DZY wants to perform m operations, each operation can be one of the following:

  1. Paint all the units with numbers between l and r (both inclusive) with color x.
  2. Ask the sum of colorfulness of the units between l and r (both inclusive).

Can you help DZY?

Input

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105).

Each of the next m lines begins with a integer type (1 ≤ type ≤ 2), which represents the type of this operation.

If type = 1, there will be 3 more integers l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 108) in this line, describing an operation 1.

If type = 2, there will be 2 more integers l, r (1 ≤ l ≤ r ≤ n) in this line, describing an operation 2.

Output

For each operation 2, print a line containing the answer — sum of colorfulness.

Sample test(s)
input
3 3
1 1 2 4
1 2 3 5
2 1 3
output
8
input
3 4
1 1 3 4
2 1 1
2 2 2
2 3 3
output
3
2
1
input
10 6
1 1 5 3
1 2 7 9
1 10 10 11
1 3 8 12
1 1 10 3
2 1 10
output
129
Note

In the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].

After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].

After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].

So the answer to the only operation of type 2 is 8.



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

typedef  long long int LL;

const int maxn=210000;

LL color[maxn<<2],add[maxn<<2],sum[maxn<<2];

void pushup(int rt)
{
    color[rt]=(color[rt<<1]==color[rt<<1|1])?color[rt<<1]:0;
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void pushdown(int rt,int m)
{
    if(add[rt])
    {
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        sum[rt<<1]+=add[rt]*(m-(m>>1));
        sum[rt<<1|1]+=add[rt]*(m>>1);
        add[rt]=0;
        color[rt<<1]=color[rt<<1|1]=color[rt];
        color[rt]=0;
    }
}

void build(int l,int r,int rt)
{
    if(l==r)
    {
        color[rt]=l;
        return ;
    }
    int m=(l+r)/2;
    build(lson); build(rson);
    pushup(rt);
}

void update(int L,int R,int x,int l,int r,int rt)
{
    if(L<=l&&r<=R&&color[rt])
    {
        sum[rt]+=abs(x-color[rt])*(r-l+1);
        add[rt]+=abs(x-color[rt]);
        color[rt]=x;
        return ;
    }
    pushdown(rt,r-l+1);
    int m=(l+r)/2;
    if(m>=L)
        update(L,R,x,lson);
    if(m<R)
        update(L,R,x,rson);
    pushup(rt);
}

LL query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return sum[rt];
    }
    pushdown(rt,r-l+1);
    int m=(l+r)/2;
    LL ret=0;
    if(m>=L)
        ret+=query(L,R,lson);
    if(m<R)
        ret+=query(L,R,rson);
    return ret;
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    build(1,n,1);
    while(m--)
    {
        int a,b,c,d;
        scanf("%d",&a);
        if(a==1)
        {
            scanf("%d%d%d",&b,&c,&d);
            update(b,c,d,1,n,1);
        }
        else if(a==2)
        {
            scanf("%d%d",&c,&d);
            printf("%I64d\n",query(c,d,1,n,1));
        }
    }
    return 0;
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值