1. 程式人生 > >Codeforces 500B New Year Permutation Floyd演算法+貪心

Codeforces 500B New Year Permutation Floyd演算法+貪心

題目連結

題意:給出一個數列(n<=300),Permutation a1, a2, …, an is prettier than permutation b1, b2, …, bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, …, ak - 1 = bk - 1 and ak < bk all holds。然後給出一張表,告訴你每一次可將哪兩個元素可以交換。輸出最優排列。


解法:先用floyd求出每個元素經過一定次數的交換可以和哪個元素換位置,然後用貪心求解,保證最左邊的數儘量小。

code:

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0]) #define mem(a,x) memset(a,x,sizeof a) #define ysk(x) (1<<(x)) typedef long long ll; //const int INF = 0x3f3f3f3f ; const int maxn= 300 ; int n,a[maxn+3]; bool dp[maxn+3][maxn+3]; void floyd() { for1(i,n) dp[i][i]=1; for(int k=1;k<=n;k++) { for
(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { dp[j][i]=dp[i][j]= dp[i][j]|| dp[i][k]&&dp[k][j]; } } } } void greedy() { for1(i,n) { int k=i; for(int j=i+1;j<=n;j++) if(a[j]<a[k]&&dp[i][j]) { k=j; } if(k!=i) swap(a[i],a[k]); } for1(i,n) { printf("%d%c",a[i],i==n?'\n':' '); } } int main() { scanf("%d",&n); for1(i,n) scanf("%d",&a[i]); char ch; for1(i,n) for1(j,n) { scanf(" %c",&ch); dp[i][j]= ch=='1'?1:0; } floyd(); greedy(); return 0; }

相關推薦

Codeforces 500B New Year Permutation Floyd演算法+貪心

題目連結 題意:給出一個數列(n<=300),Permutation a1, a2, …, an is prettier than permutation b1, b2, …, bn, if and only if there exists

CodeForces - 500B New Year Permutation floyd+選擇排序

User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation&nb

Codeforces 1091D New Year and the Permutation Concatenation 找規律,數學 B

Codeforces 1091D New Year and the Permutation Concatenation    https://codeforces.com/contest/1091/problem/D 題目:     Let n be an integer. C

Codeforces Round #536 E. Lunar New Year and Red Envelopes /// 貪心 記憶化搜索 multiset取最大項

find 去掉 close 分享 搜索 data- 技術分享 ace using 題目大意: 給定n m k;(1≤n≤1e5, 0≤m≤200, 1≤k≤1e5) 表示n個時間長度內 最多被打擾m次 k個紅包 接下來k行描述紅包 s t d w;(1≤s≤t≤d≤n ,

Codeforces 611D.New Year and Ancient Prophecy (dp + lcp)

ack com names getchar turn 一個數 長度 getc targe 題目鏈接: http://codeforces.com/problemset/problem/611/D 題意: 長為n的只有數字組成的字符串(n<=5000),問能分割成多少組

[Codeforces 750E]New Year and Old Subsequence

ons lin def aws swa rip 我們 can space Description 題庫鏈接 給出一個長度為 \(n\) 的僅包含數字的字符串。 \(q\) 次詢問,每次詢問該串 \([a,b]\) 段內刪去幾個數能夠使其不含 \(2016\) 的子串,但存在

Codeforces 908D New Year and Arbitrary Arrangement(概率DP,邊界條件處理)

sin color 一個 pan ++ urn fin [1] bsp 題目鏈接 Goodbye 2017 Problem D 題意 一個字符串開始,每次有$\frac{pa}{pa+pb}$的概率在後面加一個a,$\frac{pb}{pa+pb}$的概率在後面加一個

Codeforces 620E New Year Tree

LINK 題目大意 給你一棵樹 讓你支援子樹染色,子樹查詢顏色個數,顏色數<=60, 節點數<=4e5 思路 因為顏色數很少,考慮狀態壓縮變成二進位制 然後直接在dfs序上用線段樹維護就可以了 //Author: dream_maker #include<bits/s

codeforces 750A New Year and Hurry

題目描述:   Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, e

CodeForces - 500D New Year Santa Network 樹形dp 記憶化 求期望

New Year is coming in Tree World! In this world, as the name implies, there are ncities connected by n - 1 roads, and for any two disti

codeforces 908A new year and counting cards

這題看著真TM嚇人,其實很假 只要讀懂題,都會做 題意: 如果一個字元是母音字母,則它對應的數字必為0,2,4,6,8(偶數)中的一個,否則為false。就是如果是母音字母,則要判斷,如果是奇數也要判斷(因為如果它的對應字元為母音,那麼則為false)如果是偶數或非子音字母則不用判斷。要保證

Floyd演算法--+貪心演算法

題目大意: 給你n個站,有m條邊,每條邊有一個耗費值。 問你如果A站到B站可通,選一條路,每條可行路徑上的相鄰兩站的耗費值有一個確定的最大值,使得儘量讓這個值最小,輸出。 否則輸出-1.(有向

Codeforces 1091E. New Year and the Acquaintance Estimation 二分+結論

題解: 第一次做這種……題面放個連結讓你自己去看題解的題目…… 連結告訴你的是一個非常有用的結論: 對於從大到小排好序的一些度數 d d

Codeforces 908D New Year and Arbitrary Arrangement

題意: 給定字元’a’出現的概率pa/(pa+pb),’b’出現的概率pb/(pa+pb); 給定一個正整數k,構造一個字串,當出現至少k個子序列”ab”時停止構造; 問子序列”ab”出現次數的期望P/Q,輸出P*(Q的逆元); 注意是子序列不是子串

New Year Permutation(並查集+動態容器)

B. New Year Permutation  time limit per test2 seconds  memory limit per test256 megabytes  inputstandard input  outputstandard output  U

Codeforces-140C】New Year Snowmen(貪心+優先佇列、二分)

As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs:

CodeForces - 140C】New Year Snowmen (貪心

題幹: As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowb

Codeforces Good Bye 2018 D (1091D) New Year and the Permutation Concatenation

題意:給n!個n的排列,按字典序從小到大連成一條序列,例如3的情況為:[1,2,3, 1,3,2, 2,1,3 ,2,3,1 ,3,1,2 ,3,2,1],問其中長度為n,且和為sum=n*(n+1)/2的序列有多少個? 思路(官方題解):我們考慮一下nex

codeforces goodbye 2018 C. New Year and the Sphere Transmission D. New Year and the Permutation Concatenation

這兩題都是打表找的規律,C題發現具有相同最大因子的數字所走的點是一樣的,D題發現排列數和開頭有幾個相同的數字有關,所以用排列數公式算出有幾個這樣的開頭再乘上這種開頭的種類數就可以了 C題 #include<bits/stdc++.h> using namespace std; typed

codeforces#1090 D. New Year and the Permutation Concatenation(打表找規律)

.com 長度 clear name 分享圖片 ++ 全排列 找到 true 題意:給出一個n,生成n的所有全排列,將他們按順序前後拼接在一起組成一個新的序列,問有多少個長度為n的連續的子序列和為(n+1)*n/2 題解:由於只有一個輸入,第一感覺就是打表找規律,雖然表打出