这一场因为重载运算符用了等于号,导致签到失败,成绩巨烂,感谢dreamoon,让我在训练赛中摸到了这些坑,给现场赛铺路
1001 fraction
学习自这位大佬:Flyppy_White
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void gao(ll p1, ll x1, ll p2, ll x2, ll& b, ll& c) {
ll d = (p1 + x1 - 1) / x1;
if (d <= p2 / x2) {
c = 1;
b = d;
return;
}
d--;
p1 -= d * x1;
p2 -= d * x2;
gao(x2, p2, x1, p1, c, b);
b += d * c;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
ll p, x, b, c;
scanf("%lld%lld", &p, &x);
gao(p, x, p, x - 1, b, c);
printf("%lld/%lld\n", b * x - p * c, b);
}
}
题意:给你两个长度为n的序列a,b,你可以对a,b任意排序,然后构造一个序列c:ci = ai^bi,使得序列c字典序最小
解法:提交了20多次,花了5个小时,终于搞定这题了,首先我们对两个序列分别建字典树,假设ai和b序列中的bj异或值最小(我们用ai喜欢bj来表示这个关系),我们可以用字典树b找到bj,然后再到字典树a找到一个ak,使得bj和ak异或值最小,如果ak = ai,那么显然ai 和 bj 直接配对是最优的,然后从两颗字典树中分别删除ai和bj,如果ak不等于bj,那我们继续从字典树中找和ak喜欢的bp,继续去判断他们的关系,假设我们对于每一对喜欢关系建一个有向图,该有向图如果有环,环长肯定只能是2(可以自己证明一下环长>2肯定不存在),那我们用这个栈来存这个有向图,每次遇到二元环,我们就依次从栈中删除二元环,然后继续找栈顶元素喜欢的对象即可。有点卡常
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct Tri {
int ch[maxn * 31][2], cnt[maxn * 31], val[maxn * 31], sz;
void init() {
sz = 0;
ch[sz][0] = ch[sz][1] = 0;
}
void insert(int x, int v) {
int o = 0;
for (int i = 29; i >= 0; i--) {
int c = 0;
if (x >> i & 1)
c = 1;
if (!ch[o][c]) {
ch[o][c] = ++sz;
cnt[sz] = ch[sz][0] = ch[sz][1] = 0;
}
o = ch[o][c];
cnt[o] += v;
}
val[o] = x;
}
int query(int x) {
int o = 0;
for (int i = 29; i >= 0; i--) {
int c = 0;
if (x >> i & 1)
c = 1;
if (!cnt[ch[o][c]])
c ^= 1;
o = ch[o][c];
}
return val[o];
}
} ac[2];
int a[maxn], b[maxn], c[maxn], s[maxn * 2];
int main() {
int T, n;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
ac[0].init();
ac[1].init();
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ac[0].insert(a[i], 1);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
ac[1].insert(b[i], 1);
}
int cnt = 0, top = 0;
s[0] = -1;
for (int i = 1; i <= n; i++)
if (cnt < n && ac[0].query(a[i]) == a[i]) {
s[++top] = a[i];
while (top && cnt < n) {
if (top & 1) {
int B = ac[1].query(s[top]);
if (ac[0].query(B) == s[top]) {
c[++cnt] = s[top] ^ B;
ac[0].insert(s[top], -1);
ac[1].insert(B, -1);
top--;
if (s[top] == B)
top--;
}
else
s[++top] = B;
}
else {
int A = ac[0].query(s[top]);
if (ac[1].query(A) == s[top]) {
c[++cnt] = s[top] ^ A;
ac[0].insert(A, -1);
ac[1].insert(s[top], -1);
top--;
if (s[top] == A)
top--;
}
else
s[++top] = A;
}
}
}
sort(c + 1, c + 1 + n);
for (int i = 1; i < n; i++)
printf("%d ", c[i]);
printf("%d\n", c[n]);
}
}
就是这个题,让我学会了,重载运算符,一定不能写等于!!!
#include<bits/stdc++.h>
#define pi pair<ll, ll>
#define mk make_pair
#define ll long long
using namespace std;
pi gao(pi a) {
ll d = __gcd(a.first, a.second);
a.first /= d;
a.second /= d;
if (a.second < 0)
a.first = -a.first, a.second = -a.second;
return a;
}
bool cmp(pi a, pi b) {
return a.first * b.second < a.second * b.first;
}
const int maxn = 1e5 + 10;
ll suma[maxn], sumb[maxn], c;
pi arr[maxn];
vector<pi> ans, ans2;
int main() {
int T;
scanf("%d", &T);
while (T--) {
ans.clear();
int n, a, b;
scanf("%d%lld", &n, &c);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a, &b);
arr[i] = mk(-b, a);
}
sort(arr + 1, arr + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
suma[i] = suma[i - 1] + arr[i].second;
sumb[i] = sumb[i - 1] - arr[i].first;
}
int flag = 0;
for (int i = 0; i <= n; i++) {
ll A = suma[i] - (suma[n] - suma[i]);
ll B = sumb[i] - (sumb[n] - sumb[i]);
if (!A) {
if (B == c)
flag = 1;
continue;
}
pi tmp = gao(mk(c - B, A));
int ok = 1;
if (i < n)
if (cmp(arr[i + 1], tmp))
ok = 0;
if (i)
if (cmp(tmp, arr[i]))
ok = 0;
if (ok)
ans.push_back(tmp);
}
if (flag)
puts("-1");
else if (ans.empty())
puts("0");
else {
sort(ans.begin(), ans.end(), cmp);
ans2.clear();
pi pre = mk(-1000000, 1);
for (auto tmp : ans)
if (ans2.empty() || cmp(pre, tmp))
ans2.push_back(tmp), pre = tmp;
printf("%d", ans2.size());
for (int i = 0; i < ans2.size(); i++)
printf(" %lld/%lld", ans2[i].first, ans2[i].second);
puts("");
}
}
}
开始用后缀数组,tle,再用二分+hash,再tle,最后才用exkmp过的,没啥好讲的,贴个板子吧
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
int nxt[maxn],ex[maxn];
char a[maxn], b[maxn];
void GETNEXT(char *str)
{
int i=0,j,po,len=strlen(str);
nxt[0]=len;
while(str[i]==str[i+1]&&i+1<len)
i++;
nxt[1]=i;
po=1;
for(i=2; i<len; i++)
{
if(nxt[i-po]+i<nxt[po]+po)
nxt[i]=nxt[i-po];
else
{
j=nxt[po]+po-i;
if(j<0)j=0;
while(i+j<len&&str[j]==str[j+i])
j++;
nxt[i]=j;
po=i;
}
}
}
ll EXKMP(char *s1,char *s2)
{
ll ans = 0;
int i=0,j,po,len=strlen(s1),l2=strlen(s2);
GETNEXT(s2);
while(s1[i]==s2[i]&&i<l2&&i<len)
i++;
ex[0]=i;
ans += min(len - 0,ex[0] + 1);
po=0;
for(i=1; i<len; i++)
{
if(nxt[i-po]+i<ex[po]+po)
ex[i]=nxt[i-po], ans += min(len - i,ex[i] + 1);
else
{
j=ex[po]+po-i;
if(j<0)j=0;
while(i+j<len&&j<l2&&s1[j+i]==s2[j])
j++;
ex[i]=j;
ans += min(len - i,ex[i] + 1);
po=i;
}
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
scanf("%s", b);
int n = strlen(b);
for (int i = 1; i < n; i++)
a[i - 1] = b[i];
a[n - 1] = '\0';
printf("%lld\n", EXKMP(a, b));
}
}