-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6dc7d4
commit 0b536af
Showing
4 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |