SDNU_ACM_ICPC_2022_Winter_Practice_4th [individual]

Posted by travelkind on Mon, 14 Feb 2022 16:56:17 +0100

A

A - A Funny Game

Meaning: n coins form a circle. Each round can take away one or two adjacent coins. Finally, the player who takes all the coins wins. Alice took the lead and asked who would win.

Idea: when n < = 2, Alice can take it all at once, and Alice will win; When n > = 3, no matter Alice takes 1 or 2 coins, Bob can take the coins according to the parity of the remaining coins, so that the number of coins left for Alice is even. After that, Bob just needs to imitate Alice to take the coins, and Bob will win. (symmetric game)

#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <cstdlib>
#include <set>
#include <queue>
#define lowbit(x) x&(-x)
#define endl '\n'
#define sf(x) scanf("%d",&x)
#define rep(i,x) for(i=0;i<(x);i++)
#define gao(x) cerr<<#x<<"->"<<x<<endl
#define gen(x) x##_
typedef long long ll;
const int maxx=1e6+10;
using namespace std;

void solve()
{
    int n;
    while(cin>>n&&n)
    {
        if(n<=2) cout<<"Alice\n";
        else cout<<"Bob\n";
    }
}
int main()
{
    int _t=1;
    //scanf("%d",&_t);
    while(_t--)
    {
        solve();
    }
    system("pause");
}

D

D - Red and Blue

Give you an r array of n elements and a b array of m elements. Without changing the relative position of the two array elements, merge them into an array and find the maximum value of f(a)

Idea: the maximum prefix sum of array a is required. Because the order of array b and array r is independent, only the maximum prefix sum of array b plus the maximum prefix sum of array r is the answer.

#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <cstdlib>
#include <set>
#include <queue>
#define lowbit(x) x&(-x)
#define endl '\n'
#define sf(x) scanf("%d",&x)
#define rep(i,x) for(i=0;i<(x);i++)
#define gao(x) cerr<<#x<<"->"<<x<<endl
#define gen(x) x##_
typedef long long ll;
const int maxx=110;
const int inf=0x3f3f3f3f;
using namespace std;
ll n,m;
ll a[maxx],b[maxx];
void solve()
{
    scanf("%lld",&n);
    ll maxa=0,maxb=0;
    ll suma=0,sumb=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        suma+=a[i];
        if(suma>maxa) maxa=suma;
    }
    scanf("%lld",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%lld",&b[i]);
        sumb+=b[i];
        if(sumb>maxb) maxb=sumb;
    }
    printf("%lld\n",maxa+maxb);
}
int main()
{
    int _t=1;
    scanf("%d",&_t);
    while(_t--)
    {
        solve();
    }
    system("pause");
}

F

F - The XOR Largest Pair 

Meaning:

Idea: find the maximum XOR value of two numbers. Because XOR is a non carry addition, it must be a larger number (note that it is not the maximum) and another number with the largest number of different numbers in the corresponding bit under binary. So first take out the numbers with the largest number of binary digits, enumerate these numbers, XOR with other numbers, and keep getting the maximum.

#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <cstdlib>
#include <set>
#include <queue>
#define lowbit(x) x&(-x)
#define endl '\n'
#define sf(x) scanf("%d",&x)
#define rep(i,x) for(i=0;i<(x);i++)
#define gao(x) cerr<<#x<<"->"<<x<<endl
#define gen(x) x##_
typedef long long ll;
const int maxx=1e5+10;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
using namespace std;
int n;
ll a[maxx];
ll op[maxx];
ll ppow(ll n,ll m)
{
    ll ret=1;
    for(ll i=1;i<=m;i++)
    {
        ret*=n;
    }
    return ret;
}
void solve()
{
    scanf("%d",&n);
    ll ma=-inf;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if(a[i]>ma) ma=a[i];//Take out the maximum value
    }
    ll ans=-inf;
    int x=1.0*log(ma)/log(2);//X is the number of bits of the maximum value in binary x=log 2 ma rounded down
    ll tem=ppow(2,x);//tem is the minimum value under x-bit binary
    int cnt=0;
    for(int i=1;i<=n;i++)//Take out the maximum number of binary digits
    {
        if(a[i]>=tem)//Take out the maximum number of binary digits
        {
            op[++cnt]=a[i];
        }
    }
    for(int i=1;i<=cnt;i++)//Enumerate the maximum number of binary digits
    {
        for(int j=1;j<=n;j++)//Enumeration a[j]
        {
            ll ret=op[i]^a[j];
            if(ret>ans) ans=ret;
        }
    }
    printf("%lld",ans);
}
int main()
{
    int _t=1;

    //scanf("%d",&_t);
    while(_t--)
    {
        solve();
    }
    system("pause");
}

I

I - Regular Bracket Sequence

Give you an include (and), the others are? String, where? It can be replaced by (or), asking whether it can be put together into a legal sequence.

Idea: first, the length of the string must be an even number. Second, just ensure that it is not in the first place (not at the end).

#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <cstdlib>
#include <set>
#include <queue>
#define lowbit(x) x&(-x)
#define endl '\n'
#define sf(x) scanf("%d",&x)
#define rep(i,x) for(i=0;i<(x);i++)
#define gao(x) cerr<<#x<<"->"<<x<<endl
#define gen(x) x##_
typedef long long ll;
const int maxx=1e6+10;
using namespace std;
char s[110];
void solve()
{
    scanf("%s",s+1);
    int len=strlen(s+1);
    if(len&1)
    {
        printf("NO\n");
        return ;
    }
    if(s[1]==')'||s[len]=='(') printf("NO\n");
    else printf("YES\n");
}
int main()
{
    int _t=1;
    scanf("%d",&_t);
    while(_t--)
    {
        solve();
    }
    system("pause");
}

L - : (Colon)

Give you the length a and b of the hour hand and minute hand. After h hours and m minutes, find the length of the connecting line at the end of the hour hand and minute hand.

#include <stdio.h>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <cstdlib>
#include <set>
#include <queue>
#define lowbit(x) x&(-x)
#define endl '\n'
#define sf(x) scanf("%d",&x)
#define rep(i,x) for(i=0;i<(x);i++)
#define gao(x) cerr<<#x<<"->"<<x<<endl
#define gen(x) x##_
typedef long long ll;
const int maxx=110;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
using namespace std;
ll la,lb,h,m;
void solve()
{
    scanf("%lld %lld %lld %lld",&la,&lb,&h,&m);
    ll ta=0,tb=0;//min
    ta=h*60+m,tb=m;//Hour hand movement time
    double ang=(double)abs(12*tb-ta)/360.0;
    ang*=PI;//Angle of two needles
    printf("%.20f",sqrt(la*la+lb*lb-2*la*lb*cos(ang)));//a*a+b*b-c*c=2*a*b*cosC
}
int main()
{
    int _t=1;
    //scanf("%d",&_t);
    while(_t--)
    {
        solve();
    }
    system("pause");
}

Topics: Algorithm Dynamic Programming