結果

問題 No.270 next_permutation (1)
ユーザー pekempey
提出日時 2015-08-22 00:05:45
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 2,271 bytes
コンパイル時間 1,707 ms
コンパイル使用メモリ 163,140 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-18 02:07:56
合計ジャッジ時間 2,939 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
#define rep(i, a) rep2 (i, 0, a)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) repr2 (i, 0, a)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define asn(a, b, c) fill_n(&(b), sizeof(a) / sizeof(b), c)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
template <class BidirectionalIterator, class Compare>
int next_permutation2(BidirectionalIterator first, BidirectionalIterator last, Compare comp)
{
if (first == last)
return false;
BidirectionalIterator i = first;
++i;
if (i == last)
return 0;
i = last;
--i;
for(;;) {
BidirectionalIterator ii = i;
--i;
if (comp(*i, *ii)) {
BidirectionalIterator j = last;
while (!comp(*i, *--j)) {}
std::swap(*i, *j);
std::reverse(ii, last);
return i - first;
}
if (i == first) {
std::reverse(first, last);
return i - first;
}
}
}
template <class BidirectionalIterator>
int next_permutation2(BidirectionalIterator first, BidirectionalIterator last)
{
typedef
typename std::iterator_traits<BidirectionalIterator>::value_type
value_type;
return next_permutation2(first, last, std::less<value_type>());
}
int N, K;
int p[100000], B[100000];
struct BIT {
int size;
vector<ll> bit;
BIT(int size) : size(size), bit(size + 1) {}
void add(int k, ll x) {
while (k <= size) {
bit[k] += x;
k += k & -k;
}
}
ll sum(int k) {
ll res = 0;
while (k > 0) {
res += bit[k];
k -= k & -k;
}
return res;
}
void update(int k, ll x) {
ll g = sum(k) - sum(k - 1);
add(k, x - g);
}
};
int main() {
cin >> N >> K;
rep (i, N) cin >> p[i];
rep (i, N) cin >> B[i];
BIT bit(N * 2);
ll ans = 0;
if (K == 0) {
cout << 0 << endl;
return 0;
}
rep (i, N) {
bit.add(i + 1, abs(B[i] - p[i]));
}
ans += bit.sum(N + 1);
rep (ii, K - 1) {
int k = next_permutation2(p, p + N);
rep2 (i, k, N) {
bit.update(i + 1, abs(B[i] - p[i]));
}
ans += bit.sum(N + 1);
}
cout << ans << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0