题目大意:
相当于需要找到一个子集 使得 最大化。
思路:
一开始也没想到怎么做的(还是题解好用) , 简单来讲,我们需要将元素分为两个集合 和其补集, 使得两个集合乘积最大,直接枚举集合显然不可行,主要是因为 , 但是呢,数据随机生成,所以我们可以合理猜测最优解会按照某一种比例排序之后出现在中间部分,我们可以按照 的比率从大到小排序,这样最优解可能会比较靠前。然后预处理前缀和和后缀和。接下来,通过在中间部分设置窗口检查,然后枚举集合,找最大值。窗口大小通常为 左右,是可以接受的,给出以下代码:
Code:
#include <bits/stdc++.h>
using namespace std;
using Int128 = __int128;
struct Pnt {
Int128 x;
Int128 y;
double rto;
Pnt(Int128 x = 0, Int128 y = 0) : x(x), y(y), rto(static_cast<double>(x) / y) {}
};
class InpRdr {
public:
Int128 readInt() {
Int128 val = 0;
int sgn = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
val = val * 10 + (ch - '0');
ch = getchar();
}
return val * sgn;
}
};
class OutPrt {
public:
void printInt(Int128 val) {
if (val == 0) {
putchar('0');
putchar('\n');
return;
}
if (val < 0) {
putchar('-');
val = -val;
}
char buf[40];
int idx = 0;
while (val > 0) {
buf[idx++] = '0' + val % 10;
val /= 10;
}
for (int i = idx - 1; i >= 0; i--) {
putchar(buf[i]);
}
putchar('\n');
}
};
class PrfSfx {
public:
vector<Int128> prfX;
vector<Int128> sfxY;
void init(const vector<Pnt>& pnts, int n) {
prfX.resize(n + 2, 0);
sfxY.resize(n + 2, 0);
for (int i = 1; i <= n; ++i) {
prfX[i] = prfX[i - 1] + pnts[i - 1].x;
}
for (int i = n; i >= 1; --i) {
sfxY[i] = sfxY[i + 1] + pnts[i - 1].y;
}
}
};
class Solv {
private:
Int128 tCnt, pNum, a, b;
vector<Pnt> pnts;
PrfSfx ps;
Int128 bstRes;
InpRdr rdr;
OutPrt prt;
void explore(int st, int en, Int128 cX, Int128 cY) {
if (st > en) {
bstRes = max(bstRes, cX * cY);
return;
}
explore(st + 1, en, cX + pnts[st].x, cY);
explore(st + 1, en, cX, cY + pnts[st].y);
}
void sortInit() {
sort(pnts.begin(), pnts.end(), [](const Pnt& a, const Pnt& b) {
return a.rto > b.rto;
});
ps.init(pnts, pNum);
bstRes = 0;
const int DLT = 100;
const int TMP = 7;
int mid = pNum / 2;
int stRng = max(1, mid - DLT);
int enRng = min(static_cast<int>(pNum), mid + DLT);
for (int i = stRng; i <= enRng; ++i) {
int stIdx = max(1, i - TMP + 1);
int enIdx = min(static_cast<int>(pNum), i + TMP - 1);
Int128 bX = ps.prfX[stIdx - 1];
Int128 bY = ps.sfxY[enIdx + 1];
explore(stIdx - 1, enIdx - 1, bX, bY);
}
}
public:
void init() {
tCnt = rdr.readInt();
pNum = rdr.readInt();
a = rdr.readInt();
b = rdr.readInt();
}
void exec() {
while (tCnt-- > 0) {
pnts.clear();
pnts.reserve(pNum);
for (int i = 0; i < pNum; ++i) {
Int128 x = rdr.readInt();
Int128 y = rdr.readInt();
pnts.emplace_back(x, y);
}
sortInit();
prt.printInt(bstRes);
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solv solv;
solv.init();
solv.exec();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using Int128 = __int128;
struct Pnt {
Int128 x;
Int128 y;
double rto;
Pnt(Int128 x = 0, Int128 y = 0) : x(x), y(y), rto(static_cast<double>(x) / y) {}
};
class InpRdr {
public:
Int128 readInt() {
Int128 val = 0;
int sgn = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
val = val * 10 + (ch - '0');
ch = getchar();
}
return val * sgn;
}
};
class OutPrt {
public:
void printInt(Int128 val) {
if (val == 0) {
putchar('0');
putchar('\n');
return;
}
if (val < 0) {
putchar('-');
val = -val;
}
char buf[40];
int idx = 0;
while (val > 0) {
buf[idx++] = '0' + val % 10;
val /= 10;
}
for (int i = idx - 1; i >= 0; i--) {
putchar(buf[i]);
}
putchar('\n');
}
};
class PrfSfx {
public:
vector<Int128> prfX;
vector<Int128> sfxY;
void init(const vector<Pnt>& pnts, int n) {
prfX.resize(n + 2, 0);
sfxY.resize(n + 2, 0);
for (int i = 1; i <= n; ++i) {
prfX[i] = prfX[i - 1] + pnts[i - 1].x;
}
for (int i = n; i >= 1; --i) {
sfxY[i] = sfxY[i + 1] + pnts[i - 1].y;
}
}
};
class Solv {
private:
Int128 tCnt, pNum, a, b;
vector<Pnt> pnts;
PrfSfx ps;
Int128 bstRes;
InpRdr rdr;
OutPrt prt;
void explore(int st, int en, Int128 cX, Int128 cY) {
if (st > en) {
bstRes = max(bstRes, cX * cY);
return;
}
explore(st + 1, en, cX + pnts[st].x, cY);
explore(st + 1, en, cX, cY + pnts[st].y);
}
void sortInit() {
sort(pnts.begin(), pnts.end(), [](const Pnt& a, const Pnt& b) {
return a.rto > b.rto;
});
ps.init(pnts, pNum);
bstRes = 0;
const int DLT = 100;
const int TMP = 7;
int mid = pNum / 2;
int stRng = max(1, mid - DLT);
int enRng = min(static_cast<int>(pNum), mid + DLT);
for (int i = stRng; i <= enRng; ++i) {
int stIdx = max(1, i - TMP + 1);
int enIdx = min(static_cast<int>(pNum), i + TMP - 1);
Int128 bX = ps.prfX[stIdx - 1];
Int128 bY = ps.sfxY[enIdx + 1];
explore(stIdx - 1, enIdx - 1, bX, bY);
}
}
public:
void init() {
tCnt = rdr.readInt();
pNum = rdr.readInt();
a = rdr.readInt();
b = rdr.readInt();
}
void exec() {
while (tCnt-- > 0) {
pnts.clear();
pnts.reserve(pNum);
for (int i = 0; i < pNum; ++i) {
Int128 x = rdr.readInt();
Int128 y = rdr.readInt();
pnts.emplace_back(x, y);
}
sortInit();
prt.printInt(bstRes);
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solv solv;
solv.init();
solv.exec();
return 0;
}