@zzzc18
2017-07-16T21:38:40.000000Z
字数 3824
阅读 1230
AtCoder
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.
2 2
8
1 8
0
3 2
12
100000 100000
530123477
对于n和m相差大于等于二的,不考虑,直接输出0
当n和m相等时,有种情况(y因为奇数位放猴与偶数位放猴时两种等值但不同的情况)
当n和m相差1时,只能狗和猴插空站,整体上就一种形态,如例3,只能NMNMN这样站,共种情况
注意计算过程中取模,要开long long
#include<cstdio>
#include<cmath>
#include<algorithm>
#define LL long long
#define MOD 1000000007
using 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;
}
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?
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
由于边权只与x的差值和y的差值有关,可以分别考虑走x和走y
如果按x值排序,显然若前三个点为A,B,C,我们一定会连A->B->C而不是A->C->B或其他排列,所以我们只连和的边,边权为
再按y来排序,方法同上;
总共只连了条边然后Kruskal求解
我感觉还是官方讲得好
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 difference 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 difference 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 100009
using 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;
}