1130B - Two Cakes - CodeForces Solution


greedy *1200

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;

// Header files, namespaces, 
// macros as defined above 
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
  
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
  

#define Faster_IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vint vector<int>
#define vlong vector<ll>
#define vchar vector<char>
#define vbool vector<bool>
#define vstring vector<string>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>> 
#define PR_Q priority_queue
#define sz(var) (int)(var).size()
#define flf(var, start, lim_of_loop) for(ll var=start;var<=lim_of_loop;var++)
#define flr(var, start, lim_of_loop) for(ll var=start;var>=lim_of_loop;var--)
#define mset(var) memset(var, 0, sizeof(var))
#define sorting(var1, var2) sort(var1.begin() + var2, var1.end())
#define all(var1, var2) var1.begin() + var2, var1.end()
#define bitcount(var) __builtin_popcount(var)


#define here cout<<"Here"<<endl
#define haa cout<<"YES"<<endl
#define naa cout<<"NO"<<endl
#define line cout << endl;
#define nl endl
#define endl '\n'
#define sp " "
#define ff first
#define ss second
#define LB lower_bound
#define UB upper_bound
#define llmax LLONG_MAX
#define llmin LLONG_MIN
#define intmax INT_MAX
#define intmin INT_MIN
#define sstream stringstream
#define pf(var) push_front(var)
#define pb(var) push_back(var)
#define pbpr(var1, var2) push_back({var1, var2})
#define modval 1000000007
#define PI acos(-1.0)
#define eps 0.0000001 

template < typename F, typename S >ostream& operator << ( ostream& os, const pair< F, S > & p ) {return os << "(" << p.first << ", " << p.second << ")";}
template < typename T >ostream &operator << ( ostream & os, const vector< T > &v ) {os << "{";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "}";}
template < typename T >ostream &operator << ( ostream & os, const set< T > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "]";}
template < typename T >ostream &operator << ( ostream & os, const multiset< T > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << *it;}return os << "]";}
template < typename F, typename S >ostream &operator << ( ostream & os, const map< F, S > &v ) {os << "[";for(auto it = v.begin(); it != v.end(); ++it) {if( it != v.begin() ) os << ", ";os << it -> first << " = " << it -> second ;}return os << "]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)
void faltu () {cerr << endl;}
template <typename T>void faltu( T a[], int n ) {for(int i = 0; i < n; ++i) cerr << a[i] << ' ';cerr << endl;}
template <typename T, typename ... hello>void faltu( T arg, const hello &... rest) {cerr << arg << ' ';faltu(rest...);}

bool CMP(pair<ll, ll> &a, pair<ll, ll> &b){
	if(a.ff > b.ff){
		return 1;
	}
	else if(a.ff == b.ff){
		if(a.ss < b.ss){
			return 1;
		}
		else{
			return 0;
		}
	}
	else{
		return 0;
	}
}

bool SPACE(char ch){if(ch == ' '){return true;}else{return false;}}
bool PALINDROME(string s, int sz){for(int i=1, j=sz;i<=(sz/2);i++, j--){if(s[i] != s[j]){return false;}}return true;}
bool PRIME(ll n){if(n < 2){return false;}if(n == 2){return true;}for(ll ii = 2; ii * ii <= n; ii++){if(n % ii == 0){return false;}}return true;}


ll LEN(ll x){ll res=0;while(x!=0){res++;x/=10;}return res;}
ll CEIL(ll n, ll x){if(n % x == 0){return (n / x);}else{return ((n / x) + 1);}}
ll GCD(ll a, ll b){if (b == 0){return a;}return GCD(b, a % b);}
ll LCM(ll a, ll b){return (a / GCD(a, b)) * b;}
ll FACTORIAL(ll a){if(a == 0 || a == 1){return 1;}else{return a * FACTORIAL(a - 1);}}
ll COMBINATION(ll n, ll r){ll res = 0;if (r == 0) {return 1;}else{res = COMBINATION(n, r - 1) * (n - r + 1) / r;}return res;}
ll SOD(ll x){ll res = 0;while(x != 0){res += (x % 10); x /= 10;}return res;}
ll SUBSTRACT(ll a, ll b, ll mod){return (a%mod-b%mod+mod)%mod;}

/// Base Changer ///
char reVal(ll num){if (num >= 0 && num <= 9){return (char)(num + '0');}else{return (char)(num - 10 + 'A');}}
string BASE_CHANGER(ll inputNum, ll base){ll index = 0;string res = "";while (inputNum > 0) {res.push_back(reVal(inputNum % base));index++;inputNum /= base;}reverse(res.begin(), res.end());return res;}

void STACK_Show(stack<int> temp){while(!temp.empty()){cout << temp.top() << sp; temp.pop();}cout << endl;}

void Q_Show(queue<int> temp){while(!temp.empty()){cout << temp.front() << sp; temp.pop();}cout << endl;}
void DQ_Show(deque<int> temp){while(!temp.empty()){cout << temp.front() << sp; temp.pop_front();}cout << endl;}
void PRQ_SHOW(PR_Q<ll> temp){while(!temp.empty()){cout << temp.top() << sp; temp.pop();}cout << endl;}

void Q_Pair_Show(queue<pii>temp){while(!temp.empty()){cout << temp.front().ff << sp << temp.front().ss << endl;temp.pop();}}
void PRQ_Pair_Show(PR_Q<pll, vector<pll>, greater<pll>> temp){while(!temp.empty()){cout << temp.top().ff << sp << temp.top().ss << endl;temp.pop();}cout << endl;}


int dx[]  = {  1, -1,  0,  0,  1, -1,  1, -1};
int dy[]  = {  0,  0,  1, -1,  1, -1, -1,  1};
int knx[] = { -2, -2,  2,  2, -1,  1, -1,  1};
int kny[] = { -1,  1, -1,  1, -2, -2,  2,  2};



vector<vector<ll>> g;
vector<ll> par, need, dp;
vector<char> dir;
vector<bool> vis;

void solve(){
	ll n; cin >> n;

	vlong arr(2 * n);

	vpll vpr(n + 1, {-10, -10});

	for(int i = 0; i < 2 * n; i++){
		cin >> arr[i];

		if(vpr[arr[i]].ff == -10){
			vpr[arr[i]].ff = i;
		}
		else{
			vpr[arr[i]].ss = i;
		}
	}

	pll cur = {0, 0};

	ll ans = 0;

	for(int i = 1; i <= n; i++){
		ll dis1 = abs(cur.ff - vpr[i].ff) + abs(cur.ss - vpr[i].ss);
		ll dis2 = abs(cur.ff - vpr[i].ss) + abs(cur.ss - vpr[i].ff);

		if(dis1 <= dis2){
			ans += dis1;
		}
		else{
			ans += dis2;
		}

		cur = vpr[i];
	}

	cout << ans << endl;

}


int main ()
{
	Faster_IO;
	int tc; tc = 1;
	// int tc; cin >> tc;
	// cout << tc << endl;
	flf(iii, 1, tc){
		// cout << "Case " << iii << ": " << endl;
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths