1881C - Perfect Square - CodeForces Solution


brute force implementation

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
using C = char;
using I = int;
using D = double;
using LD = long double;
using S = string;
using B = bool;
using ULL = unsigned long long;
using G = greater<I>;

#define endl "\n"
#define dbg(x...)
#define fr(i,a,n) for(i = a;i<n;++i)
#define fl(i,n) fr(i,0,n)
#define testno int t; cin>>t; while(t--){solve();}
#define LMAX (LL)2e18
#define LMIN (LL)-2e18
#define yes cout<<"YES"
#define no cout<<"NO"
    
typedef vector<I> VI;
typedef vector<LL> VL;
typedef vector<C> VC;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<pair<I,I>> VP;
typedef priority_queue<I, VI, G> PQ;
typedef priority_queue<I> PI;
typedef unordered_map<I, I>UMI;
typedef unordered_map<LL, I>UMLI;
typedef unordered_map<LL, LL>UML;
typedef unordered_map<I, VI>UVI;
typedef unordered_map<I, PQ> UPQ;
typedef map<LL, LL>MPL;
typedef map<I, I>MPI;
typedef set<I>SI;

#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);

LL gcd(LL a, LL b){
    return (a%b==0)? b : gcd(b, a%b);
}
// bitset<10000007> prime;
// bitset<5000003> chk;
// auto st_prime = [](){
//     prime.set(0); prime.set(1);
//     for(int i=2; i*i<=1e7+7; ++i){
//         if(!prime[i]){
//             for(int j = i*i;j<=1e7+7; j+=i){
//                 prime.set(j);
//             }
//         }
//     }
// };
// vector<int>store(1e9, 0);
// VVI tree;
// VI ver_conn;
// I chk = (1<<31)-1;

void solve(){
    I n; cin>>n;
    vector<vector<C>> vc(n, vector<C>(n));
    I i, j;
    fl(i, n){
        fl(j, n){
            cin>>vc[i][j];
        }
    }
    LL cnt{};
    fl(i, n>>1){
        I k = n-i-1;
        fr(j, i, k){
            C mx = vc[i][j];
            mx = max(mx, max(vc[j][k], max(vc[k][k-(j-i)], vc[k-(j-i)][i])));
            cnt += mx-vc[i][j];
            cnt += mx-vc[j][k];
            cnt += mx-vc[k][k-(j-i)];
            cnt += mx-vc[k-(j-i)][i];
            // vc[i][j] = mx;
            // vc[j][k]=mx;
            // vc[k][k-(j-i)]=mx;
            // vc[k-(j-i)][i]=mx;
        }
    }
    cout<<cnt<<endl;
    // fl(i, n){
    //     fl(j, n){
    //         cout<<vc[i][j];
    //     }
    //     cout<<endl;
    // }
    // cout<<endl;
}
int32_t main(){
    fastio; 
    // st_prime();
    testno;
    // solve();
    // cout<<flush;
    return 0;
}             


Comments

Submit
0 Comments
More Questions

1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson