Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
33arsenic75 committed Jun 27, 2024
1 parent e6dc7d4 commit 0b536af
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
48 changes: 48 additions & 0 deletions problem-of-the-day/day26/B_I_Hate_1111.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*The main idea is that we can split the numbers into the two halves, the big half and small half,
we can place the bigger half at the odd positions and the smaller half at the even positions.
This works because the smallest big number is larger than the biggest small number.
Hence, the mean of any two small numbers is smaller than any big number,
and the mean of any two big numbers is bigger than any small number.*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl

#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound

#define rep(x,start,end) for(auto x=(start);x!=(end);((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

int n;
int arr[55];

int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);

int t;
cin >> t;
while (t--){
cin >> n;
rep(x, 0, 2 * n) cin >> arr[x];

sort(arr, arr + 2 * n);

rep(x, 0, n) cout << arr[x] << " " << arr[x + n] << " ";
cout << endl;
}
}
73 changes: 73 additions & 0 deletions problem-of-the-day/day26/Good Sequences.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*The main idea is DP. Let's define dp[x] as the maximal value of the length of the good sequence whose last element is x,
and define d[i] as the (maximal value of dp[x] where x is divisible by i).
You should calculate dp[x] in the increasing order of x.
The value of dp[x] is (maximal value of d[i] where i is a divisor of x) + 1.
After you calculate dp[x], for each divisor i of x, you should update d[i] too.
This algorithm works in O(nlogn) because the sum of the number of the divisor from 1 to n is O(nlogn).
Note that there is a corner case. When the set is {1}, you should output 1.
*/


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vl vector<ll>
#define pb push_back
#define pop pop_back
#define vi vector<int>
#define vb vector<bool>
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()

vl divisor(ll n){
vl div(0);
set<ll> s;
for (ll i = 2; i * i <= n ; i++)
{
if(s.find(i) != s.end()){
break;
}
if(n % i == 0){
s.insert(i);
s.insert(n / i);
div.pb(i);
div.pb(n / i);
}
}
div.pb(n);
return div;
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0);
ll n;
cin>>n;
vl a(n);
for (int i = 0; i < n; i++)
{
cin>>a[i];
}
if(n == 1){
cout<<1<<endl;
}else{
vl dp(n);
vl d(a[n - 1] + 1);
for(int i = 0; i < n; i++){
vl div = divisor(a[i]);
ll maxi = 0;
for (auto it : div)
{
maxi = max(d[it], maxi);
}
dp[i] = max(dp[i], maxi + 1);
for (auto it : div)
{
d[it] = dp[i];
}
}
cout<<*max_element(all(dp))<<endl;
}
}
19 changes: 19 additions & 0 deletions problem-of-the-day/day26/Kth factor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int kthFactor(int n, int k) {
// If i is a divisor of n, then n / i is also a divisor of n. So, we can
// find all the divisors of n by processing the numbers <= sqrt(n).
int factor = 1;
int i = 0; // the i-th factor

for (; factor * factor < n; ++factor)
if (n % factor == 0 && ++i == k)
return factor;

for (factor = n / factor; factor >= 1; --factor)
if (n % factor == 0 && ++i == k)
return n / factor;

return -1;
}
};
45 changes: 45 additions & 0 deletions problem-of-the-day/day26/LCM Challenge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*It is a simple problem, but many competitors used some wrong guesses and failed. First of all, we should check
if n is at most 3 and then we can simply output 1,2,6.
Now there are two cases: When n is odd, the answer is obviously n(n-1)(n-2). When n is even,
we can still get at least (n-1)(n-2)(n-3), so these three numbers in the optimal answer would not be very small compared to n.
So we can just iterate every 3 number triple in [n-50,n] and update the answer.*/

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;

ll lcm(ll a ,ll b){
return a*b / (__gcd(a,b));
}

int main()
{
long long n;
cin >> n;
long long ans = 0;
if (n % 2)
{
ans = n * (n - 1) * (n - 2);
if (1 == n)
ans = 1;
}
else
{
for (long long i = max(1LL, n - 50); i <= n; i++)
for (long long j = max(1LL, n - 50); j <= n; j++)
for (long long k = max(1LL, n - 50); k <= n; k++)
{
ans = max(ans, lcm(i, lcm(j, k)));
}
}
cout << ans << "\n";
return 0;
}

0 comments on commit 0b536af

Please sign in to comment.