click below for solution

Thursday, 27 January 2022

                                      E. Paint the Middle

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n elements numbered from 1 to n, the element i has value ai and color ci, initially, ci=0 for all i.

The following operation can be applied:

  • Select three elements ij and k (1i<j<kn), such that cicj and ck are all equal to 0 and ai=ak, then set cj=1.

Find the maximum value of i=1nci that can be obtained after applying the given operation any number of times.

Input

The first line contains an integer n (3n2105) — the number of elements.

The second line consists of n integers a1,a2,,an (1ain), where ai is the value of the i-th element.

Output

Print a single integer in a line — the maximum value of i=1nci that can be obtained after applying the given operation any number of times.

Note

In the first test, it is possible to apply the following operations in order:

Solution:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. ll gcd(ll a,ll b)
  5. {
  6. if(a>b)
  7. return gcd(b,a);
  8. if(b%a==0)
  9. return a;
  10. return gcd(a,b%a);
  11. }
  12. void solve()
  13. {
  14. ll n;
  15. cin>>n;
  16. vector<ll>a(n);
  17. vector<ll>b(n);
  18. for(ll i=0;i<n;i++)
  19. {
  20. cin>>a[i];
  21. }
  22. for(ll i=0;i<n;i++)
  23. {
  24. cin>>b[i];
  25. }
  26. ll mxa=0,mxb=0;
  27. for(ll i=0;i<n;i++)
  28. {
  29. ll mn=min(a[i],b[i]);
  30. ll mx=max(a[i],b[i]);
  31. mxa=max(mxa,mx);
  32. mxb=max(mxb,mn);
  33. }
  34. cout<<mxa*mxb<<endl;
  35. }
  36. int main()
  37. {
  38. ll t; cin>>t;
  39. while(t--)
  40. {
  41. solve();
  42. }
  43. }

No comments:

Post a Comment