QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#585464 | #1773. Breaking Bars | KarL06 | TL | 393ms | 47212kb | C++20 | 5.4kb | 2024-09-23 20:54:07 | 2024-09-23 20:54:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define nL '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define int long long
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
const ll MOD = 1e9 + 7;
void settings()__attribute__((constructor));
void eval(bool condition) { cout << (condition ? "yes" : "no") << nL; }
void Eval(bool condition) { cout << (condition ? "Yes" : "No") << nL; }
void EVAL(bool condition) { cout << (condition ? "YES" : "NO") << nL; }
int ipow(int a, int n) {
if (n == 0) return 1;
int x = ipow(a, n/2);
if (n % 2 == 0) return x*x;
return x*x*a;
}
template <typename T>
ostream& operator<<(ostream& stream, const vector<T>& v) {
for (auto elem : v)
stream << elem << " ";
return stream;
}
template <typename T>
istream& operator>>(istream& stream, vector<T>& v){
for(auto &elem : v)
stream >> elem;
return stream;
}
void settings() {
#ifdef LOCAL
freopen("io/input.txt", "r", stdin);
freopen("io/output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
}
vector<map<vi, int>> dp;
map<pii, int> mapper;
map<int, pii> invMapper;
int toMask(const vi& a) {
int res = 0;
for (int i = 0; i < a.size(); i++) res += (1LL<<i)*a[i];
return res;
}
bool emp(const vi& a) {
bool res = true;
for (int i = 0; i < a.size(); i++)res &= a[i] == 0;
return res;
}
int MAX_X = 2;
int res1 = -1;
int cnt = 0;
int currX = 0;
int getDP(int t, vi& a) {
// if (res1 != -1) cnt++;
// if (cnt >= 1000000) return INT_MAX;
if (t <= 0) return 0;
if (dp[t].count(a)) return dp[t][a];
// if (res1 != -1 && currX >= res1) {
// return INT_MAX;
// }
if (emp(a) && t > 0) return INT_MAX;
int idx = 0;
for (int i = 20; i >= 0; i--) {
if (a[i] > 0) {
idx = i;
break;
}
}
pii curr = invMapper[idx];
vi oldA = a;
for (int i = 0; i < 21; i++) {
if (a[i] > MAX_X) {
int tmp = a[i]-MAX_X;
if (tmp % 2 == 1) tmp++;
// cout << tmp << nL;
a[i] -= tmp;
dp[t][oldA] = getDP(t-invMapper[i].first*invMapper[i].second*(tmp/2), a);
a[i] += tmp;
return dp[t][oldA];
}
}
int tmp = a[idx];
a[idx] = 0;
dp[t][oldA] = getDP(t-curr.first*curr.second*(tmp/2), a);
a[idx] = tmp;
for (int i = 1; i < curr.first; i++) {
a[idx]--;
a[(mapper[{i, curr.second}])]++;
a[(mapper[{curr.first-i, curr.second}])]++;
currX++;
dp[t][oldA] = min(dp[t][oldA], getDP(t, a)+1);
currX--;
a[idx]++;
a[(mapper[{i, curr.second}])]--;
a[(mapper[{curr.first-i, curr.second}])]--;
}
for (int i = 1; i < curr.second; i++) {
a[idx]--;
a[(mapper[{curr.first, i}])]++;
a[(mapper[{curr.first, curr.second-i}])]++;
currX++;
dp[t][oldA] = min(dp[t][oldA], getDP(t, a)+1);
currX--;
a[idx]++;
a[(mapper[{curr.first, i}])]--;
a[(mapper[{curr.first, curr.second-i}])]--;
}
return dp[t][oldA];
}
vector<map<vi, int>> dp2;
clock_t time_req;
int getDP2(int br, vi& a) {
if (dp2[br].count(a)) return dp2[br][a];
if (br == 0) {
int tmp = 0;
for (int i = 0; i < 21; i++) {
tmp += (a[i]/2)*invMapper[i].first*invMapper[i].second;
}
dp2[br][a] = tmp;
return tmp;
}
vi oldA = a;
dp2[br][oldA] = getDP2(br-1, a);
int idx = 0;
for (int i = 20; i >= 0; i--) {
if (a[i] > 0) {
idx = i;
break;
}
}
int tmp = a[idx];
a[idx] = 0;
dp2[br][oldA] = getDP2(br, a)+(tmp/2)*invMapper[idx].first*invMapper[idx].second;
a[idx] = tmp;
pii curr = invMapper[idx];
for (int i = 1; i < curr.first; i++) {
a[idx]--;
a[(mapper[{i, curr.second}])]++;
a[(mapper[{curr.first-i, curr.second}])]++;
dp2[br][oldA] = max(dp2[br][oldA], getDP2(br-1, a));
a[idx]++;
a[(mapper[{i, curr.second}])]--;
a[(mapper[{curr.first-i, curr.second}])]--;
}
for (int i = 1; i < curr.second; i++) {
a[idx]--;
a[(mapper[{curr.first, i}])]++;
a[(mapper[{curr.first, curr.second-i}])]++;
dp2[br][oldA] = max(dp2[br][oldA], getDP2(br-1, a));
// cout << curr.first << " " << curr.second << " " << dp[br][oldA] << nL;
a[idx]++;
a[(mapper[{curr.first, i}])]--;
a[(mapper[{curr.first, curr.second-i}])]--;
}
return dp2[br][oldA];
}
signed main() {
time_req = clock();
int idx = 0;
for (int i = 1; i <= 6; i++) {
for (int j = i; j <= 6; j++) {
mapper[{i, j}] = idx;
invMapper[idx] = {i, j};
mapper[{j, i}] = idx++;
}
}
int n, t;
cin >> n >> t;
vi a(21);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
a[mapper[{s[0]-'0', s[2]-'0'}]]++;
}
// for (int i = 0; i < 21; i++) {
// t -= invMapper[i].first*invMapper[i].second*(a[i]/2);
// a[i] %= 2;
// }
// cout << a << nL;
dp = vector<map<vi, int>>(t+5);
MAX_X = 1;
res1 = getDP(t, a);
dp2 = vector<map<vi, int>>(res1+5);
// MAX_X = 2;
// cout << min(res1, getDP(t, a)) << nL;
for (int i = 0; i <= res1; i++) {
if ((float)(clock()- time_req)/CLOCKS_PER_SEC > 1.0) {
cout << res1 << nL;
break;
}
if (getDP2(i, a) < t) continue;
cout << i << nL;
break;
}
// cout << (float)(clock()- time_req)/CLOCKS_PER_SEC << nL;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 33ms
memory: 9408kb
input:
16 118 5x6 3x5 4x5 6x3 6x1 1x1 4x5 4x5 2x3 1x2 5x3 5x3 6x2 3x6 5x6 4x2
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3732kb
input:
6 30 2x3 3x3 1x5 2x5 3x5 3x5
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
3 2 1x1 1x1 1x2
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 2ms
memory: 4108kb
input:
4 25 2x3 3x3 2x5 5x5
output:
2
result:
ok single line: '2'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
5 10 1x1 1x1 1x1 1x1 4x4
output:
1
result:
ok single line: '1'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3756kb
input:
6 34 1x1 1x2 2x3 3x3 5x5 5x5
output:
2
result:
ok single line: '2'
Test #7:
score: 0
Accepted
time: 198ms
memory: 27796kb
input:
15 70 1x1 1x2 1x3 1x4 1x5 2x2 2x3 2x4 2x5 3x3 3x4 3x5 4x4 4x5 5x5
output:
7
result:
ok single line: '7'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
11 40 1x1 1x2 1x3 2x3 2x4 4x5 2x2 2x2 1x4 2x4 4x5
output:
3
result:
ok single line: '3'
Test #9:
score: 0
Accepted
time: 63ms
memory: 13456kb
input:
20 74 4x3 5x1 4x2 3x3 1x2 2x1 4x2 1x5 1x1 1x4 1x1 5x5 1x4 4x5 3x2 3x5 1x2 3x4 1x1 2x3
output:
6
result:
ok single line: '6'
Test #10:
score: 0
Accepted
time: 111ms
memory: 18320kb
input:
20 104 4x2 2x3 4x5 5x1 1x3 4x3 1x2 1x1 5x2 5x4 5x5 4x1 5x5 3x5 4x2 3x1 3x1 5x4 2x1 4x4
output:
6
result:
ok single line: '6'
Test #11:
score: 0
Accepted
time: 7ms
memory: 5736kb
input:
13 44 5x4 3x2 3x2 4x1 4x4 1x2 1x5 1x2 1x1 3x5 1x2 1x3 3x2
output:
5
result:
ok single line: '5'
Test #12:
score: 0
Accepted
time: 2ms
memory: 3940kb
input:
5 21 1x3 1x2 5x4 4x4 1x1
output:
3
result:
ok single line: '3'
Test #13:
score: 0
Accepted
time: 80ms
memory: 15400kb
input:
18 77 5x4 4x2 5x5 1x4 3x1 4x3 2x3 1x1 3x4 5x2 5x3 2x2 2x1 2x1 1x2 5x3 3x3 1x4
output:
6
result:
ok single line: '6'
Test #14:
score: 0
Accepted
time: 5ms
memory: 4492kb
input:
9 30 5x2 5x3 1x4 1x4 2x3 1x2 3x3 2x3 4x1
output:
5
result:
ok single line: '5'
Test #15:
score: 0
Accepted
time: 3ms
memory: 4108kb
input:
8 37 2x4 1x3 5x4 5x5 2x4 1x4 1x2 1x4
output:
2
result:
ok single line: '2'
Test #16:
score: 0
Accepted
time: 393ms
memory: 47212kb
input:
19 103 1x5 5x2 2x2 5x4 1x5 1x1 5x5 2x2 2x5 5x4 3x4 3x2 4x4 5x4 5x3 2x2 2x4 4x3 3x3
output:
7
result:
ok single line: '7'
Test #17:
score: 0
Accepted
time: 152ms
memory: 22668kb
input:
19 75 2x1 1x1 5x5 2x4 1x3 2x3 2x2 2x3 4x5 4x3 3x1 4x1 4x2 4x4 5x1 1x4 1x5 5x3 3x1
output:
7
result:
ok single line: '7'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3880kb
input:
20 81 2x3 2x5 5x3 2x1 3x1 5x2 4x5 2x1 1x5 5x2 2x5 1x5 3x2 1x5 1x2 4x2 4x2 5x4 3x2 3x3
output:
2
result:
ok single line: '2'
Test #19:
score: -100
Time Limit Exceeded
input:
47 297 3x5 3x2 1x5 5x6 5x5 5x5 4x2 5x4 4x1 6x2 6x6 5x3 1x2 2x6 6x2 3x3 2x2 2x2 1x4 2x5 5x3 4x4 6x3 3x6 5x4 3x6 3x1 6x1 3x1 1x2 3x4 1x6 6x6 5x3 1x1 5x5 2x1 1x4 5x1 5x6 2x1 4x6 2x2 6x6 2x3 6x1 3x1