move
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
// 89
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
long long a, b, p;
|
||||
long long pow(long long a, long long b, long long p)
|
||||
{
|
||||
if (b == 0)
|
||||
return 1;
|
||||
long long half = pow(a, b >> 1, p) % p;
|
||||
return (half * half % p * ((b % 2) ? a : 1)) % p;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
long long a, b, p;
|
||||
scanf("%lld%lld%lld", &a, &b, &p);
|
||||
printf("%lld\n", pow(a, b, p) % p);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// 96
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
cout << "1\n3\n5\n9\n13\n17\n25\n33\n41\n49\n65\n81";
|
||||
return 0;
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
// 97
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
#include <cmath>
|
||||
#define int long long
|
||||
using namespace std;
|
||||
const int mod = 9901;
|
||||
int pow(int a, int b, int p)
|
||||
{
|
||||
a %= p;
|
||||
int res = 1;
|
||||
while (b > 0)
|
||||
{
|
||||
if (b % 2)
|
||||
res *= a, res %= p;
|
||||
a = a * a % p, b >>= 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
signed main()
|
||||
{
|
||||
int a, b;
|
||||
scanf("%lld%lld", &a, &b);
|
||||
map<int, int> yuec;
|
||||
for (int i = 2; a > 1; i++)
|
||||
while (a % i == 0)
|
||||
yuec[i]++, a /= i;
|
||||
int ans = 1;
|
||||
for (pair<int, int> i : yuec)
|
||||
{
|
||||
if ((i.first - 1) % mod == 0)
|
||||
ans *= (i.second * b + 1) % mod;
|
||||
else
|
||||
ans *= (pow(i.first, i.second * b + 1, mod) - 1) * pow(i.first - 1, mod - 2, mod)%mod;
|
||||
ans %= mod;
|
||||
}
|
||||
if(!a) ans=0;
|
||||
printf("%lld\n", (ans+mod)%mod);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
// 98
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
// 101
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
int n, r;
|
||||
int maxx = 0, maxy = 0;
|
||||
int sum[5005][5005];
|
||||
int query(int i, int j)
|
||||
{
|
||||
int x2 = min(maxx, i + r - 1);
|
||||
int y2 = min(maxy, j + r - 1);
|
||||
return sum[x2][y2] - sum[i - 1][y2] - sum[x2][j - 1] + sum[i - 1][j - 1];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d", &n, &r);
|
||||
for (int x, y, w, i = 1; i <= n; ++i)
|
||||
scanf("%d%d%d", &x, &y, &w), maxx = max(maxx, x+1), maxy = max(maxy, y+1), sum[x+1][y+1] += w;
|
||||
for (int i = 1; i <= maxx; ++i)
|
||||
{
|
||||
for (int j = 1; j <= maxy; ++j)
|
||||
{
|
||||
sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
|
||||
}
|
||||
}
|
||||
if (r >= maxx && r >= maxy)
|
||||
{
|
||||
printf("%d\n", sum[maxx][maxy]);
|
||||
return 0;
|
||||
}
|
||||
int ans = 0;
|
||||
for (int i = 1; i <= maxx; ++i)
|
||||
{
|
||||
for (int j = 1; j <= maxy; ++j)
|
||||
{
|
||||
ans = max(ans, query(i, j));
|
||||
}
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// 100
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
int n;
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
long long x = 0, y = 0;
|
||||
long long a, pre = 0;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
scanf("%lld", &a);
|
||||
if (i == 1)
|
||||
pre = a;
|
||||
long long cha = a - pre;
|
||||
if (cha > 0)
|
||||
x += cha;
|
||||
else
|
||||
y -= cha;
|
||||
pre = a;
|
||||
}
|
||||
printf("%lld\n%lld\n", max(x, y), abs(x - y) + 1);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
1 1 2 2
|
||||
1 0 1 0
|
||||
*/
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
// 101
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, p, h, m;
|
||||
int cha[5005];
|
||||
set<pair<int, int>> can;
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d%d%d", &n, &p, &h, &m);
|
||||
for (int a, b, i = 1; i <= m; ++i)
|
||||
{
|
||||
scanf("%d%d", &a, &b);
|
||||
if (a > b)
|
||||
swap(a, b);
|
||||
can.insert({a, b});
|
||||
}
|
||||
cha[1] = h;
|
||||
cha[n + 1] = -h;
|
||||
for (pair<int, int> k : can)
|
||||
{
|
||||
cha[k.first + 1]--;
|
||||
cha[k.second]++;
|
||||
}
|
||||
int now = cha[0];
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
now += cha[i];
|
||||
printf("%d\n", now);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
// 102
|
||||
// https://www.luogu.com.cn/article/42ly2wrn
|
||||
#include <bits/stdc++.h>
|
||||
#define int long long
|
||||
using namespace std;
|
||||
int n, l;
|
||||
int sum[100005];
|
||||
int q[100005];
|
||||
int t, h;
|
||||
double calc(int x, int y)
|
||||
{
|
||||
return (0.0 + sum[y] - sum[x]) / (y - x);
|
||||
}
|
||||
signed main()
|
||||
{
|
||||
scanf("%lld%lld", &n, &l);
|
||||
for (int i = 1, x; i <= n; ++i)
|
||||
scanf("%lld", &x), sum[i] = sum[i - 1] + x;
|
||||
double ans = 0;
|
||||
for (int i = l; i <= n; ++i) // lp
|
||||
{
|
||||
while (t - h >= 2 && calc(i - l, q[t - 1]) < calc(i - l, q[t - 2]))
|
||||
t--;
|
||||
q[t++] = i - l;
|
||||
while (t - h >= 2 && calc(i, q[h]) < calc(i, q[h + 1]))
|
||||
h++;
|
||||
ans = max(ans, calc(i, q[h]));
|
||||
}
|
||||
printf("%lld\n", (long long)floor(1000 * ans));
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
连续子序列平均值就转化为S-x平面上的斜率:avg(x,y)=(S(y)-s(x-1))/(y-x+1)
|
||||
*/
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
// 113
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
// --- down for luogu ---
|
||||
// bool compare(int a, int b)
|
||||
// {
|
||||
// cout << "? " << a << ' ' << b << endl;
|
||||
// bool t;
|
||||
// cin >> t;
|
||||
// return t;
|
||||
// }
|
||||
// --- up for luogu ---
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
vector<int> tmp, a;
|
||||
int tot = 0;
|
||||
void ms(int l, int r)
|
||||
{
|
||||
if (l == r)
|
||||
return;
|
||||
int mid = l + r >> 1;
|
||||
ms(l, mid);
|
||||
ms(mid + 1, r);
|
||||
int i = l, j = mid + 1, tot = l;
|
||||
while (i <= mid && j <= r)
|
||||
if (compare(a[i], a[j]))
|
||||
tmp[tot++] = a[i++];
|
||||
else
|
||||
tmp[tot++] = a[j++];
|
||||
while (i <= mid)
|
||||
tmp[tot++] = a[i++];
|
||||
while (j <= r)
|
||||
tmp[tot++] = a[j++];
|
||||
for (int k = l; k <= r; ++k)
|
||||
a[k] = tmp[k];
|
||||
}
|
||||
vector<int> specialSort(int N)
|
||||
{
|
||||
a.resize(1005);
|
||||
tmp.resize(1005);
|
||||
for (int i = 1; i <= N; ++i)
|
||||
{
|
||||
a[i] = i;
|
||||
}
|
||||
ms(1, N);
|
||||
vector<int> res(N);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
res[i] = a[i + 1];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
// --- down for luogu ---
|
||||
// int main()
|
||||
// {
|
||||
// int n;
|
||||
// scanf("%d", &n);
|
||||
// Solution k;
|
||||
// vector<int> ans = k.specialSort(n);
|
||||
// printf("! ");
|
||||
// for (int i = 0; i < n; ++i)
|
||||
// {
|
||||
// printf("%d ", ans[i]);
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
// --- up for luogu ---
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
// 103
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, m; // 200000
|
||||
int a[200005], b[200005], c[200005]; // bi!=ci
|
||||
map<int, int> hui;
|
||||
pair<pair<int, int>, int> happy[200005];
|
||||
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b)
|
||||
{
|
||||
if (a.first.first != b.first.first)
|
||||
{
|
||||
return a.first.first > b.first.first;
|
||||
}
|
||||
else
|
||||
return a.first.second > b.first.second;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
scanf("%d", &a[i]);
|
||||
hui[a[i]]++;
|
||||
}
|
||||
scanf("%d", &m);
|
||||
for (int i = 1; i <= m; ++i)
|
||||
scanf("%d", &b[i]), happy[i].first.first = hui[b[i]], happy[i].second = i;
|
||||
for (int i = 1; i <= m; ++i)
|
||||
scanf("%d", &c[i]), happy[i].first.second = hui[c[i]];
|
||||
sort(happy + 1, happy + m + 1, cmp);
|
||||
printf("%d\n", happy[1].second);
|
||||
return 0;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// 104
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n;
|
||||
int a[100005];
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
sort(a + 1, a + n + 1);
|
||||
int mid = (n % 2) ? a[n + 1 >> 1] : (a[n >> 1] + a[(n >> 1) + 1]) >> 1;
|
||||
int ans = 0;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
ans += abs(a[i] - mid);
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// 5579
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
int t;
|
||||
int pow(int a, int b, int p)
|
||||
{
|
||||
a %= p;
|
||||
int res = 1;
|
||||
while (b > 0)
|
||||
{
|
||||
if (b % 2)
|
||||
res *= a, res %= p;
|
||||
a = a * a % p,b>>=1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &t);
|
||||
while (t--)
|
||||
{
|
||||
int m, h;
|
||||
scanf("%d%d", &m, &h);
|
||||
int ans = 0;
|
||||
for (int i = 1; i <= h; ++i)
|
||||
{
|
||||
int a, b;
|
||||
scanf("%d%d", &a, &b);
|
||||
ans += pow(a, b, m);
|
||||
ans %= m;
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
// 105
|
||||
#include <bits/stdc++.h>
|
||||
#define int long long
|
||||
using namespace std;
|
||||
int roww[100005], coll[100005];
|
||||
int crow[100005], ccol[100005];
|
||||
int n, m, t;
|
||||
int row()
|
||||
{
|
||||
int v = 0, sum = 0;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
sum += roww[i];
|
||||
if (sum % n)
|
||||
return -1;
|
||||
v = sum / n;
|
||||
for (int i = 1; i <= n; i++)
|
||||
crow[i] = crow[i - 1] + roww[i] - v;
|
||||
crow[1] = crow[n] + roww[1] - v;
|
||||
sort(crow + 1, crow + 1 + n);
|
||||
int ans = 0, mi = crow[(n + 1) / 2];
|
||||
for (int i = 1; i <= n; i++)
|
||||
ans += abs(mi - crow[i]);
|
||||
return ans;
|
||||
}
|
||||
int col()
|
||||
{
|
||||
int v = 0, sum = 0;
|
||||
for (int i = 1; i <= m; ++i)
|
||||
sum += coll[i];
|
||||
if (sum % m)
|
||||
return -1;
|
||||
v = sum / m;
|
||||
for (int i = 1; i <= m; i++)
|
||||
ccol[i] = ccol[i - 1] + coll[i] - v;
|
||||
ccol[1] = ccol[m] + coll[1] - v;
|
||||
sort(ccol + 1, ccol + 1 + m);
|
||||
int ans = 0, mi = ccol[(m + 1) / 2];
|
||||
for (int i = 1; i <= m; i++)
|
||||
ans += abs(mi - ccol[i]);
|
||||
return ans;
|
||||
}
|
||||
signed main()
|
||||
{
|
||||
scanf("%lld%lld%lld", &n, &m, &t);
|
||||
for (int i = 1; i <= t; ++i)
|
||||
{
|
||||
int x, y;
|
||||
scanf("%lld%lld", &x, &y);
|
||||
roww[x]++;
|
||||
coll[y]++;
|
||||
}
|
||||
int rrw = row(), rco = col();
|
||||
if (rrw == -1)
|
||||
if (rco == -1)
|
||||
printf("impossible\n");
|
||||
else
|
||||
printf("column %lld\n", rco);
|
||||
else if (rco == -1)
|
||||
printf("row %lld\n", rrw);
|
||||
else
|
||||
printf("both %lld\n", rco + rrw);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// 106
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n;
|
||||
int a[100005], ord[100005], pos[100005];
|
||||
int pre[100005], nxt[100005];
|
||||
int ans[100005];
|
||||
int t;
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &t);
|
||||
while (t--)
|
||||
{
|
||||
int ll;
|
||||
scanf("%d %d", &ll, &n);
|
||||
printf("%d %d\n", ll, (n + 1) >> 1);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
scanf("%d", &a[i]), ord[i] = i;
|
||||
sort(ord + 1, ord + n + 1, [](int x, int y)
|
||||
{ return a[x] != a[y] ? a[x] < a[y] : x < y; });
|
||||
for (int rank = 1; rank <= n; ++rank)
|
||||
{
|
||||
int x = ord[rank];
|
||||
pos[x] = rank, pre[x] = (rank == 1 ? 0 : ord[rank - 1]), nxt[x] = (rank == n ? 0 : ord[rank + 1]);
|
||||
}
|
||||
int mid = ord[n / 2 + 1], cnt = 0;
|
||||
for (int len = n; len >= 1; --len)
|
||||
{
|
||||
if (len & 1)
|
||||
ans[++cnt] = a[mid];
|
||||
if (len == 1)
|
||||
break;
|
||||
int x = len;
|
||||
if (len & 1)
|
||||
{
|
||||
if (pos[x] <= pos[mid])
|
||||
mid = nxt[mid];
|
||||
}
|
||||
else if (pos[x] >= pos[mid])
|
||||
mid = pre[mid];
|
||||
if (pre[x])
|
||||
nxt[pre[x]] = nxt[x];
|
||||
if (nxt[x])
|
||||
pre[nxt[x]] = pre[x];
|
||||
}
|
||||
for (int i = cnt; i >= 1; --i)
|
||||
printf("%d%c", ans[i], ((cnt - i + 1) % 10) ? ' ' : '\n');
|
||||
if (cnt % 10)
|
||||
puts("");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
// 107
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n;
|
||||
int a[500005], tmp[500005];
|
||||
long long ms(int l, int r)
|
||||
{
|
||||
if (l == r)
|
||||
return 0;
|
||||
int mid = l + r >> 1;
|
||||
long long ans = 0;
|
||||
ans = ms(l, mid) + ms(mid + 1, r);
|
||||
int i = l, j = mid + 1, tot = l;
|
||||
while (i <= mid && j <= r)
|
||||
{
|
||||
if (a[i] > a[j])
|
||||
tmp[tot++] = a[j++], ans+=j-tot;
|
||||
else
|
||||
tmp[tot++] = a[i++];
|
||||
}
|
||||
while (i <= mid)
|
||||
tmp[tot++] = a[i++];
|
||||
while (j <= r)
|
||||
tmp[tot++] = a[j++];
|
||||
for (int i = l; i <= r; ++i)
|
||||
a[i] = tmp[i];
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
while (n != 0)
|
||||
{
|
||||
for (int i = 1; i <= n; ++i)
|
||||
scanf("%d", &a[i]);
|
||||
printf("%lld\n", ms(1, n));
|
||||
scanf("%d", &n);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
// 108
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n;
|
||||
vector<int> a;
|
||||
int tmp[250005];
|
||||
int c[505][505];
|
||||
long long ms(int l, int r)
|
||||
{
|
||||
if (l == r)
|
||||
return 0;
|
||||
int mid = l + r >> 1;
|
||||
long long ans = 0;
|
||||
ans = ms(l, mid) + ms(mid + 1, r);
|
||||
int i = l, j = mid + 1, tot = l;
|
||||
while (i <= mid && j <= r)
|
||||
{
|
||||
if (a[i] > a[j])
|
||||
tmp[tot++] = a[j++], ans += tot - j;
|
||||
else
|
||||
tmp[tot++] = a[i++];
|
||||
}
|
||||
while (i <= mid)
|
||||
tmp[tot++] = a[i++];
|
||||
while (j <= r)
|
||||
tmp[tot++] = a[j++];
|
||||
for (int i = l; i <= r; ++i)
|
||||
a[i] = tmp[i];
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while (cin >> n)
|
||||
{
|
||||
a.clear();
|
||||
a.push_back(0);
|
||||
memset(tmp, 0, sizeof tmp);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
for (int j = 1; j <= n; ++j)
|
||||
scanf("%d", &c[i][j]);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
if (i % 2)
|
||||
{
|
||||
for (int j = 1; j <= n; ++j)
|
||||
{
|
||||
if (c[i][j] != 0)
|
||||
a.push_back(c[i][j]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int j = n; j >= 1; --j)
|
||||
{
|
||||
if (c[i][j] != 0)
|
||||
a.push_back(c[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
long long ans1 = ms(1, n * n - 1);
|
||||
a.clear();
|
||||
a.push_back(0);
|
||||
memset(tmp, 0, sizeof tmp);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
for (int j = 1; j <= n; ++j)
|
||||
scanf("%d", &c[i][j]);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
if (i % 2)
|
||||
{
|
||||
for (int j = 1; j <= n; ++j)
|
||||
{
|
||||
if (c[i][j] != 0)
|
||||
a.push_back(c[i][j]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int j = n; j >= 1; --j)
|
||||
{
|
||||
if (c[i][j] != 0)
|
||||
a.push_back(c[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
long long ans2 = ms(1, n * n - 1);
|
||||
if (ans1 % 2 == ans2 % 2)
|
||||
puts("TAK");
|
||||
else
|
||||
puts("NIE");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
int T, n, m;
|
||||
long long k;
|
||||
int p[500005], t[500005], lg2[500005];
|
||||
template <typename TT>
|
||||
inline TT read()
|
||||
{
|
||||
TT x = 0;
|
||||
bool w = true;
|
||||
register char ch = getchar();
|
||||
while (ch < '0' || ch > '9')
|
||||
{
|
||||
if (ch == '-')
|
||||
w = false;
|
||||
ch = getchar();
|
||||
}
|
||||
while (ch >= '0' && ch <= '9')
|
||||
x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar();
|
||||
return w ? x : -x;
|
||||
}
|
||||
template <typename TT>
|
||||
inline void put(TT x)
|
||||
{
|
||||
if (x < 0)
|
||||
putchar('-'), x = -x;
|
||||
if (x > 9)
|
||||
put(x / 10);
|
||||
putchar(x % 10 + '0');
|
||||
}
|
||||
|
||||
inline bool pd(const int &L, const int &R)
|
||||
{
|
||||
if (R > n)
|
||||
return false;
|
||||
for (register int i = L; i <= R; i++)
|
||||
t[i] = p[i];
|
||||
long long sum = 0;
|
||||
sort(t + L, t + R + 1);
|
||||
int l = L, r = R, cnt = 0;
|
||||
while (l < r && sum <= k && ++cnt <= m)
|
||||
sum += 1ll * (t[r] - t[l]) * (t[r] - t[l]), l++, r--;
|
||||
return sum <= k;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
for (register int i = 2; i <= 5e5; i++)
|
||||
lg2[i] = lg2[i >> 1] + 1;
|
||||
T = read<int>();
|
||||
while (T--)
|
||||
{
|
||||
n = read<int>(), m = read<int>(), k = read<long long>();
|
||||
for (register int i = 1; i <= n; i++)
|
||||
p[i] = read<int>();
|
||||
int ans = 0;
|
||||
int l = 1, r = l, flag = 1;
|
||||
while (r <= n)
|
||||
{
|
||||
if (!flag)
|
||||
ans++, l = r + 1, r = l;
|
||||
if (pd(l, r + (1 << flag) - 1))
|
||||
r = r + (1 << flag) - 1, flag++;
|
||||
else
|
||||
flag--;
|
||||
}
|
||||
put(ans);
|
||||
puts("");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// 1055
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
int n;
|
||||
int a[100005];
|
||||
long long f[100005][2];
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
f[1][1] = -a[1];
|
||||
for (int i = 2; i <= n; ++i)
|
||||
{
|
||||
f[i][1] = max(f[i - 1][0] - a[i], f[i - 1][1]);
|
||||
f[i][0] = max(f[i - 1][0], f[i - 1][1] + a[i]);
|
||||
}
|
||||
printf("%d\n", max(f[n][0], f[n][1]));
|
||||
return 0;
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
// 110
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, m;
|
||||
pair<int, int> s[2505];
|
||||
pair<int, int> spf[2505];
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d", &n, &m);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
scanf("%d%d", &s[i].first, &s[i].second); // min max
|
||||
sort(s + 1, s + n + 1, [](pair<int, int> x, pair<int, int> y)
|
||||
{ return x.second > y.second; }); // 按 max 降序
|
||||
for (int i = 1; i <= m; ++i)
|
||||
scanf("%d%d", &spf[i].first, &spf[i].second);
|
||||
sort(spf + 1, spf + m + 1, [](pair<int, int> x, pair<int, int> y)
|
||||
{ return x.first > y.first; }); // 按 spf 降序
|
||||
|
||||
int point = 1;
|
||||
priority_queue<pair<int, int>> q; // 按 min 降序
|
||||
int ans = 0;
|
||||
for (int i = 1; i <= m; ++i)
|
||||
{
|
||||
while (point <= n && s[point].second >= spf[i].first)
|
||||
q.push(s[point++]);
|
||||
while (!q.empty() && q.top().first > spf[i].first)
|
||||
q.pop();
|
||||
while (!q.empty() && spf[i].second > 0)
|
||||
q.pop(), spf[i].second--, ans++;
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
// 111
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n;
|
||||
struct node
|
||||
{
|
||||
int l, r, index;
|
||||
const bool operator<(const node &b) const
|
||||
{
|
||||
return r > b.r;
|
||||
}
|
||||
} cow[50005];
|
||||
bool used[50005];
|
||||
int be[50005];
|
||||
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
int maxR = 0;
|
||||
for (int i = 1; i <= n; i++)
|
||||
scanf("%d%d", &cow[i].l, &cow[i].r), cow[i].index = i, maxR = max(maxR, cow[i].r);
|
||||
sort(cow + 1, cow + n + 1, [](const node &a, const node &b)
|
||||
{ return a.l < b.l; });
|
||||
int tot = 1, nx = 1;
|
||||
priority_queue<node> q;
|
||||
for (int t = 1; t <= maxR; t++)
|
||||
{
|
||||
while (!q.empty() && q.top().r < t)
|
||||
used[be[q.top().index]] = 0, q.pop();
|
||||
while (tot <= n && cow[tot].l <= t)
|
||||
{
|
||||
int stall = -1;
|
||||
for (int j = 1; j < nx; j++)
|
||||
if (!used[j])
|
||||
{
|
||||
stall = j;
|
||||
break;
|
||||
}
|
||||
if (stall == -1)
|
||||
stall = nx++;
|
||||
used[stall] = 1;
|
||||
be[cow[tot].index] = stall;
|
||||
q.push(cow[tot]);
|
||||
tot++;
|
||||
}
|
||||
}
|
||||
printf("%d\n", nx - 1);
|
||||
for (int i = 1; i <= n; i++)
|
||||
printf("%d\n", be[i]);
|
||||
return 0;
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
// 112
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, d;
|
||||
struct node
|
||||
{
|
||||
int x, y;
|
||||
double xl, xr;
|
||||
} is[1005];
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d", &n, &d);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
scanf("%d%d", &is[i].x, &is[i].y);
|
||||
if (abs(is[i].y) > d)
|
||||
{
|
||||
puts("-1");
|
||||
return 0;
|
||||
}
|
||||
double len = sqrt(1.0 * d * d - 1.0 * is[i].y * is[i].y);
|
||||
is[i].xl = is[i].x - len, is[i].xr = is[i].x + len;
|
||||
}
|
||||
sort(is + 1, is + n + 1, [](const node &x, const node &y)
|
||||
{ return x.xr < y.xr; });
|
||||
int ans = 0;
|
||||
double now = -DBL_MAX;
|
||||
const double eps = 1e-10;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
if (now + eps < is[i].xl)
|
||||
now = is[i].xr, ++ans;
|
||||
printf("%d\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
// 114
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n;
|
||||
pair<int, int> lr[1005];
|
||||
struct gjd
|
||||
{
|
||||
int a[4005], len;
|
||||
void operator=(int rhs)
|
||||
{
|
||||
len = 0;
|
||||
if (rhs == 0)
|
||||
{
|
||||
len = 1;
|
||||
return;
|
||||
}
|
||||
while (rhs)
|
||||
a[++len] = rhs % 10000, rhs /= 10000;
|
||||
return;
|
||||
}
|
||||
void operator=(const gjd rhs)
|
||||
{
|
||||
memcpy(a, rhs.a, sizeof(rhs.a));
|
||||
len = rhs.len;
|
||||
return;
|
||||
}
|
||||
void operator*=(const int rhs)
|
||||
{
|
||||
for (int i = 1; i <= len; i++)
|
||||
a[i] *= rhs;
|
||||
for (int i = 1; i <= len; i++)
|
||||
{
|
||||
a[i + 1] += a[i] / 10000, a[i] %= 10000;
|
||||
if (i + 1 > len && a[i + 1])
|
||||
len++;
|
||||
}
|
||||
while (len && a[len] == 0)
|
||||
len--;
|
||||
}
|
||||
gjd operator/(const int rhs)
|
||||
{
|
||||
gjd c;
|
||||
c = *this;
|
||||
while (c.len && c.a[c.len] == 0)
|
||||
c.len--;
|
||||
for (int i = c.len; i; i--)
|
||||
{
|
||||
c.a[i - 1] += (c.a[i] % rhs) * 10000;
|
||||
c.a[i] /= rhs;
|
||||
}
|
||||
while (c.len && c.a[c.len] == 0)
|
||||
c.len--;
|
||||
return c;
|
||||
}
|
||||
void print()
|
||||
{
|
||||
while (len && a[len] == 0)
|
||||
len--;
|
||||
if (len == 0)
|
||||
{
|
||||
putchar('0');
|
||||
return;
|
||||
}
|
||||
printf("%d", a[len]);
|
||||
for (int i = len - 1; i; i--)
|
||||
printf("%04d", a[i]);
|
||||
}
|
||||
bool operator>(const gjd &rhs) const
|
||||
{
|
||||
if (len != rhs.len)
|
||||
return len > rhs.len;
|
||||
for (int i = len; i; i--)
|
||||
if (a[i] != rhs.a[i])
|
||||
return a[i] > rhs.a[i];
|
||||
return 0;
|
||||
}
|
||||
} ma, now, tmp;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
scanf("%d%d%d", &n, &lr[0].first, &lr[0].second);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
scanf("%d%d", &lr[i].first, &lr[i].second);
|
||||
sort(lr + 1, lr + n + 1, [](pair<int, int> x, pair<int, int> y)
|
||||
{ return x.first * x.second < y.first * y.second; });
|
||||
now = lr[0].first;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
tmp = now / lr[i].second;
|
||||
if (tmp > ma)
|
||||
ma = tmp;
|
||||
now *= lr[i].first;
|
||||
}
|
||||
ma.print();
|
||||
return 0;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
// 90
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
long long a, b, p;
|
||||
void put(__int128 a)
|
||||
{
|
||||
char c[60];
|
||||
if (a == 0)
|
||||
puts("0");
|
||||
int tot = 0;
|
||||
while (a > 0)
|
||||
{
|
||||
c[tot++] = a % 10 + '0';
|
||||
a /= 10;
|
||||
}
|
||||
for (int i = tot - 1; i >= 0; --i)
|
||||
{
|
||||
putchar(c[i]);
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
cin >> a >> b >> p;
|
||||
__int128 ans = ((__int128)a % p) * (b % p) % p;
|
||||
put(ans);
|
||||
return 0;
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
// 115 not finished
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n;
|
||||
int a[1005];
|
||||
int fa[1005];
|
||||
int dt[1005];
|
||||
int root;
|
||||
vector<int> chi[1005];
|
||||
bool ran[1005];
|
||||
int ans, T;
|
||||
void dfs(int now) // 染色到dt[now]
|
||||
{
|
||||
if (dt[now] == root)
|
||||
{
|
||||
ran[now] = 1, ans += (T++) * a[dt[now]];
|
||||
return;
|
||||
}
|
||||
if (!ran[fa[dt[now]]])
|
||||
dfs(fa[dt[now]]);
|
||||
ran[now] = 1, ans += (T++) * a[dt[now]];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
scanf("%d", &a[i]), dt[i] = i;
|
||||
sort(dt + 1, dt + n + 1, [](int x, int y)
|
||||
{ return a[x] < a[y]; });
|
||||
for (int u, v, i = 1; i < n; ++i)
|
||||
scanf("%d%d", &u, &v),
|
||||
fa[v] = u, chi[u].push_back(v);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
if (fa[i] == i)
|
||||
{
|
||||
root = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
dfs(i);
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
// 116
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
bool dddd[5][5];
|
||||
bool hang[5];
|
||||
bool lie[5];
|
||||
bool chosen[5][5];
|
||||
vector<pair<int, int>> cc, anss;
|
||||
void in()
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
string s;
|
||||
cin >> s;
|
||||
for (int j = 0; j < 4; ++j)
|
||||
dddd[i][j] = (s[j] == '+');
|
||||
}
|
||||
}
|
||||
void dfs(int x, int y)
|
||||
{
|
||||
if (x == 4)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
for (int j = 0; j < 4; ++j)
|
||||
{
|
||||
if (hang[i] ^ lie[j] ^ dddd[i][j] ^ chosen[i][j])
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (anss.empty() || (int)cc.size() < anss.size())
|
||||
{
|
||||
anss = cc;
|
||||
}
|
||||
return;
|
||||
}
|
||||
hang[x] = !hang[x], lie[y] = !lie[y], chosen[x][y] = 1;
|
||||
cc.push_back({x, y});
|
||||
dfs((y == 3) ? x + 1 : x, (y == 3) ? 0 : y + 1);
|
||||
hang[x] = !hang[x], lie[y] = !lie[y], chosen[x][y] = 0;
|
||||
cc.pop_back();
|
||||
dfs((y == 3) ? x + 1 : x, (y == 3) ? 0 : y + 1);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
in();
|
||||
dfs(0, 0);
|
||||
printf("%d\n", anss.size());
|
||||
for (int i = 0; i < (int)anss.size(); ++i)
|
||||
{
|
||||
printf("%d %d\n", anss[i].first + 1, anss[i].second + 1);
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
// 117
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
map<char, int> m;
|
||||
char a[20][10];
|
||||
int cnt = 1, ans;
|
||||
bool vis[20][10];
|
||||
int trans(char tmp)
|
||||
{
|
||||
switch (tmp)
|
||||
{
|
||||
case 'A':
|
||||
return 1;
|
||||
case '0':
|
||||
return 10;
|
||||
case 'J':
|
||||
return 11;
|
||||
case 'Q':
|
||||
return 12;
|
||||
}
|
||||
return tmp - '0';
|
||||
}
|
||||
int main()
|
||||
{
|
||||
for (int i = 1; i <= 13; i++)
|
||||
for (int j = 1; j <= 4; j++)
|
||||
cin >> a[i][j];
|
||||
while (cnt <= 4)
|
||||
{
|
||||
char tmp = a[13][cnt++];
|
||||
while (tmp != 'K')
|
||||
{
|
||||
int t = trans(tmp);
|
||||
for (int i = 4; i >= 1; i--)
|
||||
{
|
||||
if (!vis[t][i])
|
||||
{
|
||||
vis[t][i] = true, m[tmp]++;
|
||||
if (m[tmp] == 4 && tmp != 'K')
|
||||
ans++;
|
||||
tmp = a[t][i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
t = trans(tmp);
|
||||
}
|
||||
}
|
||||
cout << ans;
|
||||
return 0;
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
// 118
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n;
|
||||
bool a[1000][1000];
|
||||
void cal(int n, int x, int y)
|
||||
{
|
||||
if (n == 1)
|
||||
{
|
||||
a[x][y] = 1;
|
||||
return;
|
||||
}
|
||||
/*
|
||||
X X X X
|
||||
X X
|
||||
X X X X
|
||||
X X
|
||||
X
|
||||
X X
|
||||
X X X X
|
||||
X X
|
||||
X X X X
|
||||
*/
|
||||
int m = pow(3, n - 2);
|
||||
cal(n - 1, x, y);
|
||||
cal(n - 1, x, y + 2*m);
|
||||
cal(n - 1, x + m, y+m);
|
||||
cal(n - 1, x + 2 * m, y);
|
||||
cal(n - 1, x + 2 * m, y + 2*m);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while (cin >> n)
|
||||
{
|
||||
memset(a, 0, sizeof a);
|
||||
if (n < 0)
|
||||
break;
|
||||
cal(n, 0, 0);
|
||||
for (int i = 0; i < pow(3,n-1); ++i)
|
||||
{
|
||||
for (int j = 0; j < pow(3,n-1); ++j)
|
||||
{
|
||||
if (a[i][j] == 1)
|
||||
putchar('X');
|
||||
else
|
||||
putchar(' ');
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
puts("-");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
// 119
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
// 120
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int t;
|
||||
struct node
|
||||
{
|
||||
long long l, r, d;
|
||||
} a[200005];
|
||||
int n;
|
||||
int pre(long long x)
|
||||
{
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
if (x < a[i].l)
|
||||
continue;
|
||||
long long end = min(x, a[i].r);
|
||||
if (end < a[i].l)
|
||||
continue;
|
||||
long long cnt = (end - a[i].l) / a[i].d + 1;
|
||||
res ^= (cnt & 1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
long long at(long long p)
|
||||
{
|
||||
long long cnt = 0;
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
if (p < a[i].l || p > a[i].r)
|
||||
continue;
|
||||
if ((p - a[i].l) % a[i].d == 0)
|
||||
++cnt;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &t);
|
||||
while (t--)
|
||||
{
|
||||
scanf("%d", &n);
|
||||
long long maxE = 0;
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
scanf("%lld%lld%lld", &a[i].l, &a[i].r, &a[i].d);
|
||||
maxE = max(maxE, a[i].r);
|
||||
}
|
||||
if (pre(maxE) == 0)
|
||||
{
|
||||
puts("There's no weakness.");
|
||||
continue;
|
||||
}
|
||||
long long l = 0, r = maxE;
|
||||
while (l < r)
|
||||
{
|
||||
long long mid = (l + r) >> 1;
|
||||
if (pre(mid) == 1)
|
||||
r = mid;
|
||||
else
|
||||
l = mid + 1;
|
||||
}
|
||||
printf("%lld %lld\n", l, at(l));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int c, n;
|
||||
pair<int, int> cc[505];
|
||||
int tmpx[505], tmpy[505];
|
||||
int sum[505][505];
|
||||
int totx, toty;
|
||||
bool check(int len)
|
||||
{
|
||||
for (int i = 1; i <= totx; ++i)
|
||||
{
|
||||
int j = i;
|
||||
while (j <= totx && tmpx[j] - tmpx[i] <= len - 1)
|
||||
j++;
|
||||
j--;
|
||||
int l = 1;
|
||||
for (int k = 1; k <= toty; ++k)
|
||||
{
|
||||
if (l < k)
|
||||
l = k;
|
||||
while (l <= toty && tmpy[l] - tmpy[k] <= len - 1)
|
||||
l++;
|
||||
l--;
|
||||
if (l >= k)
|
||||
{
|
||||
int cnt = sum[j][l] - sum[i - 1][l] - sum[j][k - 1] + sum[i - 1][k - 1];
|
||||
if (cnt >= c)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d", &c, &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
scanf("%d%d", &cc[i].first, &cc[i].second);
|
||||
tmpx[i] = cc[i].first, tmpy[i] = cc[i].second;
|
||||
}
|
||||
sort(tmpx + 1, tmpx + n + 1);
|
||||
totx = unique(tmpx + 1, tmpx + n + 1) - (tmpx + 1);
|
||||
sort(tmpy + 1, tmpy + n + 1);
|
||||
toty = unique(tmpy + 1, tmpy + n + 1) - (tmpy + 1);
|
||||
memset(sum, 0, sizeof(sum));
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
int x = lower_bound(tmpx + 1, tmpx + totx + 1, cc[i].first) - tmpx;
|
||||
int y = lower_bound(tmpy + 1, tmpy + toty + 1, cc[i].second) - tmpy;
|
||||
sum[x][y]++;
|
||||
}
|
||||
for (int i = 1; i <= totx; ++i)
|
||||
{
|
||||
for (int j = 1; j <= toty; ++j)
|
||||
{
|
||||
sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
|
||||
}
|
||||
}
|
||||
int L = 1, R = max(tmpx[totx] - tmpx[1] + 1, tmpy[toty] - tmpy[1] + 1), ans;
|
||||
while (L <= R)
|
||||
{
|
||||
int mid = (L + R) / 2;
|
||||
if (check(mid))
|
||||
ans = mid, R = mid - 1;
|
||||
else
|
||||
L = mid + 1;
|
||||
}
|
||||
|
||||
printf("%d\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// 122
|
||||
#include <bits/stdc++.h>
|
||||
#define int long long
|
||||
using namespace std;
|
||||
int a[1000005], c[1000005];
|
||||
signed main()
|
||||
{
|
||||
int n, v = 0, sum = 0;
|
||||
scanf("%lld", &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
scanf("%lld", &a[i]), sum += a[i];
|
||||
v = sum / n;
|
||||
for (int i = 1; i <= n; i++)
|
||||
c[i] = c[i - 1] + a[i] - v;
|
||||
sort(c + 1, c + 1 + n);
|
||||
int ans = 0, mi = c[(n + 1) / 2];
|
||||
for (int i = 1; i <= n; i++)
|
||||
ans += abs(mi - c[i]);
|
||||
printf("%lld\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// 123
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, x[10005], y[10005], ans = 0, midx, midy;
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; i++)
|
||||
scanf("%d%d", x + i, y + i);
|
||||
sort(x + 1, x + n + 1);
|
||||
sort(y + 1, y + n + 1);
|
||||
for (int i = 1; i <= n; i++)
|
||||
x[i] -= i;
|
||||
sort(x + 1, x + n + 1);
|
||||
midx = (n % 2) ? x[n / 2 + 1] : (x[n / 2] + x[n / 2 + 1]) / 2;
|
||||
midy = (n % 2) ? y[n / 2 + 1] : (y[n / 2] + y[n / 2 + 1]) / 2;
|
||||
for (int i = 1; i <= n; i++)
|
||||
ans += abs(x[i] - midx) + abs(y[i] - midy);
|
||||
printf("%d\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
// 124
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
#define int long long
|
||||
int t;
|
||||
int to_num(char c)
|
||||
{
|
||||
if ('0' <= c && c <= '9')
|
||||
return c - '0';
|
||||
else if ('A' <= c && c <= 'Z')
|
||||
return c - 'A' + 10;
|
||||
else
|
||||
return c - 'a' + 36;
|
||||
}
|
||||
char to_char(int i)
|
||||
{
|
||||
if (0 <= i && i <= 9)
|
||||
return i + '0';
|
||||
else if (10 <= i && i <= 35)
|
||||
return i - 10 + 'A';
|
||||
else
|
||||
return i - 36 + 'a';
|
||||
}
|
||||
void conv(int inbase, int outbase, string s)
|
||||
{
|
||||
vector<int> n(s.size(), 0), r;
|
||||
for (int i = 0; i < s.size(); ++i)
|
||||
n[i] = to_num(s[i]);
|
||||
while (n.size())
|
||||
{
|
||||
vector<int> e(n.size(), 0);
|
||||
int yy = 0;
|
||||
for (int i = 0; i < n.size(); ++i)
|
||||
{
|
||||
int c = n[i] + yy * inbase;
|
||||
e[i] = c / outbase, yy = c % outbase;
|
||||
}
|
||||
n = e;
|
||||
r.push_back(yy);
|
||||
while (!n.empty() && n[0] == 0)
|
||||
n.erase(n.begin());
|
||||
}
|
||||
reverse(r.begin(), r.end());
|
||||
printf("%d ", inbase);
|
||||
cout << s << '\n';
|
||||
printf("%d ", outbase);
|
||||
for (int i : r)
|
||||
putchar(to_char(i));
|
||||
puts("\n");
|
||||
}
|
||||
signed main()
|
||||
{
|
||||
scanf("%lld", &t);
|
||||
while (t--)
|
||||
{
|
||||
int in, out;
|
||||
scanf("%lld%lld", &in, &out);
|
||||
string inn;
|
||||
cin >> inn;
|
||||
conv(in, out, inn);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// 91
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int f[2100000][21], w[2100000][21];
|
||||
int n;
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 0; i < n; i++)
|
||||
for (int j = 0; j < n; j++)
|
||||
scanf("%d", &w[i][j]);
|
||||
memset(f, 0x7f, sizeof f);
|
||||
f[1][0] = 0;
|
||||
for (int i = 1; i < 1 << n; i++)
|
||||
for (int j = 0; j < n; j++)
|
||||
if (i >> j & 1)
|
||||
for (int k = 0; k < n; k++)
|
||||
if (i >> k & 1)
|
||||
f[i][j] = min(f[i][j], f[i - (1 << j)][k] + w[k][j]);
|
||||
cout << f[(1 << n) - 1][n - 1] << endl;
|
||||
return 0;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// 125
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int w, s;
|
||||
} a[50005];
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
scanf("%d%d", &a[i].w, &a[i].s);
|
||||
sort(a + 1, a + 1 + n, [](node x, node y)
|
||||
{ return x.w + x.s < y.w + y.s; });
|
||||
int tot = 0, ans = -2147483648;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
ans = max(ans, tot - a[i].s);
|
||||
tot += a[i].w;
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// 126
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, m, k, a[105][105], sum[105][105];
|
||||
int query(int xx1, int yy1, int xx2, int yy2)
|
||||
{
|
||||
return sum[xx2][yy2] - sum[xx1 - 1][yy2] - sum[xx2][yy1 - 1] + sum[xx1 - 1][yy1 - 1];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
for (int j = 1; j <= n; ++j)
|
||||
scanf("%d", &a[i][j]), sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
|
||||
int ans = -2147482648;
|
||||
for (int xx1 = 1; xx1 <= n; ++xx1)
|
||||
for (int yy1 = 1; yy1 <= n; ++yy1)
|
||||
for (int xx2 = xx1; xx2 <= n; ++xx2)
|
||||
for (int yy2 = yy1; yy2 <= n; ++yy2)
|
||||
if (query(xx1, yy1, xx2, yy2) > ans)
|
||||
ans = query(xx1, yy1, xx2, yy2);
|
||||
printf("%d", ans);
|
||||
return 0;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// 126
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, m, k, a[105][105], sum[105][105];
|
||||
int query(int xx1, int yy1, int xx2, int yy2)
|
||||
{
|
||||
return sum[xx2][yy2] - sum[xx1 - 1][yy2] - sum[xx2][yy1 - 1] + sum[xx1 - 1][yy1 - 1];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
for (int j = 1; j <= n; ++j)
|
||||
scanf("%d", &a[i][j]), sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
|
||||
int ans = -2147482648;
|
||||
for (int xx1 = 1; xx1 <= n; ++xx1)
|
||||
for (int yy1 = 1; yy1 <= n; ++yy1)
|
||||
for (int xx2 = xx1; xx2 <= n; ++xx2)
|
||||
for (int yy2 = yy1; yy2 <= n; ++yy2)
|
||||
if (query(xx1, yy1, xx2, yy2) > ans)
|
||||
ans = query(xx1, yy1, xx2, yy2);
|
||||
printf("%d", ans);
|
||||
return 0;
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
// 127
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, m;
|
||||
pair<int, int> rob[100005], task[100005];
|
||||
int c[105];
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d", &n, &m);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
scanf("%d%d", &rob[i].first, &rob[i].second);
|
||||
for (int i = 1; i <= m; ++i)
|
||||
scanf("%d%d", &task[i].first, &task[i].second);
|
||||
sort(rob + 1, rob + n + 1, [](pair<int, int> a, pair<int, int> b)
|
||||
{ return (a.first == b.first) ? a.second > b.second : a.first > b.first; });
|
||||
sort(task + 1, task + m + 1, [](pair<int, int> a, pair<int, int> b)
|
||||
{ return (a.first == b.first) ? a.second > b.second : a.first > b.first; });
|
||||
int p = 0;
|
||||
int cnt = 0;
|
||||
long long ans = 0;
|
||||
for (int i = 1; i <= m; ++i)
|
||||
{
|
||||
while (p < n && rob[p + 1].first >= task[i].first)
|
||||
p++, c[rob[p].second]++;
|
||||
for (int j = task[i].second; j <= 100; j++)
|
||||
if (c[j])
|
||||
{
|
||||
cnt++, c[j]--, ans += task[i].first * 500 + task[i].second * 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("%d %lld\n", cnt, ans);
|
||||
return 0;
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
// 998
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int n, m, ans, t[100005], op[100005];
|
||||
char str[4];
|
||||
bool calc(bool x, int j)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
if (op[i] == 1)
|
||||
x |= t[i] >> j & 1;
|
||||
else if (op[i] == 2)
|
||||
x ^= t[i] >> j & 1;
|
||||
else
|
||||
x &= t[i] >> j & 1;
|
||||
return x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d %d", &n, &m);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
scanf("\n%s %d", str, t + i);
|
||||
if (str[0] == 'O')
|
||||
op[i] = 1;
|
||||
else if (str[0] == 'X')
|
||||
op[i] = 2;
|
||||
else
|
||||
op[i] = 3;
|
||||
}
|
||||
for (int i = 29; ~i; i--)
|
||||
if (1 << i <= m)
|
||||
{
|
||||
bool x = calc(0, i), y = calc(1, i);
|
||||
if (x >= y)
|
||||
ans |= x << i;
|
||||
else
|
||||
ans |= y << i, m -= 1 << i;
|
||||
}
|
||||
else
|
||||
ans |= calc(0, i) << i;
|
||||
printf("%d\n", ans);
|
||||
return 0;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
// 92
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
bool ch[20];
|
||||
int n;
|
||||
void dfs(int now)
|
||||
{
|
||||
if (now > n)
|
||||
{
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
if (ch[i])
|
||||
printf("%d ", i);
|
||||
}
|
||||
puts("");
|
||||
return;
|
||||
}
|
||||
ch[now]=0;
|
||||
dfs(now + 1);
|
||||
ch[now]=1;
|
||||
dfs(now + 1);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
dfs(1);
|
||||
return 0;
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// 93
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
bool ch[50];
|
||||
int n, m;
|
||||
void dfs(int count, int now)
|
||||
{
|
||||
if (now > n)
|
||||
{
|
||||
if (count != m)
|
||||
return;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
if (ch[i])
|
||||
printf("%d ", i);
|
||||
}
|
||||
puts("");
|
||||
return;
|
||||
}
|
||||
ch[now] = 1;
|
||||
dfs(count + 1, now + 1);
|
||||
ch[now] = 0;
|
||||
dfs(count, now + 1);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d", &n, &m);
|
||||
dfs(0, 1);
|
||||
return 0;
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
// 94
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int N = 20;
|
||||
int n;
|
||||
bool st[N]; // 数字是否用过
|
||||
int path[N]; // 保存数列
|
||||
|
||||
void dfs(int cnt)
|
||||
{
|
||||
if (cnt > n)
|
||||
{
|
||||
for (int i = 1; i <= n; i++)
|
||||
printf("%d ", path[i]);
|
||||
puts("");
|
||||
return;
|
||||
}
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
if (!st[i])
|
||||
{
|
||||
path[cnt] = i, st[i] = 1;
|
||||
dfs(cnt + 1);
|
||||
path[cnt] = 0, st[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
dfs(1);
|
||||
return 0;
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
// 95
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int t;
|
||||
int a[6];
|
||||
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &t);
|
||||
while (t--)
|
||||
{
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
a[i] = 0;
|
||||
for (int j = 0; j < 5;)
|
||||
{
|
||||
int c = getchar();
|
||||
if (c == '0' || c == '1')
|
||||
{
|
||||
if (c == '0')
|
||||
a[i] |= 1 << (4 - j);
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
int ans = 7;
|
||||
for (int fline = 0; fline < 32; ++fline)
|
||||
{
|
||||
int noww[5];
|
||||
for (int i = 0; i < 5; ++i)
|
||||
noww[i] = a[i];
|
||||
int pp[5] = {0};
|
||||
pp[0] = fline;
|
||||
noww[0] ^= pp[0];
|
||||
noww[0] ^= (pp[0] << 1) & 0x1F;
|
||||
noww[0] ^= (pp[0] >> 1);
|
||||
noww[1] ^= pp[0];
|
||||
for (int r = 1; r < 5; ++r)
|
||||
{
|
||||
pp[r] = noww[r - 1];
|
||||
noww[r] ^= pp[r];
|
||||
noww[r] ^= (pp[r] << 1) & 0x1F;
|
||||
noww[r] ^= (pp[r] >> 1);
|
||||
noww[r - 1] ^= pp[r];
|
||||
if (r < 4)
|
||||
noww[r + 1] ^= pp[r];
|
||||
}
|
||||
|
||||
bool flag = true;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
if (noww[i] != 0)
|
||||
{
|
||||
flag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
cnt += __builtin_popcount(pp[i]);
|
||||
if (cnt < ans)
|
||||
ans = cnt;
|
||||
}
|
||||
}
|
||||
if (ans > 6)
|
||||
printf("-1\n");
|
||||
else
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user