Codeforces goodbye 2014 b

/**
 * @brief good bye 2014 b
 * @file b.cpp
 * @author mianma
 * @created 2014/01/06  10:20
 * @edited  2014/01/06  10:20
 * @type dfs greedy 
 * @note
 */
#include <fstream>
#include <iostream>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>

using namespace std;

#define max(a, b)  ((a) > (b) ? (a) : (b))
#define min(a, b)  ((a) > (b) ? (b) : (a)) 
#define abs(a)     ((a) >  0  ? (a) : (0 - (a)))
#define CLR(vec)   memset(vec, 0, sizeof(vec))

#ifdef DEBUG
ifstream in;
ofstream out;
#define CIN in
#define COUT out
#else
#define CIN cin
#define COUT cout
#endif

#define MAXN 320

int n;
int table[MAXN];
int visit[MAXN];
vector<int> store;
vector<int> record[MAXN];

set<int> dfs(int root){
    int pos = root;
    set<int> ret;
    if(visit[pos])
        return ret;
    stack<int> st;
    st.push(pos);
    while(!st.empty()){
        int pos = st.top();
        st.pop();
        ret.insert(pos);
        visit[pos] = 1;
        for(int i = 0; i < record[pos].size(); i++){
            if(visit[record[pos][i]])
                continue;
            st.push(record[pos][i]);
        }
    }
    return ret;
}

int main(void){
    ios_base::sync_with_stdio(0);
#ifdef DEBUG
    CIN.open("./in",  ios::in);
    COUT.open("./out",  ios::out);
#endif
    CIN >> n;
    for(int i = 1; i <= n; i++)
        CIN >> table[i];
    
    char ch;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++){
            CIN >> ch;
            if(‘1‘ == ch)
                record[i].push_back(j);
        }
    for(int i = 1; i <= n; i++){
    store.clear();
        set<int> st = dfs(i);
    if(st.empty())
        continue;
        for(set<int>::iterator iter = st.begin(); iter != st.end(); iter++)
                    store.push_back(table[*iter]);
    sort(store.begin(), store.end());
        set<int>::iterator iter = st.begin();
        for(int i = 0; i < store.size(); i++, iter++)
                table[*iter] = store[i];
    }
    for(int i = 1; i <=n; i++)
        COUT << table[i] << (i == n ? "\n": " ");
        return 0;
}
时间: 2024-08-01 17:03:38

Codeforces goodbye 2014 b的相关文章

Codeforces goodbye 2014 c

/**  * @brief good bye 2014 c  * @file c.cpp  * @author mianma  * @created 2014/01/06  16:42  * @edited  2014/01/06  16:42  * @type game  * @note   */ #include <fstream> #include <iostream> #include <cstring> #include <vector> #inc

Codeforces Goodbye 2018

真是可怕...家里设备差的我无法想象...用txt打完的全场比赛[无奈] 先%下镇海ljz大佬和杭二lqz大佬 tql A题 签到题吧... 题面那么长,说到底就是求一个最大的 n + (n + 1) + (n + 2)的和 #include <bits/stdc++.h> using namespace std; int a,b,c; int main(){ scanf("%d%d%d",&a,&b,&c); b--;c-=2; int ans=m

codeforces 391E2 (【Codeforces Rockethon 2014】E2)

/* 题意:有三棵树,每颗树有ni个结点,添加两条边把这三棵树连接起来,合并成一棵树,使得树中任意两点之间的最短路径 的和最大. 分析: 三棵树要合并成一棵树,则第一棵树必须选择一个点,假设为X,第二棵树必须选择两个点,假设为Y1, Y2,第三棵树必须选择一个点,假设为Z 记第一棵树中所有结点到X的路径总和为:tot1 第二棵树中所有结点到Y1,Y2的路径总和分别为:tot2, tot3 第三棵树中所有结点到Z的路径总和为:tot4; 共有四种情况: 1,每棵树内部的结点之间的距离为常数,可以求

CodeForces Goodbye 2017

传送门 A - New Year and Counting Cards •题意 有n张牌,正面有字母,反面有数字 其中元音字母$a,e,o,i,u$对应 原文地址:https://www.cnblogs.com/MMMinoz/p/11616206.html

ZOJ 3650(多米诺骨牌 dp + 线段树优化)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3650 题意: 给你n个骨牌,每个骨牌有一个在x轴上的位置和高度,每个骨牌可以想左推也可以向右推,问最少多少步可以把骨牌全部推倒. 思路: 之前Goodbye 2014 上的E题就是一道多米诺骨牌的题目,虽然跟这道题目不太一样但是还是提供了一下思路, 附题解的链接: http://blog.csdn.net/u013649253/article/details/4

Codeforces Zepto Code Rush 2014 Feed with Candy 贪心

A. Feed with Candy time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is

[Codeforces]849E Goodbye Souvenir

又是一道比较新的模板题吧,即使是在Codeforces上小C还是贴了出来. Description 给定一个长度为n的序列a1~an,每个元素代表一种颜色.m次操作,每次操作为两种中的一种: 1 p x:将第p个位置上的颜色修改为x: 2 l r:询问[l,r]区间,求该区间内的每种颜色的“最大出现位置-最小出现位置”之和. Input 第一行两个正整数n.m: 第二行n个整数,表示a1~an: 接下来m行,每行表示一个如题所示的操作. Output 对于每个操作2,输出题目所求的答案. Sam

【Codeforces 848C】Goodbye Souvenir

Codeforces 848 C 题意:给\(n\)个数,\(m\)个询问,每一个询问有以下类型: 1 p x:将第p位改成x. 2 l r:求出\([l,r]\)区间中每一个出现的数的最后一次出现位置-第一次出现位置的和. 思路:我比较愚钝,只会最菜的\(O(n\times sqrt(n)\times log(n))\)的做法. 首先我们来想查询操作.我们将原序列分成B个一段,其中B是自己指定的.然后我们维护好所有的从第\(i\times B\)个到第\(j\times B-1\)个的数中的答

CodeForces Good Bye 2014 B. New Year Permutation

可能是因为这次没有分Div.1和Div.2,所以感觉题的难度比较大. 题意: 给出一个1~n的排列和一个邻接矩阵A,Aij = 1表示可以交换排列的第i项和第j项,问经过若干次交换后,求能够得到最小字典序的排列. 分析: 如果a和b可交换,b和c可交换,则a和c也可以交换位置.如果把这n个位置看做顶点,两个可交换的位置连一条边,则图中在同一连通分量的顶点都是可以交换元素的.所以用并查集做就很方便了. 要想得到字典序最小的排列,直接贪心就可以了.从第一个数开始,首先试试1能不能交换到第一个位置去,