Since topcoder reserves the proprietorship of the problem statement, I will not paste it here, and I do not have time and energy and see any necessity to reinterpret it.
My answer, the proof of the correctness of which is not reliable with the performance of the overall program and some part of it improvable has got no more than 300 points for an earlier version :(
#include <map>
#include <vector>
#include <string>
using namespace std;
class SkipList
{
protected:
vector<int> succq;
vector<int> precnoncached;
vector<int> succcached;
vector< vector<int> > Q;
int n;
int cost;
int stridx, charidx;
public:
void RunMin ()
{
int i, j;
int total = 0;
for (i = 0; i < n; i++)
{
/* net cache gain for element i */
/* apparently succcached is logically unnecessary, it's a remnant */
int imp = (i - succcached[i]) * Q[i][n-i-1]; /* a plus */
/* two minus */
imp -= precnoncached[i];
imp -= succq[i];
/*
* the central point of the problem is the cache gain for each
* element is independent and commutably addible.
*/
if (imp > 0)
{
total += imp;
/* update the stat */
for (j = 0; j < i; j++)
{
succcached[j]++;
}
for (j = i + 1; j < n; j++)
{
precnoncached[j] -= Q[i][n - i - 1] - Q[i][j - i - 1];
}
}
}
cost -= total;
}
void init ()
{
int i, j;
/* elements cached after current */
succcached.resize(n);
/* instances not cached before current occurring after current */
precnoncached.resize(n);
/* instances of elements succeeding current */
succq.resize(n);
cost = 0;
for (i = 0; i < n; i++)
{
succcached[i] = 0;
precnoncached[i] = 0;
for (j = 0; j < i; j++)
{
precnoncached[i] += Q[j][n-j-1] - Q[j][i-j-1];
}
succq[i] = 0;
for (j = i+1; j < n; j++)
{
succq[i] += Q[j][n-j-1];
}
cost += (i + 1) * Q[i][n-i-1];
}
}
bool ParseNext (vector <string> &ops, bool &op, int &qcode)
{
if (stridx >= ops.size())
{
return false;
}
string &str = ops[stridx];
int len = str.size();
char c = str[charidx];
op = c == 'A';
++charidx;
qcode = 0;
while (1)
{
if (charidx >= len)
{
stridx++;
if (stridx >= ops.size())
{
return true;
}
str = ops[stridx];
len = str.size();
charidx = 0;
}
c = str[charidx];
if (c == ' ')
{
++charidx;
if (charidx >= len)
{
stridx++; charidx = 0;
}
return true;
}
qcode *= 10;
qcode += c - '0';
++charidx;
}
}
int minCost(vector <string> ops)
{
int prevadd = -1;
map<int, int> idxmap;
Q.clear();
stridx = 0;
charidx = 0;
while (1)
{
bool op;
int qcode;
if (!ParseNext(ops, op, qcode))
{
break;
}
if (op)
{
idxmap[qcode] = ++prevadd;
Q.resize(prevadd + 1);
for (int t = 0; t < prevadd; t++)
{
Q[t].resize(prevadd - t + 1);
Q[t][prevadd - t] = Q[t][prevadd - t - 1];
}
Q[prevadd].resize(1);
}
else
{ // query d
int d = idxmap[qcode];
if (prevadd + 1 - d > Q[d].size())
{
Q[d].resize(prevadd + 1 - d);
}
Q[d][prevadd - d]++;
}
}
n = Q.size();
init();
RunMin();
return cost;
}
};
void Fuel (int test_idx, vector<string> &ops)
{
char *k_input0[] = {"A1 ","A2 ","A3"," F3"," F3"," F3"};
char *k_input1[] = {"A1 F1 F1 F1 A2 F1 F2 F1 F2 F1" };
char *k_input2[] = {"A1 F1 F1 F1 A2 F1 F2 F1 F2 F2 F2 F2 F2 F2" };
char *k_input3[] = {"A10","000 A","900 A800 F800 A1 ","F1 F1 F1 F1 A2 F1 F1 A3 F1"};
char *k_input4[] = {"A1 A2 A3 A4 A5"};
#define SIZE(k) sizeof(k_input##k)/sizeof(char*)
char ** k_input_map[5] = {k_input0, k_input1, k_input2, k_input3, k_input4};
int size[5] = { SIZE(0), SIZE(1), SIZE(2), SIZE(3), SIZE(4)};
ops.clear();
for (int i = 0; i < size[test_idx]; i++)
{
string s = k_input_map[test_idx][i];
ops.push_back(s);
}
}
#include <iostream>
int main (void)
{
SkipList skipList;
vector<string> ops;
int test_idx = 0;
for (test_idx = 0; test_idx < 5; test_idx++)
{
Fuel(test_idx, ops);
int cost = skipList.minCost(ops);
cout << "test " << test_idx << ", cost = " << cost << "/n";
}
return 0;
}
My answer, the proof of the correctness of which is not reliable with the performance of the overall program and some part of it improvable has got no more than 300 points for an earlier version :(
#include <map>
#include <vector>
#include <string>
using namespace std;
class SkipList
{
protected:
vector<int> succq;
vector<int> precnoncached;
vector<int> succcached;
vector< vector<int> > Q;
int n;
int cost;
int stridx, charidx;
public:
void RunMin ()
{
int i, j;
int total = 0;
for (i = 0; i < n; i++)
{
/* net cache gain for element i */
/* apparently succcached is logically unnecessary, it's a remnant */
int imp = (i - succcached[i]) * Q[i][n-i-1]; /* a plus */
/* two minus */
imp -= precnoncached[i];
imp -= succq[i];
/*
* the central point of the problem is the cache gain for each
* element is independent and commutably addible.
*/
if (imp > 0)
{
total += imp;
/* update the stat */
for (j = 0; j < i; j++)
{
succcached[j]++;
}
for (j = i + 1; j < n; j++)
{
precnoncached[j] -= Q[i][n - i - 1] - Q[i][j - i - 1];
}
}
}
cost -= total;
}
void init ()
{
int i, j;
/* elements cached after current */
succcached.resize(n);
/* instances not cached before current occurring after current */
precnoncached.resize(n);
/* instances of elements succeeding current */
succq.resize(n);
cost = 0;
for (i = 0; i < n; i++)
{
succcached[i] = 0;
precnoncached[i] = 0;
for (j = 0; j < i; j++)
{
precnoncached[i] += Q[j][n-j-1] - Q[j][i-j-1];
}
succq[i] = 0;
for (j = i+1; j < n; j++)
{
succq[i] += Q[j][n-j-1];
}
cost += (i + 1) * Q[i][n-i-1];
}
}
bool ParseNext (vector <string> &ops, bool &op, int &qcode)
{
if (stridx >= ops.size())
{
return false;
}
string &str = ops[stridx];
int len = str.size();
char c = str[charidx];
op = c == 'A';
++charidx;
qcode = 0;
while (1)
{
if (charidx >= len)
{
stridx++;
if (stridx >= ops.size())
{
return true;
}
str = ops[stridx];
len = str.size();
charidx = 0;
}
c = str[charidx];
if (c == ' ')
{
++charidx;
if (charidx >= len)
{
stridx++; charidx = 0;
}
return true;
}
qcode *= 10;
qcode += c - '0';
++charidx;
}
}
int minCost(vector <string> ops)
{
int prevadd = -1;
map<int, int> idxmap;
Q.clear();
stridx = 0;
charidx = 0;
while (1)
{
bool op;
int qcode;
if (!ParseNext(ops, op, qcode))
{
break;
}
if (op)
{
idxmap[qcode] = ++prevadd;
Q.resize(prevadd + 1);
for (int t = 0; t < prevadd; t++)
{
Q[t].resize(prevadd - t + 1);
Q[t][prevadd - t] = Q[t][prevadd - t - 1];
}
Q[prevadd].resize(1);
}
else
{ // query d
int d = idxmap[qcode];
if (prevadd + 1 - d > Q[d].size())
{
Q[d].resize(prevadd + 1 - d);
}
Q[d][prevadd - d]++;
}
}
n = Q.size();
init();
RunMin();
return cost;
}
};
void Fuel (int test_idx, vector<string> &ops)
{
char *k_input0[] = {"A1 ","A2 ","A3"," F3"," F3"," F3"};
char *k_input1[] = {"A1 F1 F1 F1 A2 F1 F2 F1 F2 F1" };
char *k_input2[] = {"A1 F1 F1 F1 A2 F1 F2 F1 F2 F2 F2 F2 F2 F2" };
char *k_input3[] = {"A10","000 A","900 A800 F800 A1 ","F1 F1 F1 F1 A2 F1 F1 A3 F1"};
char *k_input4[] = {"A1 A2 A3 A4 A5"};
#define SIZE(k) sizeof(k_input##k)/sizeof(char*)
char ** k_input_map[5] = {k_input0, k_input1, k_input2, k_input3, k_input4};
int size[5] = { SIZE(0), SIZE(1), SIZE(2), SIZE(3), SIZE(4)};
ops.clear();
for (int i = 0; i < size[test_idx]; i++)
{
string s = k_input_map[test_idx][i];
ops.push_back(s);
}
}
#include <iostream>
int main (void)
{
SkipList skipList;
vector<string> ops;
int test_idx = 0;
for (test_idx = 0; test_idx < 5; test_idx++)
{
Fuel(test_idx, ops);
int cost = skipList.minCost(ops);
cout << "test " << test_idx << ", cost = " << cost << "/n";
}
return 0;
}