↓ Skip to main content
  1. Posts/

codeforces 500 B. New Year Permutation

·499 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://codeforces.com/contest/500/problem/B

题意:给定一个1至n的数的一种排列。给定一个n*n的矩阵,a[i][j]==0代表pi,pj不可以交换,a[i][j]为1代表p[i],p[j]可以交换。 问字典序最小的排列。。

思路:把矩阵看成图的关系。。反正n很小。。跑一遍floyd..得到可以间接交换的点。。然后冒泡排序就好。。只有p[i]>p[j]并且a[i][j]的时候交换。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月05日 星期六 15时00分14秒
 4File Name :code/cf/problem/500B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=305;
34char ok[N][N];
35int p[N];
36int n;
37int a[N][N];
38void print()
39{
40    for ( int i = 0 ; i < n ;i++) printf("%d ",p[i]);
41}
42void floyd()
43{
44    for ( int k = 0 ; k < n ;k ++)
45	for ( int i = 0 ; i < n ; i++)
46	    for ( int j = 0 ; j < n ; j++)
47		if (a[i][j]==-1&&a[i][k]==1&&a[k][j]==1)
48		    a[i][j] = 1;
49}
50int main()
51{
52	#ifndef  ONLINE_JUDGE
53	freopen("code/in.txt","r",stdin);
54  #endif
55	scanf("%d",&n);
56	ms(a,-1);
57	for ( int i = 0 ; i < n ; i++) scanf("%d",&p[i]);
58	for ( int i = 0 ; i < n ; i++) scanf("%s",ok[i]);
59
60	for ( int i = 0 ; i < n ; i ++)
61	{
62	    for ( int j = 0 ; j < n ; j++)
63	    {
64		if (ok[i][j]=='1')
65		{
66		    a[i][j] = 1;
67		//    a[j][i] = 1;
68		}
69	    }
70	}
71	floyd();
72	for ( int i = 0 ; i < n-1 ; i++)
73	    for ( int j = i+1 ; j < n ;j++)
74		if (p[i]>p[j]&&a[i][j]==1)
75		{
76		    swap(p[i],p[j]);
77		}
78	print();
79
80  #ifndef ONLINE_JUDGE
81  fclose(stdin);
82  #endif
83    return 0;
84}

Related

POJ 2253 - Frogger (floyd)

·622 words·2 mins
A - Frogger **Time Limit:**1000MS **Memory Limit:**65536KB 64bit IO Format:%I64d & %I64u Submit Status Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping. Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps. To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence. The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.

codeforces 377 A maze

·637 words·2 mins
http://codeforces.com/contest/377/problem/A 题意:给定一个n*m的maze. ‘.’代表空,‘#’代表墙。要求构造一种方案,使得将k个空格填成墙壁后不影响当前的连通性(即没有被填充的空格之间可以相互到达) 思路:一开始想从上往下从左往右构造。错误的认为四个角一定是可以变成墙的。

codeforces 129 B. Students and Shoelaces

·549 words·2 mins
http://codeforces.com/contest/129/problem/B 题意:n个点。m条边。每一次会将图中度为1的点加入到等待队列中。然后一起删掉,记为一次操作。当删掉一个点的时候,与它相连的边也全部删掉。问一共做进行多少次操作。使得图中不再有度为1的点。 思路:重点是用开一个数组deg[i]记录点i的度。这样比用.size()高明太多。。因为我们并不需要知道具体删了哪条边。我们只要知道与点i相连的点的边数因为点i被删除而减少了1.

codeforces 580 C. Kefa and Park

·394 words·1 min
http://codeforces.com/contest/580/problem/C 题意:给出一棵树。每个叶子节点上有一个饭店。某些节点上有cat.现在问从根节点出发可以到达多少个饭店,保证在到达饭店的路径中补连续遇到m个以上的cat.

codeforces 115A A. Party

·356 words·1 min
http://codeforces.com/problemset/problem/115/A 题意:给出n个人之间的上级下级关系。问如何分得最少的组,使得没一组中的人不存在上下级关系。 思路:用树的观点来考虑会很容易。可以看成给了一棵森冷。对于不同的树的相同层的点,不存在上下级关系,可以放在一个group.对于同一棵树,每一层要单独放一个group.所以答案是所有树的深度的最大值。