답안 #778450

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
778450 2023-07-10T10:37:26 Z andrei_iorgulescu T-Covering (eJOI19_covering) C++14
25 / 100
339 ms 1568 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

int n,m,k;
vector<vector<int>>a;
vector<vector<bool>>b;
vector<pair<int,int>>v;
int ori[15];
pair<int,int>cell[10][10];
int ans = -1;

void afis()
{
    bool bb = true;
    int sm = 0;
    for (int i = 1; i <= k; i++)
    {
        bool boo = true;
        for (int j = 1; j <= 4; j++)
        {
            int l = v[i].first + cell[ori[i]][j].first,c = v[i].second + cell[ori[i]][j].second;
            if (l == 0 or l == n + 1 or c == 0 or c == m + 1)
                boo = false,bb = false;
        }
        if (boo == true)
        {
            for (int j = 1; j <= 4; j++)
            {
                int l = v[i].first + cell[ori[i]][j].first,c = v[i].second + cell[ori[i]][j].second;
                if (!b[l][c])
                    b[l][c] = true,sm += a[l][c];
                else
                    bb = false;
            }
        }
    }
    for (int i = 1; i <= k; i++)
    {
        bool boo = true;
        for (int j = 1; j <= 4; j++)
        {
            int l = v[i].first + cell[ori[i]][j].first,c = v[i].second + cell[ori[i]][j].second;
            if (l == 0 or l == n + 1 or c == 0 or c == m + 1)
                boo = false;
        }
        if (boo == true)
        {
            for (int j = 1; j <= 4; j++)
            {
                int l = v[i].first + cell[ori[i]][j].first,c = v[i].second + cell[ori[i]][j].second;
                b[l][c] = false;
            }
        }
    }
    if (bb == true)
        ans = max(ans,sm);
}

void bkt(int pos)
{
    if (pos == k + 1)
        afis();
    else
    {
        for (int i = 1; i <= 4; i++)
        {
            ori[pos] = i;
            bkt(pos + 1);
        }
    }
}

void solve_small()
{
    b.resize(n + 1);
    for (int i = 1; i <= n; i++)
        b[i].resize(m + 1);
    bkt(1);
    if (ans == -1)
        cout << "No";
    else
        cout << ans;
}

bool all_same_row()
{
    for (int i = 2; i <= k; i++)
        if (v[i].first != v[1].first)
            return false;
    return true;
}

int dp[1005][10];///dp[i][j] = cmax sa pun primele i T-uri, ultimul avand rotatia j

bool inters(pair<int,int>p1,int r1,pair<int,int>p2,int r2)
{
    for (int c1 = 1; c1 <= 4; c1++)
    {
        for (int c2 = 1; c2 <= 4; c2++)
        {
            pair<int,int>n1 = {p1.first + cell[r1][c1].first,p1.second + cell[r1][c1].second};
            pair<int,int>n2 = {p2.first + cell[r2][c2].first,p2.second + cell[r2][c2].second};
            if (n1 == n2)
                return true;
        }
    }
    return false;
}

void solve_all_same_row()
{
    sort(v.begin() + 1,v.end());
    for (int r = 1; r <= 4; r++)
    {
        bool boo = true;
        int sm = 0;
        for (int j = 1; j <= 4; j++)
        {
            int l = v[1].first + cell[r][j].first,c = v[1].second + cell[r][j].second;
            if (l == 0 or l == m + 1 or c == 0 or c == m + 1)
                boo = false;
            else
                sm += a[l][c];
        }
        if (boo == true)
            dp[1][r] = sm;
        else
            dp[1][r] = -2e6;
    }
    for (int i = 2; i <= k; i++)
    {
        for (int r = 1; r <= 4; r++)
        {
            bool boo = true;
            int sm = 0;
            for (int j = 1; j <= 4; j++)
            {
                int l = v[i].first + cell[r][j].first,c = v[i].second + cell[r][j].second;
                if (l == 0 or l == m + 1 or c == 0 or c == m + 1)
                    boo = false;
                else
                    sm += a[l][c];
            }
            if (boo == false)
                dp[i][r] = -2e6;
            else
            {
                dp[i][r] = -2e6;
                for (int rant = 1; rant <= 4; rant++)
                    if (dp[i - 1][rant] >= 0 and !inters(v[i - 1],rant,v[i],r))
                        dp[i][r] = max(dp[i][r],dp[i - 1][rant] + sm);
            }
        }
    }
    int mx = max(max(dp[k][1],dp[k][2]),max(dp[k][3],dp[k][4]));
    if (mx < 0)
        cout << "No";
    else
        cout << mx;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> m;
    a.resize(n + 1);
    for (int i = 1; i <= n; i++)
    {
        a[i].resize(m + 1);
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];

    }
    cin >> k;
    v.resize(k + 1);
    for (int i = 1; i <= k; i++)
        cin >> v[i].first >> v[i].second,v[i].first++,v[i].second++;
    cell[1][1] = {0,0},cell[1][2] = {0,-1},cell[1][3] = {0,1},cell[1][4] = {1,0};
    cell[2][1] = {0,0},cell[2][2] = {0,-1},cell[2][3] = {0,1},cell[2][4] = {-1,0};
    cell[3][1] = {0,0},cell[3][2] = {-1,0},cell[3][3] = {0,1},cell[3][4] = {1,0};
    cell[4][1] = {0,0},cell[4][2] = {-1,0},cell[4][3] = {0,-1},cell[4][4] = {1,0};
    if (k <= 10)
    {
        solve_small();
        return 0;
    }
    if (all_same_row() == true)
    {
        solve_all_same_row();
        return 0;
    }
    /*if (all_subtask3() == true)
    {
        solve_subtask2();
        return 0;
    }
    solve_normal();*/
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 320 ms 340 KB Output is correct
2 Incorrect 2 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 330 ms 392 KB Output is correct
2 Incorrect 3 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 284 ms 392 KB Output is correct
2 Incorrect 3 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 300 ms 312 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 6 ms 936 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 6 ms 1568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 315 ms 212 KB Output is correct
2 Correct 5 ms 212 KB Output is correct
3 Correct 339 ms 300 KB Output is correct
4 Correct 73 ms 296 KB Output is correct
5 Correct 313 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 320 ms 340 KB Output is correct
2 Incorrect 2 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 320 ms 340 KB Output is correct
2 Incorrect 2 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -