提出 #9583217
ソースコード 拡げる
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int N, AB[18][2];
int dp[1 << 18][51];
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N;
rep(i, 0, N) cin >> AB[i][0];
rep(i, 0, N) cin >> AB[i][1];
rep(msk, 0, 1 << N) rep(lst, 0, 51) dp[msk][lst] = inf;
dp[0][0] = 0;
rep(msk, 0, 1 << N) rep(lst, 0, 51) if (dp[msk][lst] != inf) {
int done = 0;
rep(i, 0, N) if (msk & (1 << i)) done++;
rep(i, 0, N) {
if (msk & (1 << i)) continue;
int p = abs(done - i) % 2;
if (AB[i][p] < lst) continue;
int c = 0;
rep(j, 0, i) if (!(msk & (1 << j))) c++;
chmin(dp[msk | (1 << i)][AB[i][p]], dp[msk][lst] + c);
}
}
int ans = inf;
rep(lst, 0, 51) chmin(ans, dp[(1 << N) - 1][lst]);
if (ans == inf) ans = -1;
cout << ans << endl;
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | D - Swap and Flip |
| ユーザ | hamayanhamayan |
| 言語 | C++14 (GCC 5.4.1) |
| 得点 | 700 |
| コード長 | 2108 Byte |
| 結果 | AC |
| 実行時間 | 114 ms |
| メモリ | 52480 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 700 / 700 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, sample-05.txt |
| All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, sample-05.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 01.txt | AC | 1 ms | 256 KiB |
| 02.txt | AC | 1 ms | 512 KiB |
| 03.txt | AC | 10 ms | 8448 KiB |
| 04.txt | AC | 71 ms | 52480 KiB |
| 05.txt | AC | 61 ms | 52480 KiB |
| 06.txt | AC | 69 ms | 52480 KiB |
| 07.txt | AC | 75 ms | 52480 KiB |
| 08.txt | AC | 65 ms | 52480 KiB |
| 09.txt | AC | 67 ms | 52480 KiB |
| 10.txt | AC | 67 ms | 52480 KiB |
| 11.txt | AC | 40 ms | 52480 KiB |
| 12.txt | AC | 61 ms | 52480 KiB |
| 13.txt | AC | 71 ms | 52480 KiB |
| 14.txt | AC | 41 ms | 52480 KiB |
| 15.txt | AC | 56 ms | 52480 KiB |
| 16.txt | AC | 65 ms | 52480 KiB |
| 17.txt | AC | 73 ms | 52480 KiB |
| 18.txt | AC | 73 ms | 52480 KiB |
| 19.txt | AC | 77 ms | 52480 KiB |
| 20.txt | AC | 70 ms | 52480 KiB |
| 21.txt | AC | 60 ms | 52480 KiB |
| 22.txt | AC | 66 ms | 52480 KiB |
| 23.txt | AC | 1 ms | 256 KiB |
| 24.txt | AC | 68 ms | 52480 KiB |
| 25.txt | AC | 79 ms | 52480 KiB |
| 26.txt | AC | 70 ms | 52480 KiB |
| 27.txt | AC | 75 ms | 52480 KiB |
| 28.txt | AC | 67 ms | 52480 KiB |
| 29.txt | AC | 90 ms | 52480 KiB |
| 30.txt | AC | 90 ms | 52480 KiB |
| 31.txt | AC | 69 ms | 52480 KiB |
| 32.txt | AC | 88 ms | 52480 KiB |
| 33.txt | AC | 114 ms | 52480 KiB |
| 34.txt | AC | 96 ms | 52480 KiB |
| 35.txt | AC | 93 ms | 52480 KiB |
| 36.txt | AC | 108 ms | 52480 KiB |
| 37.txt | AC | 100 ms | 52480 KiB |
| 38.txt | AC | 104 ms | 52480 KiB |
| 39.txt | AC | 23 ms | 14592 KiB |
| sample-01.txt | AC | 1 ms | 256 KiB |
| sample-02.txt | AC | 1 ms | 256 KiB |
| sample-03.txt | AC | 1 ms | 256 KiB |
| sample-04.txt | AC | 1 ms | 256 KiB |
| sample-05.txt | AC | 1 ms | 256 KiB |