#include <iostream>
#include <stdio.h>
#include <vector>
#include <climits>
#include <string>
#include <tuple>
#include <queue>
#include <algorithm>
using namespace std;
typedef tuple<int, int, int> row;
bool is_finished(const vector<int> &v)
{
for (size_t i = 0; i < v.size(); ++i)
{
if (v[i] != 2)
{
return false;
}
}
return true;
}
bool del(vector<int> &cups, int i, int j)
{
for (int pos = i; pos <= j; ++pos)
{
if (cups[pos] == 1)
return false;
}
return true;
}
void doit()
{
size_t n;
cin >> n;
vector<row> v;
vector<int> cups;
int result = 0;
int c;
cups.resize(n+1);
size_t count = 0;
for (size_t i = 1; i <= n; ++i)
{
cups[i] = 0;
}
for (size_t i = 1; i <= n; ++i)
{
for (size_t j = i; j <=n; ++j)
{
cin >> c;
v.push_back(make_tuple(-c, i, j));
for (size_t pos = i; pos <= j; ++pos)
{
cups[pos]++;
count++;
}
}
}
sort(v.begin(), v.end());
while (v.size() > n)
{
for (int i = 0; i < v.size(); ++i)
{
if (!del(cups, get<1>(v[i]), get<2>(v[i])))
continue;
else
{
for (int pos = get<1>(v[i]); pos <= get<2>(v[i]); ++pos)
{
cups[pos]--;
}
v.erase(v.begin() + i);
break;
}
}
}
for (size_t pos = 0; pos < v.size(); ++pos)
{
result -= get<0>(v[pos]);
}
cout << result << endl;
}
int main(void)
{
doit();
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <iostream> #include <stdio.h> #include <vector> #include <climits> #include <string> #include <tuple> #include <queue> #include <algorithm> using namespace std; typedef tuple<int, int, int> row; bool is_finished(const vector<int> &v) { for (size_t i = 0; i < v.size(); ++i) { if (v[i] != 2) { return false; } } return true; } bool del(vector<int> &cups, int i, int j) { for (int pos = i; pos <= j; ++pos) { if (cups[pos] == 1) return false; } return true; } void doit() { size_t n; cin >> n; vector<row> v; vector<int> cups; int result = 0; int c; cups.resize(n+1); size_t count = 0; for (size_t i = 1; i <= n; ++i) { cups[i] = 0; } for (size_t i = 1; i <= n; ++i) { for (size_t j = i; j <=n; ++j) { cin >> c; v.push_back(make_tuple(-c, i, j)); for (size_t pos = i; pos <= j; ++pos) { cups[pos]++; count++; } } } sort(v.begin(), v.end()); while (v.size() > n) { for (int i = 0; i < v.size(); ++i) { if (!del(cups, get<1>(v[i]), get<2>(v[i]))) continue; else { for (int pos = get<1>(v[i]); pos <= get<2>(v[i]); ++pos) { cups[pos]--; } v.erase(v.begin() + i); break; } } } for (size_t pos = 0; pos < v.size(); ++pos) { result -= get<0>(v[pos]); } cout << result << endl; } int main(void) { doit(); return 0; } |
English