geometry greedy implementation

Please click on ads to support us..

Python Code:

def eff(l):
    if l[0]>=0:
        return l[-1]
    elif l[-1]<0:
        return -l[0]
    elif l[0]<=0 and l[-1]>=0:
        return l[-1]-l[0]
    
t=int(input())
for i in range (0,t):
    x=[]
    y=[]
    n=int(input())
    ans=0
    for i1 in range (0,n):
        cx,cy=input().split()
        if int(cx)==0:
            y.append(int(cy))
        else:
            x.append(int(cx))
    x=sorted(x)
    y=sorted(y)
    if len(x)==0:
        print(2*eff(y))
    elif len(y)==0:
        print(2*eff(x))
    else:
        print(2*eff(x)+2*(eff(y)))
        
        
        

C++ Code:

/*
* @Author: gorsonpy
* @Date: 2023-02-22 17:27
* @Last Modified by: gorsonpy
* @Last Modified time: 2023-02-22 17:27
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<unordered_map>
#include<queue>
#include<set>
#include<unordered_set>
#include<cmath>
using namespace std;
using PII = pair<int, int>;
using LL = long long;

#define x first
#define y second
#define endl '\n'
#define pb push_back
#define cl(a) memset(a, 0, sizeof a)
#define dbg(x) cout << x << ':' << '-' << '-' << x << endl;

const int N = 2e5 + 10, mod = 1e9 + 7; 
int T, n, m, k, q;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; 
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void solve()
{
    cin >> n;
    int xmax = 0, ymax = 0, xmin = 0, ymin = 0;
    for(int i = 0; i < n; ++i)
    {
        int x, y;
        cin >> x >> y;
        xmax = max(xmax, x), xmin = min(x, xmin);
        ymax = max(ymax, y), ymin = min(ymin, y);
    }

    int a = xmax - xmin, b = ymax - ymin;
    cout << (a + b) * 2 << endl;
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> T;
    while(T--) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering