求助!超时
查看原帖
求助!超时
1001535
封禁用户楼主2024/7/30 17:29
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
const int MANX = 1e8;
namespace fastio {
	const int bufl = 1 << 20;
	const double base1[16] = {1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15};
	const double base2[16] = {1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15};
	struct IN {
		FILE *IT;
		char ibuf[bufl], *is = ibuf, *it = ibuf;
		IN() {
			IT = stdin;
		} IN(char *a) {
			IT = fopen(a, "r");
		}
		inline char getChar() {
			if (is == it) {
				it = (is = ibuf) + fread(ibuf, 1, bufl, IT);
				if (is == it)return EOF;
			}
			return *is++;
		}
		template<typename Temp>inline void getInt(Temp &a) {
			a = 0;
			int b = 0, c = getChar();
			while (c < 48 || c > 57)b ^= (c == 45), c = getChar();
			while (c >= 48 && c <= 57)a = (a << 1) + (a << 3) + c - 48, c = getChar();
			if (b)a = -a;
		}
		IN& operator>>(char &a) {
			a = getChar();
			return *this;
		}
		IN& operator>>(char *a) {
			do {
				*a = getChar();
			} while (*a <= 32);
			while (*a > 32)*++a = getChar();
			*a = 0;
			return *this;
		}
		IN& operator>>(string &a) {
			char b = getChar();
			while (b <= 32)b = getChar();
			while (b > 32)a += b, b = getChar();
			return *this;
		}
		IN& operator>>(int &a) {
			getInt(a);
			return *this;
		}
		IN& operator>>(long long &a) {
			getInt(a);
			return *this;
		}
	};
	struct OUT {
		FILE *IT;
		char obuf[bufl], *os = obuf, *ot = obuf + bufl;
		int Eps;
		long double Acc;
		OUT() {
			IT = stdout, Eps = 6, Acc = 1e-6;
		} OUT(char *a) {
			IT = fopen(a, "w"), Eps = 6, Acc = 1e-6;
		}
		inline void ChangEps(int x = 6) {
			Eps = x;
		}
		inline void flush() {
			fwrite(obuf, 1, os - obuf, IT);
			os = obuf;
		}
		inline void putChar(int a) {
			*os++ = a;
			if (os == ot)flush();
		}
		template<typename Temp>inline void putInt(Temp a) {
			if (a < 0) {
				putChar(45);
				a = -a;
			}
			if (a < 10) {
				putChar(a + 48);
				return;
			}
			putInt(a / 10);
			putChar(a % 10 + 48);
		}
		template<typename Temp>inline void putLeading(Temp a, int b) {
			if (!b)return;
			putLeading(a / 10, b - 1);
			putChar(a % 10 + 48);
		}
		OUT& operator<<(char a) {
			putChar(a);
			return *this;
		}
		OUT& operator<<(char *a) {
			while (*a > 32)putChar(*a++);
			return *this;
		}
		OUT& operator<<(string a) {
			for (auto c : a)putChar(c);
			return *this;
		}
		OUT& operator<<(int a) {
			putInt(a);
			return *this;
		}
		~OUT() {
			flush();
		}
	};
}
using fastio::IN;
using fastio::OUT;
IN fin;
OUT fout;
#define cin fin
#define cout fout
int a[N];
int ans;
int maxx = -1e9;
bitset <N * 30> b;
int v;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (a[i] < 0) ans = -1e9;
		maxx = max(maxx, a[i]);
	}
	if (ans == -1e9) ans = 0;
	else ans = maxx;
	if(ans != 0) {
		cout << ans ;
		return 0;
	}
	if (maxx < 0) {
		cout << maxx ;
		return 0;
	}
	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; i++) {
		if (a[i] > 0) {
			b.reset();
			v = 0;
			b[N * 15] = 1;
			for (int j = 1; j <= n; j++) {
				if (j != i) {
					if (a[j] >= 0) {
						v |= (b << a[j])[N * 15];
						b |= b << a[j];
					} else {
						v |= (b >> (-a[j]))[N * 15];
						b |= b >> (-a[j]);
					}
				}
			}
			for (int j = N * 15 - !v; j >= N * 13; j --) {
				if (b[j]) {
					ans = max(ans, j - N * 15 + a[i]);
					continue;
				}
			}
		}
	}
	cout << ans;
}

为啥会超时,用 bitset 理论能过呀?

2024/7/30 17:29
加载中...