QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#828797 | #9802. Light Up the Grid | XJK404 | TL | 0ms | 0kb | C++14 | 2.2kb | 2024-12-23 20:45:09 | 2024-12-23 20:45:14 |
answer
#include<bits/stdc++.h>
using namespace std;
#define itn int
#define tin int
#define nit int
#define longlong long long
typedef long long ll;
typedef unsigned long long ull;
int a1, a2, a3, a4;
int dp[345141], n, a[17];
void solve() {
cin >> n;
int tot = 0;
for(int i = 1; i <= n; i++) {
char x1, x2, x3, x4;
cin >> x1 >> x2 >> x3 >> x4;
int sum = (x1 - '0') + (x2 - '0') * 2 + (x3 - '0') * 4 + (x4 - '0') * 8;
tot += (1<<(sum));
}
cout << dp[tot] << '\n';
}
int cal(int x,int y) {
if(x != -1) {
for(int i = 0; i < 4; i++) {
if(((x & (1<<i)))== 0) {
if((y & (1<<i)) == 0) {
y = y + (1<<i);
}
else {
y = y - (1<<i);
}
}
}
}
int cnt = 0;
for(int i = 0;i < 4; i++) {
if(y & (1<<i))
cnt++;
}
if(cnt == 0) return a4;
if(cnt == 4) return min(a1,min(a2,min(a3,a4))) * 2;
if(cnt == 1) return min(min(a2, a3) + a1, a1 + a4);
if(cnt == 3) return a1;
if(y == 3 || y == 12) return a2;
if(y == 5 || y == 10) return a3;
return min(2 * a1, a2 + a3);
}
ll deal()
{
ll u = 0, v = 0;
for(int i=1;i<=16;i++)
{
v = u + (1<<a[i]);
int q = cal(a[i - 1], a[i]);
if(i == 1)
q = cal(-1, a[1]);
dp[v] = min(dp[v],dp[u] + q);
u = v;
}
return dp[v];
}
void sa(double T)
{
ll now = 1e9;
while(T>1e-3)
{
int x=rand()%16+1,y=rand()%16+1;
if(x==y)continue;
swap(a[x],a[y]);
ll nw=deal();
if(nw<now||exp((now-nw) * 1.0 / now)/T>double(rand())/RAND_MAX) now = nw;
else
swap(a[x],a[y]);
T=T*0.99;
}
for(int i=1;i<=16;i++)
{
for(int j=1;j<i;j++)
{
swap(a[i],a[j]);
deal();
swap(a[i],a[j]);
}
}
}
double Time(){return (double)clock()/CLOCKS_PER_SEC;}
int main() {
// freopen("a.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
srand(time(0));
int T;
for(int i = 1; i <= 16; i++) {
a[i] = i - 1;
}
cin >> T >> a1 >> a2 >> a3 >> a4;
a2 = min(a2, 2 * a1);
a3 = min(a3, 2 * a1);
a4 = min(min(a2 * 2, a3 * 2), a4);
for(int i = 0;i <= (1<<17); i++) {
dp[i] = 1e9;
}
dp[0] = 0;
while(Time()<1.6)sa(1145);
while(T--) {
solve();
}
// cout << cal(0,1);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
2 1000 100 10 1 4 10 00 01 00 00 10 00 01 1 11 11