A Clam and Fish（贪心）

AC代码：

#include<bits/stdc++.h>using namespace std; const int mxn=2000010;int n;char s[mxn]; int main(){    int T;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        scanf("%s",s);        int ans=0,nw=0;        for(int i=0;i<n;i++){            int a=s[i]-'0';            if(a&2){                ans++;                continue;            }            if(a==1){                nw++;            }            else{                if(nw==0) continue;                nw--;                ans++;            }        }        ans+=nw/2;        printf("%d\n",ans);    }    return 0;}

B Classical String Problem（找规律）

AC代码：

const int N = 2e6 + 50;int n, m;char s[N];int main(){  ss(s);  sd(m);  int len=strlen(s);  int pos = 0;  while (m--)  {    char op;    int x;    getchar();    scanf("%c %d",&op,&x);    if (op == 'M')    {      if (x > 0)      {        pos += x;        pos %= len;      }      else      {        pos += x;        pos += len;        pos %= len;      }    }    else    {      printf("%c\n", s[(x + pos - 1 + len) % len]);    }  }  return 0;}

C Operation Love（计算几何）

AC代码：

const int mxn = 1010;const double eps = 1e-3; struct po{    double x, y;} a[mxn];po operator-(po a, po b){    return (po){a.x - b.x, a.y - b.y};}double len(po a){    return sqrt(a.x * a.x + a.y * a.y);}bool dengyu(double x, double y){    return abs(x - y) < eps;}int n = 20; double cro(po a, po b){    return a.x * b.y - b.x * a.y;}bool sol(){    for (int i = 0; i < n; i++)    {        scanf("%lf%lf", &a[i].x, &a[i].y);    }    a[n] = a[0];    a[n + 1] = a[1];    for (int i = 0; i <= n; i++)    {        po p = a[i + 1] - a[i], q = a[i + 2] - a[i + 1];        if (dengyu(len(p), 6.0) && dengyu(len(q), 9.0))        {            return cro(p, q) > 0;        }    } //逆时针给点    for (int i = 0; i <= n; i++)    {        po p = a[i + 1] - a[i], q = a[i + 2] - a[i + 1];        if (dengyu(len(q), 6.0) && dengyu(len(p), 9.0))        {            return cro(q, p) > 0;        }    } //顺时针给点    return 0;}int main(){    int T;    scanf("%d", &T);    while (T--)    {        if (!sol())            printf("left\n");        else            printf("right\n");    }    return 0;}

D Points Construction Problem（构造）

AC代码：

int vis[105][105];int arr[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};int main(){  int t;  sd(t);  while (t--)  {    int n, m;    sdd(n, m);    if (m & 1)    {      puts("No");      continue;    }    m >>= 1;    int flag = 0;    rep(a, 1, m / 2)    {      int b = m - a;      if (n >= b && n <= a * b)      {        flag = 1;        puts("Yes");        int cnt = 0;        queue<int> q;        rep(i, 1, a)        {          rep(j, 1, b)            vis[i][j] = 0;        }        rep(i, 1, b)        {          int x = min(i, a), y = min(i, b);          pdd(x, y);          vis[x][y] = 1;          rep(j, 0, 3)          {            int tx = x + arr[j][0], ty = y + arr[j][1];            if (tx >= 1 && tx <= a && ty >= 1 && ty <= b && !vis[tx][ty])              q.push(tx), q.push(ty);          }          cnt++;        }        while (cnt != n)        {          int x = q.front();          q.pop();          int y = q.front();          q.pop();          if (vis[x][y])            continue;          vis[x][y] = 1;          pdd(x, y);          ;          cnt++;          rep(j, 0, 3)          {            int tx = x + arr[j][0], ty = y + arr[j][1];            if (tx >= 1 && tx <= a && ty >= 1 && ty <= b && !vis[tx][ty])              q.push(tx), q.push(ty);          }        }        break;      }    }    if (flag == 0)      puts("No");  }  return 0;}

E Two Matchings（dp）

AC代码：

const int N = 2e5 + 50;int n, m;ll a[N];ll dp[N]; int main(){    int t;    sd(t);    while (t--)    {         sd(n);        rep(i, 1, n)            sld(a[i]);        sort(a + 1, a + 1 + n);        dp[1] = dp[2] = dp[3] = dp[5] = INF;        dp[4] = 2 * (a[4] - a[1]);        dp[6] = 2 * (a[6] - a[1]);        rep(i, 7, n)        {            if (i & 1)                dp[i] = inf;            else                dp[i] = min(dp[i - 4] + 2 * (a[i] - a[i - 3]), dp[i - 6] + 2 * (a[i] - a[i - 5]));        }        pd(dp[n]);    }    return 0;}

F Fraction Construction Problem（ExGcd）

题意：

1. ，令

1. ，把 分解成两个互质的部分 ，且 均不为 ， 否则就是

AC代码：

void exgcd(ll a, ll b, ll &x, ll &y){    if (!b)    {        x = 1, y = 0;        return;    }    exgcd(b, a % b, y, x);    y -= a / b * x;}const int N = 2e6 + 10;int pri[N], cnt;bool st[N];int Mipri[N];void init(int n){    st[0] = st[1] = true;    Mipri[1] = 1;    for (int i = 2; i <= n; i++)    {        if (!st[i])            pri[cnt++] = i, Mipri[i] = i;        for (int j = 0; j < cnt && 1ll * i * pri[j] <= n; j++)        {            st[i * pri[j]] = true;            Mipri[i * pri[j]] = pri[j];            if (i % pri[j] == 0)                break;        }    }}int main(){    init(N - 1);    int t;    sd(t);    while (t--)    {        int a, b;        sdd(a,b);        int g = gcd(a, b);        if (g > 1)        {            int e = 1, f = 1, c = a / g + b / g, d = b / g;            printf("%d %d %d %d\n", c, d, e, f);        }        else        {            ll d = 1, f = b, k = Mipri[b];            while (k != 1 && f % k == 0)                d *= k, f /= k;            if (d == b && f == 1)                puts("-1 -1 -1 -1");            else            {                ll e, c;                exgcd(d, f, e, c);                e = -e;                while (e <= 0 || c <= 0)                {                    c += d;                    e += f;                }                e *= a, c *= a;                printf("%lld %lld %lld %lld\n", c, d, e, f);            }        }    }    return 0;}

G Operating on a Graph

AC代码：

#include<bits/stdc++.h>using namespace std; const int mxn=1600010,mxm=mxn<<1;int n,m;int hd[mxn],nx[mxm],v[mxm],e=0;int f[mxn];bool del[mxn];int lhd[mxn],lnx[mxn],lta[mxn]; int find(int x){    if(f[x]==x) return x;    return f[x]=find(f[x]);}void ade(int x,int y){    if(x==y) return;    nx[++e]=hd[x];    hd[x]=e;    v[e]=y;}int st[mxn],t;void merg(int x,int y){    f[y]=x;    int z=lhd[x];    lhd[x]=lhd[y];    lnx[lta[y]]=z;    if(z==0) lta[x]=lta[y];}void sol(int o){    if(find(o)!=o) return;    t=0;    for(int x=lhd[o];x;x=lnx[x]){        st[++t]=x;        del[x]=1;    }    lhd[o]=lta[o]=0;    for(int p=1;p<=t;p++){        int x=st[p];        for(int i=hd[x];i;i=nx[i]){            int y=v[i];            if(del[y]) continue;            int fy=find(y);            if(fy==o) continue;            merg(o,fy);        }    }}void solmain(){    scanf("%d%d",&n,&m);    e=0;    for(int i=1;i<=n;i++){        hd[i]=0;        f[i]=i;        del[i]=0;        lhd[i]=lta[i]=i;        lnx[i]=0;    }    while(m--){        int x,y;        scanf("%d%d",&x,&y);        x++; y++;        ade(x,y);        ade(y,x);    }    int Q;    scanf("%d",&Q);    while(Q--){        int o;        scanf("%d",&o);        o++;        sol(o);    }    for(int i=1;i<=n;i++) printf("%d ",find(i)-1);    printf("\n");}int main(){//  freopen("g.in","r",stdin);//  freopen("g.out","w",stdout);    int T;    scanf("%d",&T);    while(T--){        solmain();    }    return 0;}

L Problem L is the Only Lovely Problem（签到）

AC代码：

const int N = 1e5 + 50;int n, m;int a[N]; int main(){    string s;    cin >> s;    if ((s[0] == 'L' || s[0] == 'l') && (s[1] == 'O' || s[1] == 'o') && (s[2] == 'V' || s[2] == 'v') && (s[3] == 'E' || s[3] == 'e') && (s[4] == 'L' || s[4] == 'l') && (s[5] == 'Y' || s[5] == 'y'))        puts("lovely");    else        puts("ugly");    return 0;}