1
0
Fork 0

Practice some C++

This commit is contained in:
Nguyễn Gia Phong 2019-10-14 20:59:08 +07:00
parent 4b8df72277
commit cacc165173
5 changed files with 189 additions and 0 deletions

58
codechef/marm.cc Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
#include <vector>
using namespace std;
int
main()
{
int t, n;
long k;
cin >> t;
while (t--)
{
cin >> n >> k;
long d = k / n, m = k % n;
vector<long> a (n);
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n / 2; ++i)
{
switch ((d + (m > i)) % 3)
{
case 1:
cout << (a[i] ^ a[n - i - 1]);
break;
case 2:
cout << a[n - i - 1];
break;
default:
cout << a[i];
}
cout << ' ';
}
if (n % 2)
cout << ((d + (m > n >> 1)) ? 0 : a[n >> 1]) << ' ';
for (int i = n - (n >> 1); i < n; ++i)
{
switch ((d + (m > i)) % 3)
{
case 1:
cout << a[n - i - 1];
break;
case 2:
cout << (a[i] ^ a[n - i - 1]);
break;
default:
cout << a[i];
}
cout << ((i + 1 == n) ? '\n' : ' ');
}
}
return 0;
}

25
codechef/msng.py Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
from collections import Counter
for t in range(int(input())):
n, x = int(input()), []
for i in range(n):
b, y = input().split()
if b == '-1':
guesses = set()
for b in range(2, 37):
try:
guess = int(y, b)
except ValueError:
pass
else:
if guess > 10 ** 12: break
guesses.add(guess)
x.extend(guesses)
else:
guess = int(y, int(b))
if guess <= 10 ** 12: x.append(guess)
try:
print(min(k for k, v in Counter(x).items() if v == n))
except ValueError:
print(-1)

39
codechef/msv.cc Normal file
View File

@ -0,0 +1,39 @@
#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;
int
main()
{
int t, n, i;
long a;
cin >> t;
while (t--)
{
int star = 0;
unordered_map<long, int> m;
cin >> n;
while (n--)
{
cin >> a;
star = max (m[a], star);
for (i = 1; i * i < a; ++i)
if (a % i == 0)
{
m[i]++;
m[a / i]++;
}
if (i * i == a)
m[i]++;
}
cout << star << endl;
}
return 0;
}

35
codechef/s10e.cc Normal file
View File

@ -0,0 +1,35 @@
#include <algorithm>
#include <deque>
#include <iostream>
using namespace std;
int
main()
{
int t;
int tmp;
deque<int> p;
cin >> t;
while (t--)
{
int n;
int r = 0;
cin >> n;
p.clear();
while (n--)
{
cin >> tmp;
auto it = min_element (p.begin(), p.end());
r += it == p.end() || *it > tmp;
p.push_back (tmp);
if (p.size() > 5)
p.pop_front();
}
cout << r << endl;
}
return 0;
}

32
codechef/saktan.cc Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int
main()
{
int t;
long n, m, q, x, y;
cin >> t;
while (t--)
{
cin >> n >> m >> q;
vector<int> u (n), v (m);
while (q--)
{
cin >> x >> y;
u[x - 1] ^= 1;
v[y - 1] ^= 1;
}
x = accumulate (u.begin(), u.end(), 0);
y = accumulate (v.begin(), v.end(), 0);
cout << x * (m - y) + (n - x) * y << endl;
}
return 0;
}