A: Fermat’s Optimization Problem
Description
Consider the error function , where means the absolute value of . Given two positive integers and , our problem is to find two positive integers and such that and the error value is minimized.
For example, if we are given and then the solution is: . This solution yields the error value .
Solution
Because there are multiple test cases, we can first calculate all for and . Then we can derive them when needed with complexity.
Given and , we can first enumerate and calculate . For each binary search and find the nearest and calculate the error value. Time complexity is .
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,ansx,ansy;
int ans[52];
int a[52];
int b[52];
int c[52];
int d[52];
int e[52];
int first[100001][11][52];
void work0()
{
for (int i = 1;i <= 100000;i ++)
{
first[i][0][0] = 1;
first[i][0][1] = 1;
for (int j = 1;j <= 10;j ++)
{
for (int k = 0;k <= first[i][j - 1][0];k ++)
first[i][j][k] = first[i][j - 1][k];
for (int k = 1;k <= first[i][j][0];k ++)
{
first[i][j][k] *= i;
if (k != 1)
{
first[i][j][k] += first[i][j][k - 1] / 10;
first[i][j][k - 1] %= 10;
}
}
while (first[i][j][first[i][j][0]] >= 10)
{
first[i][j][0] ++;
first[i][j][first[i][j][0]] += first[i][j][first[i][j][0] - 1] / 10;
first[i][j][first[i][j][0] - 1] %= 10;
}
}
}
}
void work1(int z,int n)
{
memset(a,0,sizeof(a));
for (int i = 0;i <= first[z][n][0];i ++)
a[i] = first[z][n][i];
}
void work2(int y,int n)
{
memset(b,0,sizeof(b));
for (int i = 0;i <= first[y][n][0];i ++)
b[i] = first[y][n][i];
}
void work3()
{
memset(c,0,sizeof(c));
c[0] = a[0];
for (int i = 1;i <= a[0];i ++)
{
c[i] += a[i] - b[i];
while (c[i] < 0)
{
c[i] += 10;
c[i + 1] --;
}
}
while (c[c[0]] == 0)
c[0] --;
}
void work4(int x,int n)
{
memset(d,0,sizeof(d));
for (int i = 0;i <= first[x][n][0];i ++)
d[i] = first[x][n][i];
}
bool compare()
{
if (d[0] > c[0])
return true;
else if (d[0] < c[0])
return false;
for (int i = c[0];i >= 1;i --)
if (d[i] < c[i])
return false;
else if (d[i] > c[i])
return true;
}
int work5()
{
if (compare())
{
memset(e,0,sizeof(e));
e[0] = d[0];
for (int i = 1;i <= d[0];i ++)
{
e[i] += d[i] - c[i];
while (e[i] < 0)
{
e[i] += 10;
e[i + 1] --;
}
}
while (e[e[0]] == 0)
e[0] --;
}
else
{
memset(e,0,sizeof(e));
e[0] = c[0];
for (int i = 1;i <= c[0];i ++)
{
e[i] += c[i] - d[i];
while (e[i] < 0)
{
e[i] += 10;
e[i + 1] --;
}
}
while (e[e[0]] == 0)
e[0] --;
}
}
bool compare2()
{
work5();
if (ans[0] > e[0])
{
for (int i = 0;i <= e[0];i ++)
ans[i] = e[i];
return true;
}
else if (ans[0] < e[0])
return false;
for (int i = ans[0];i >= 1;i --)
if (ans[i] < e[i])
return false;
else if (ans[i] > e[i])
{
for (int i = 0;i <= e[0];i ++)
ans[i] = e[i];
return true;
}
}
int main()
{
work0();
scanf("%d",&t);
while (t --)
{
int n,z;
memset(ans,0x7f,sizeof(ans));
ans[0] = 50;
scanf("%d%d",&n,&z);
work1(z,n);
for (int y = 2;y < z;y ++)
{
work2(y,n);
work3();
int l = 1;
int r = y - 1;
while (l < r - 1)
{
int mid = l + r >> 1;
work4(mid,n);
if (compare())
r = mid;
else l = mid;
}
work4(l,n);
if (compare2())
{
ansx = l;
ansy = y;
}
work4(r,n);
if (compare2())
{
ansx = r;
ansy = y;
}
}
printf("%d %d ",ansx,ansy);
for (int i = ans[0];i >= 1;i --)
printf("%d",ans[i]);
printf("\n");
}
return 0;
}B: Black and White
Solution
For each flip, we consider it two segments: one is and the other is . Then sort all segments with increasing . Enumerate . For each we take it as a whole segment , and thus we can use a segment tree to maintain it. Because the colors are only black and white and can switch after each flip, we use xor operation to maintain the lazy tag. Time complexity is .
Code:
#include<cstdio>
#include<algorithm>
using namespace std;
struct newdata
{
int l,r,h;
bool type;
};
struct segment_tree
{
int l,r,sum;
bool lazy;
};
int n,t,k,cnt;
newdata query[20001];
segment_tree a[40001];
bool cmp(newdata i,newdata j)
{
return (i.h == j.h ? i.type > j.type : i.h < j.h);
}
void pushdown(int now)
{
if (a[now].lazy && a[now].l < a[now].r)
{
a[now].lazy = false;
a[now << 1].lazy ^= 1;
a[now << 1].sum = a[now << 1].r - a[now << 1].l + 1 - a[now << 1].sum;
a[(now << 1) + 1].lazy ^= 1;
a[(now << 1) + 1].sum = a[(now << 1) + 1].r - a[(now << 1) + 1].l + 1 - a[(now << 1) + 1].sum;
}
}
void update(int now,int l,int r)
{
if (l == a[now].l && r == a[now].r)
{
a[now].sum = r - l + 1 - a[now].sum;
a[now].lazy ^= 1;
return;
}
pushdown(now);
int mid = a[now].l + a[now].r >> 1;
if (r <= mid)
update(now << 1,l,r);
else if (l > mid)
update((now << 1) + 1,l,r);
else
{
update(now << 1,l,mid);
update((now << 1) + 1,mid + 1,r);
}
if (a[now].l < a[now].r)
a[now].sum = a[now << 1].sum + a[(now << 1) + 1].sum;
}
void first(int now,int l,int r)
{
a[now].l = l;
a[now].r = r;
a[now].lazy = false;
a[now].sum = 0;
int mid = l + r >> 1;
if (l < r)
{
first(now << 1,l,mid);
first((now << 1) + 1,mid + 1,r);
}
}
int main()
{
scanf("%d",&t);
while (t --)
{
cnt = 0;
scanf("%d%d",&n,&k);
first(1,1,n);
for (int i = 1;i <= k;i ++)
{
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
query[++ cnt].l = x1;
query[cnt].r = x2;
query[cnt].h = y1;
query[cnt].type = 1;
query[++ cnt].l = x1;
query[cnt].r = x2;
query[cnt].h = y2;
query[cnt].type = 0;
}
sort(query + 1,query + cnt + 1,cmp);
int now = 1;
int ans = 0;
for (int i = 1;i <= n;i ++)
{
while (now <= cnt && query[now].type && query[now].h <= i)
{
update(1,query[now].l,query[now].r);
now ++;
}
ans += a[1].sum;
while (now <= cnt && query[now].h <= i)
{
update(1,query[now].l,query[now].r);
now ++;
}
}
printf("%d\n",ans);
}
return 0;
}C: Equivalence of Sudoku
Solution
It is easy but has a lot of details. We just need to perform rotations of the original sudoku and the sudoku after flipping along each axis(horizontal or vertical). It can be proved that all equivalent sudokus can be generated by this. Then we save all matrixes generated.
When comparing two matrixes, we need to compare one with all forms of the other one. We can make the reflection of the numbers each time we compare. Then, we can check whether one is equal to any form of the other one. Time complexity is .
Actually, if you just flip along one axis, for example horizontal axis, it is also a right solution. However, I think it is not absolutely right under some conditions.
Code:
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int a[401][12][10][10];
int ref[10];
int main()
{
scanf("%d",&n);
for (int i = 1;i <= n;i ++)
{
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
scanf("%d",&a[i][0][j][k]);
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][1][j][k] = a[i][0][9 - k + 1][j];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][2][j][k] = a[i][0][9 - j + 1][9 - k + 1];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][3][j][k] = a[i][0][k][9 - j + 1];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][4][j][k] = a[i][0][j][9 - k + 1];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][5][j][k] = a[i][4][9 - k + 1][j];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][6][j][k] = a[i][4][9 - j + 1][9 - k + 1];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][7][j][k] = a[i][4][k][9 - j + 1];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][8][j][k] = a[i][0][9 - j + 1][k];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][9][j][k] = a[i][8][9 - k + 1][j];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][10][j][k] = a[i][8][9 - j + 1][9 - k + 1];
for (int j = 1;j <= 9;j ++)
for (int k = 1;k <= 9;k ++)
a[i][11][j][k] = a[i][8][k][9 - j + 1];
}
for (int i = 1;i <= n;i ++)
{
for (int j = 1;j <= n;j ++)
{
bool flag = false;
for (int k = 0;k <= 11;k ++)
{
memset(ref,0,sizeof(ref));
bool equiv = true;
int greater = -1;
for (int x = 1;x <= 9;x ++)
{
for (int y = 1;y <= 9;y ++)
{
if (a[i][0][x][y] != 0 && a[j][k][x][y] != 0 && a[i][0][x][y] != a[j][k][x][y])
{
if (ref[a[i][0][x][y]] == a[j][k][x][y])
continue;
else if (ref[a[i][0][x][y]] == 0)
ref[a[i][0][x][y]] = a[j][k][x][y];
else
{
equiv = false;
break;
}
}
if (a[i][0][x][y] == 0 && a[j][k][x][y] != 0 && greater == -1)
greater = false;
else if (a[i][0][x][y] == 0 && a[j][k][x][y] != 0 && greater == 1)
{
equiv = false;
break;
}
if (a[i][0][x][y] != 0 && a[j][k][x][y] == 0 && greater == -1)
greater = true;
else if (a[i][0][x][y] != 0 && a[j][k][x][y] == 0 && greater == 0)
{
equiv = false;
break;
}
}
if (!equiv)
break;
}
if (equiv)
{
if (greater == -1)
printf("E");
else if (greater == 0)
printf("L");
else if (greater == 1)
printf("G");
flag = true;
break;
}
}
if (!flag)
printf("I");
}
printf("\n");
}
return 0;
}D: Card Collection
Solution
An easy shortest-path problem. Use dijkstra directly and it can be solved. Time complexity is .
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int m;
int s,t;
int dist[51];
int a[51][51];
bool vis[51];
int main()
{
while (scanf("%d",&m) != EOF)
{
scanf("%d%d",&s,&t);
int u,v,w;
scanf("%d",&u);
memset(a,0,sizeof(a));
while (u != 0)
{
scanf("%d%d",&v,&w);
if (a[u][v] == 0)
a[u][v] = w;
else a[u][v] = min(a[u][v],w);
if (a[v][u] == 0)
a[v][u] = w;
else a[v][u] = min(a[v][u],w);
scanf("%d",&u);
}
memset(dist,0x7f7f,sizeof(dist));
memset(vis,false,sizeof(vis));
dist[s] = 0;
for (int i = 1;i <= m;i ++)
{
int k;
int now = 2147483647;
for (int j = 1;j <= m;j ++)
if (dist[j] < now && !vis[j])
{
now = dist[j];
k = j;
}
vis[k] = true;
for (int j = 1;j <= m;j ++)
if (a[k][j])
dist[j] = min(dist[j],dist[k] + a[k][j]);
}
printf("%d\n",dist[t]);
}
return 0;
}E: Base Station Sites
Solution
Binary search the answer , which means the largest minimum distance among the base stations. Time complexity is .
Code:
#include<cstdio>
#include<algorithm>
using namespace std;
int s,n;
int a[100001];
int main()
{
scanf("%d%d",&n,&s);
while (n != 0 || s != 0)
{
for (int i = 1;i <= n;i ++)
scanf("%d",&a[i]);
sort(a + 1,a + n + 1);
int l = 0;
int r = 1000000;
while (l < r)
{
int mid = l + r + 1 >> 1;
int now = 0;
int tot = 1;
bool flag = false;
for (int i = 2;i <= n;i ++)
{
now += a[i] - a[i - 1];
if (now >= mid)
{
tot ++;
now = 0;
}
if (tot == s)
{
flag = true;
break;
}
}
if (flag)
l = mid;
else r = mid - 1;
}
printf("%d\n",r);
scanf("%d%d",&n,&s);
}
return 0;
} F: Nearby Bicycles
Solution
Calculate all distances between bicycles and users. Then, for every user, count all bicycles whose distance are smaller than . Time complexity is , where is the number of test datas.
The input has a strange problem. I don’t know why following code can’t be accepted:
#include<cstdio>
#include<cmath>
using namespace std;
struct newdata
{
long long x,y;
};
int m,n;
newdata a[1005],b[1005];
long long dis[1005][1005];
long long s[1005];
long long sqr(long long x)
{
return x * x;
}
int main()
{
scanf("%d%d",&m,&n);
while (m != 0 || n != 0)
{
char c = getchar();
for (int i = 1;i <= m;i ++)
{
a[i].x = 0;
a[i].y = 0;
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
{
a[i].x = a[i].x * 10 + c - '0';
c = getchar();
}
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
{
a[i].y = a[i].y * 10 + c - '0';
c = getchar();
}
}
for (int i = 1;i <= n;i ++)
{
b[i].x = 0;
b[i].y = 0;
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
{
b[i].x = b[i].x * 10 + c - '0';
c = getchar();
}
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
{
b[i].y = b[i].y * 10 + c - '0';
c = getchar();
}
}
for (int i = 1;i <= n;i ++)
for (int j = 1;j <= m;j ++)
dis[i][j] = sqr(a[j].x - b[i].x) + sqr(a[j].y - b[i].y);
for (int i = 1;i <= n;i ++)
{
s[i] = 0;
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
{
s[i] = s[i] * 10 + c - '0';
c = getchar();
}
s[i] = sqr(s[i]);
}
for (int i = 1;i <= n;i ++)
{
int ans = 0;
for (int j = 1;j <= m;j ++)
if (dis[i][j] <= s[i])
ans ++;
if (i != n)
printf("%d ",ans);
else printf("%d\n",ans);
}
scanf("%d%d",&m,&n);
}
return 0;
}It must be changed to cin. Otherwise it gets wrong answer.
Code:
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
struct newdata
{
long long x,y;
};
int m,n;
newdata a[1005],b[1005];
long long dis[1005][1005];
long long s[1005];
long long sqr(long long x)
{
return x * x;
}
int main()
{
ios::sync_with_stdio<false>
scanf("%d%d",&m,&n);
while (m != 0 || n != 0)
{
char c,d,e;
for (int i = 1;i <= m;i ++)
cin >> c >> a[i].x >> d >> a[i].y >> e;
for (int i = 1;i <= n;i ++)
cin >> c >> b[i].x >> d >> b[i].y >> e;
for (int i = 1;i <= n;i ++)
for (int j = 1;j <= m;j ++)
dis[i][j] = sqr(a[j].x - b[i].x) + sqr(a[j].y - b[i].y);
for (int i = 1;i <= n;i ++)
{
s[i] = 0;
c = getchar();
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
{
s[i] = s[i] * 10 + c - '0';
c = getchar();
}
s[i] = sqr(s[i]);
}
for (int i = 1;i <= n;i ++)
{
int ans = 0;
for (int j = 1;j <= m;j ++)
if (dis[i][j] <= s[i])
ans ++;
if (i != n)
printf("%d ",ans);
else printf("%d\n",ans);
}
scanf("%d%d",&m,&n);
}
return 0;
}