|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
|
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
|
|
|
#include <bits/stdc++.h>
|
|
|
using namespace std;
|
|
|
#define CRP(t, x) const t &x
|
|
|
#define OPL(t, x) bool operator<(CRP(t, x)) const
|
|
|
#define FIL(x, v) memset(x, v, sizeof(x))
|
|
|
#define CLR(x) FIL(x, 0)
|
|
|
#define NE1(x) FIL(x, -1)
|
|
|
#define INF(x) FIL(x, 0x3f)
|
|
|
typedef long long ll;
|
|
|
const int maxn = 1e4;
|
|
|
const int INF = 0x7fffffff;
|
|
|
const int N = 5e3 + 50, M = N * N;
|
|
|
struct Edge
|
|
|
{
|
|
|
int nxt, to, len, cap;
|
|
|
} E[M];
|
|
|
int adj[N], ecnt;
|
|
|
inline void _int_addEdge(int f, int t, int l, int c)
|
|
|
{
|
|
|
E[ecnt] = {adj[f], t, l, c};
|
|
|
adj[f] = ecnt++;
|
|
|
}
|
|
|
void printTime()
|
|
|
{
|
|
|
static auto base = chrono::high_resolution_clock::now().time_since_epoch().count();
|
|
|
auto ne = chrono::high_resolution_clock::now().time_since_epoch().count();
|
|
|
printf("%lf\n", (ne - base) / 1e9);
|
|
|
base = ne;
|
|
|
}
|
|
|
void printTime2()
|
|
|
{
|
|
|
return;
|
|
|
static auto base = chrono::high_resolution_clock::now().time_since_epoch().count();
|
|
|
static int cnt = 0;
|
|
|
auto ne = chrono::high_resolution_clock::now().time_since_epoch().count();
|
|
|
if (cnt++ % 10000 == 0) printf("10000 loops using %lf\n", (ne - base) / 1e9), base = ne;
|
|
|
}
|
|
|
inline int read_int()
|
|
|
{
|
|
|
char c;
|
|
|
int ret = 0, sgn = 1;
|
|
|
do
|
|
|
{
|
|
|
c = getchar();
|
|
|
} while ((c < '0' || c > '9') && c != '-');
|
|
|
if (c == '-')
|
|
|
sgn = -1;
|
|
|
else
|
|
|
ret = c - '0';
|
|
|
while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0');
|
|
|
return sgn * ret;
|
|
|
}
|
|
|
struct Min_Cost_Max_Flow
|
|
|
{
|
|
|
struct edge
|
|
|
{
|
|
|
int to, capacity, cost, rev;
|
|
|
edge() {}
|
|
|
edge(int to, int _capacity, int _cost, int _rev) : to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
|
|
|
};
|
|
|
int V, H[maxn + 5], dis[maxn + 5], PreV[maxn + 5], PreE[maxn + 5];
|
|
|
vector<edge> G[maxn + 5];
|
|
|
//µ÷ÓÃǰ³õʼ»¯
|
|
|
void Init(int n)
|
|
|
{
|
|
|
V = n;
|
|
|
for (int i = 0; i <= V; ++i) G[i].clear();
|
|
|
}
|
|
|
//¼Ó±ß
|
|
|
void Add_Edge(int from, int to, int cap, int cost)
|
|
|
{
|
|
|
G[from].push_back(edge(to, cap, cost, G[to].size()));
|
|
|
G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
|
|
|
}
|
|
|
//flowÊÇ×Ô¼º´«½øÈ¥µÄ±äÁ¿£¬¾ÍÊÇ×îºóµÄ×î´óÁ÷£¬·µ»ØµÄÊÇ×îС·ÑÓÃ
|
|
|
int Min_cost_max_flow(int s, int t, int f, int &flow)
|
|
|
{
|
|
|
int res = 0;
|
|
|
fill(H, H + 1 + V, 0);
|
|
|
while (f)
|
|
|
{
|
|
|
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
|
|
|
fill(dis, dis + 1 + V, INF);
|
|
|
dis[s] = 0;
|
|
|
q.push(pair<int, int>(0, s));
|
|
|
int cnt = 0, cnt2 = 0;
|
|
|
printTime();
|
|
|
while (!q.empty())
|
|
|
{
|
|
|
pair<int, int> now = q.top();
|
|
|
q.pop();
|
|
|
int v = now.second;
|
|
|
if (dis[v] < now.first) continue;
|
|
|
for (int kk = 1; kk; kk--)
|
|
|
{
|
|
|
for (int i = G[v].size() - 1; i >= 0; --i)
|
|
|
cnt2 -= G[v][i].cost;
|
|
|
for (int e = adj[v]; ~e; e = E[e].nxt)
|
|
|
cnt2 += E[e].len;
|
|
|
}
|
|
|
for (int i = G[v].size() - 1; i >= 0; --i)
|
|
|
{
|
|
|
edge &e = G[v][i];
|
|
|
if (e.capacity && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to])
|
|
|
{
|
|
|
printTime2();
|
|
|
cnt++;
|
|
|
q.push({dis[e.to] = dis[v] + e.cost + H[v] - H[e.to], e.to});
|
|
|
PreV[e.to] = v;
|
|
|
PreE[e.to] = i;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
printf("- %d %d\n", cnt, cnt2);
|
|
|
printTime();
|
|
|
putchar('\n');
|
|
|
if (dis[t] == INF) break;
|
|
|
for (int i = 0; i <= V; ++i) H[i] += dis[i];
|
|
|
int d = f;
|
|
|
for (int v = t; v != s; v = PreV[v]) d = min(d, G[PreV[v]][PreE[v]].capacity);
|
|
|
f -= d;
|
|
|
flow += d;
|
|
|
res += d * H[t];
|
|
|
for (int v = t; v != s; v = PreV[v])
|
|
|
{
|
|
|
edge &e = G[PreV[v]][PreE[v]];
|
|
|
e.capacity -= d;
|
|
|
G[v][e.rev].capacity += d;
|
|
|
}
|
|
|
}
|
|
|
return res;
|
|
|
}
|
|
|
};
|
|
|
int n, k, s1, s2, t, flow, arr[maxn + 5];
|
|
|
Min_Cost_Max_Flow MCMF;
|
|
|
int T;
|
|
|
int countEdges(int u)
|
|
|
{
|
|
|
return MCMF.G[u].size();
|
|
|
}
|
|
|
inline void addEdge(int f, int t, int l, int c)
|
|
|
{
|
|
|
MCMF.Add_Edge(f, t, c, l);
|
|
|
_int_addEdge(f, t, l, c);
|
|
|
_int_addEdge(t, f, -l, 0);
|
|
|
}
|
|
|
int main()
|
|
|
{
|
|
|
#ifdef _MSC_VER
|
|
|
freopen(R"(E:\UserData\Desktop\1009\multi.in)", "r", stdin);
|
|
|
#endif // _MSC_VER
|
|
|
int T = read_int(), n, k;
|
|
|
while (T--)
|
|
|
{
|
|
|
NE1(adj), ecnt = 0;
|
|
|
ll ans = 0;
|
|
|
n = read_int(), k = read_int(), ::T = (n + 1) << 1 | 1;
|
|
|
int s2 = (n + 1) << 1;
|
|
|
MCMF.Init(::T);
|
|
|
addEdge(0, s2, 0, k);
|
|
|
for (int i = 1; i <= n; i++)
|
|
|
{
|
|
|
arr[i] = read_int();
|
|
|
addEdge(s2, i, 0, 1);
|
|
|
addEdge(i << 1, i << 1 | 1, -arr[i], 1);
|
|
|
addEdge(i << 1 | 1, ::T, 0, 1);
|
|
|
}
|
|
|
for (int i = 1; i <= n; i++)
|
|
|
for (int j = 1; j < i; j++)
|
|
|
if (arr[j] <= arr[i])
|
|
|
addEdge(j << 1 | 1, i << 1, 0, 1);
|
|
|
printTime();
|
|
|
int cnt = 0;
|
|
|
for (int k = 0; k < 100000000; k++)
|
|
|
for (int i = 0; i <= T; i++)
|
|
|
for (int e = 0; e < MCMF.G[i].size(); e++)
|
|
|
cnt += MCMF.G[i][e].to;
|
|
|
printTime2();
|
|
|
printTime();
|
|
|
printf("[%d]\n", cnt);
|
|
|
puts("---");
|
|
|
printf("%d\n", -MCMF.Min_cost_max_flow(0, ::T, INF, flow));
|
|
|
printTime();
|
|
|
}
|
|
|
return 0;
|
|
|
} |