@zzzc18 2017-07-16T13:38:40.000000Z 字数 3824 阅读 1163

# Regular Contest 076

AtCoder

# Problem_C

## Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 109+7 (since animals cannot understand numbers larger than that). Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

## Constraints

$1\leq N,M \leq 10^5$

2 2

8

1 8

0

3 2

12

100000 100000

530123477

### Solution

#include<cstdio>#include<cmath>#include<algorithm>#define LL long long#define MOD 1000000007using namespace std;LL n,m,ans;LL cal(LL x){    LL i;LL re=1;    for(i=1;i<=x;i++){        re=(re*i)%MOD;    }    return re;}int main(){    scanf("%lld%lld",&n,&m);    if(abs(m-n)>=2){        printf("0\n");        return 0;    }    if(m==n){        LL t=cal(n);        ans=(t*t*2)%MOD;    }    else{        ans=(cal(m)*cal(n))%MOD;    }    printf("%lld\n",ans);    return 0;}

# Problem_D

## Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (xi,yi). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a−c|,|b−d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

## Constraints

$2 \leq N \leq 10^5$
$0 \leq x_i,y_i \leq 10^9$
All input values are integers

3
1 5
3 9
7 8

3

6
8 3
4 9
12 19
18 1
13 5
7 6

8

# Solution

We are asked to compute the minimum spanning tree. Between two points (a,b) and (c,d), instead of adding an edge of cost min(|a−c|,|b−d|), we add two edges: one edge with cost |a−c| and one edge with cost |b−d|. Suppose that there are three points p,q,r, and their x-coordinates satisfy xp < xq < xr. Then, the edge between p and r with cost xr−xp never appear in the MST (it is better to use an edge between p and q with cost xq −xp and an edge between q and r with cost xr −xq). Thus, we only need to consider the following 2(N −1) edges:
• We sort the point by their x-coordinates, and for each adjacent pair of points add an edge between them (the cost is the diﬀerence of their x-coordinates).
• We sort the point by their y-coordinates, and for each adjacent pair of points add an edge between them (the cost is the diﬀerence of their y-coordinates).
We compute the MST of these edges. This can be done in O(NlogN).


#include<cstdio>#include<algorithm>#include<cmath>#define MAXN 100009using namespace std;int n;struct data{    int x,y,id;}a[MAXN];struct E{    int from,to,val,next;}edge[MAXN<<1];int head[MAXN],edge_num;int ans;int fa[MAXN];bool cmp1(const data &q,const data &w){    return q.x<w.x;}bool cmp2(const data &q,const data &w){    return q.y<w.y;}bool cmp3(const E &q,const E &w){    return q.val<w.val;}int Find(int x){    if(fa[x]!=x) fa[x]=Find(fa[x]);    return fa[x];}void addedge(int x,int y,int v){    edge[++edge_num].next=head[x];    edge[edge_num].to=y;    edge[edge_num].from=x;    edge[edge_num].val=v;    head[x]=edge_num;}void Kruskal(){    sort(edge+1,edge+edge_num+1,cmp3);    int i;int cnt=0;    for(i=1;i<=n;i++)        fa[i]=i;    for(i=1;i<=edge_num;i++){        int ff=Find(edge[i].from),tt=Find(edge[i].to);        if(ff!=tt){            fa[ff]=tt;            ans+=edge[i].val;            cnt++;            if(cnt==n-1)                return;        }    }}void solve(){    int i;    sort(a+1,a+n+1,cmp1);    for(i=2;i<=n;i++)        addedge(a[i-1].id,a[i].id,a[i].x-a[i-1].x);    sort(a+1,a+n+1,cmp2);    for(i=2;i<=n;i++)        addedge(a[i-1].id,a[i].id,a[i].y-a[i-1].y);    Kruskal();    printf("%d\n",ans);}int main(){    scanf("%d",&n);    int i;    for(i=1;i<=n;i++)        scanf("%d%d",&a[i].x,&a[i].y),a[i].id=i;    solve();    return 0;}

• 私有
• 公开
• 删除