Submission #876395

# Submission time Handle Problem Language Result Execution time Memory
876395 2023-11-21T16:30:06 Z vjudge1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back //emplace_back
#define pp pop_back
#define pii pair<ll, ll> //pair<int, int>
#define all(x) (x).begin(),(x).end()
#define mp(a,b) make_pair(a , b)
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)(x).size()
#define F first
#define S second
#define bg(x) (x).begin()
#define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
#define debug(x) cout << #x << " : " << x << endl
#define endl (char)(10)
#define arr(x) array<int , (x)>
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
     
ll Sum(ll a , ll b , ll MOD)
{
    a %= MOD;
    b %= MOD;
    a += b;
    return a % MOD;
}
     
ll Mul(ll a , ll b , ll MOD)
{
    a %= MOD;
    b %= MOD;
    a *= b;
    return a % MOD;
}
     
ll Pow(ll a , ll b , ll MOD)
{
    ll res = 1;
    while(b)
    {
        if((b & 1))res = Mul(res , a , MOD);
        a = Mul(a , a , MOD);
        b >>= 1;
    }
    return res;
}
     
ll Min(ll a , ll b)
{
    if(a > b)return b;
    return a;
}
     
ll Max(ll a , ll b)
{
    if(a > b)return a;
    return b;
}
     
ll Ceil(ll a , ll b)
{
    if(a % b)return (a/b)+1;
    return a/b;
}
     
/////////////////////
//VALS

/////////////////////
//FUNCTIONS
ll Abs(ll a, ll b)
{
	ll c = a -b;
	if(c < 0)return -c;
	return c;
}
/////////////////////
//SOLVE
void solve()
{
	ll n;
	cin >> n;
	string s;
	cin >> s;

	ll cntr = 0,cntg = 0;
	
	For(i,n)if(s[i] == 'R')cntr++;
	else if(s[i] == 'G')cntg++;

	if(n % 2 == 0)
		if(cntr != n/2)cout << -1 << endl;
	
	if(n % 2 == 1)
		if(cntr != n/2 and cntr != n/2+1){cout << -1 << endl;return;}

	if(n % 2 == 1)
	{
		if(cntr > cntg)
		{
			ll ans = 0;
			ll x = 0;
			For(i,n)
			{
				if(s[i] == 'R')
				{
					ans += Abs(i,x);
					x += 2;
				}
			}
			x = 1;
			For(i,n)
			{
				if(s[i] == 'G')
				{
						ans += Abs(i, x);
						x += 2;
				}
			}

			cout << ans << endl;
		}
		else
		{
			ll ans = 0;
			ll x = 0;
			For(i,n)
			{
				if(s[i] == 'G')
				{
					ans += Abs(i,x);
					x += 2;
				}
			}
			x = 1;
			For(i,n)
			{
				if(s[i] == 'R')
				{
						ans += Abs(i, x);
						x += 2;
				}
			}	
			cout << ans << endl;
		}
	}
	else
	{
			ll ans = 0;
			ll x = 0;
			For(i,n)
			{
				if(s[i] == 'R')
				{
					ans += Abs(i,x);
					x += 2;
				}
			}
			x = 1;
			For(i,n)
			{
				if(s[i] == 'G')
				{
						ans += Abs(i, x);
						x += 2;
				}
			}
			
			ll ans2 = 0;
			x = 0;
			For(i,n)
			{
				if(s[i] == 'G')
				{
					ans2 += Abs(i,x);
					x += 2;
				}
			}
			x = 1;
			For(i,n)
			{
				if(s[i] == 'R')
				{
						ans2 += Abs(i, x);
						x += 2;
				}
			}

			cout << Min(ans,ans2) << endl;
	}
}
/////////////////////
//MAIN
int main()
{
    FAST;
    ll t = 1;
//    cin >> t;
    while(t--)
    {
    solve();
    }
}
/////////////////////
/*
ZZZZZZZ     A        M     M     IIIIIII  N     N
     Z     A A      M M   M M       I     NN    N
    Z     A   A    M   M M   M      I     N N   N
   Z     AAAAAAA  M     M     M     I     N  N  N
  Z      A     A  M           M     I     N   N N
 Z       A     A  M           M     I     N    NN
ZZZZZZZ  A     A  M           M  IIIIIII  N     N  TREE
*/

Compilation message

joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:94:2: note: in expansion of macro 'For'
   94 |  For(i,n)if(s[i] == 'R')cntr++;
      |  ^~~
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:109:4: note: in expansion of macro 'For'
  109 |    For(i,n)
      |    ^~~
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:118:4: note: in expansion of macro 'For'
  118 |    For(i,n)
      |    ^~~
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:133:4: note: in expansion of macro 'For'
  133 |    For(i,n)
      |    ^~~
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:142:4: note: in expansion of macro 'For'
  142 |    For(i,n)
      |    ^~~
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:157:4: note: in expansion of macro 'For'
  157 |    For(i,n)
      |    ^~~
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:166:4: note: in expansion of macro 'For'
  166 |    For(i,n)
      |    ^~~
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:177:4: note: in expansion of macro 'For'
  177 |    For(i,n)
      |    ^~~
joi2019_ho_t3.cpp:18:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
      |                           ^
joi2019_ho_t3.cpp:186:4: note: in expansion of macro 'For'
  186 |    For(i,n)
      |    ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -