其他分享
首页 > 其他分享> > Codeforces Round #613(div2)解题报告

Codeforces Round #613(div2)解题报告

作者:互联网

Codeforces Round #613(div2)解题报告

A. Mezo Playing Zoma

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;

int main()
{
    int n;
    cin >> n;
    string str;
    cin >> str;
    int n1 = 0, n2 = 0;
    for(int i = 0; i < str.size(); i++)
    {
        if(str[i] == 'L') n1++;
        else n2++;
    }
    int ans = n1 + n2 + 1;
    cout << ans << endl;
    return 0;
}

B. Just Eat It!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int T, n;
ll a[maxn], sum, f[maxn], s[maxn];

void solve()
{
    sum = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        f[i] = s[i] = 0;
    for(int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        sum += a[i];
        s[i] = s[i-1] + a[i];
    }

    ll maxx = -0x3f3f3f3f;
    for(int i = 1; i <= n; i++)
    {
        f[i] = max(a[i], f[i-1] + a[i]);
        maxx = max(maxx, f[i]);
    }
    int r = 0, l = 0;
    //正序遍历保证区间最短
    for(int i = 1; i <= n; i++)
        if(f[i] == maxx)
        {
            r = i;
            break;
        }

    for(int i = r; i >= 1; i--)
        if(s[r] - s[i-1] == maxx)
        {
            l = i;
            break;
        }

    if(sum > maxx)  puts("YES");
    else {
        if(sum == maxx)
        {
            if(l == 1 && r == n) puts("YES");
            else puts("NO");
        }
        else puts("NO");
    }
}

int main()
{
    scanf("%d", &T);
    while(T--) solve();
    return 0;
}

C. Fadi and LCM

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
ll gcd(ll a, ll b)
{
    if(b == 0) return a;
    return gcd(b, a%b);
}
int main()
{
    ll x; cin >> x;
    if(x == 1) {
        cout << "1 1" << endl;
        return 0;
    }
    ll a = sqrt(x);
    while(1)
    {
        ll b = x / a;
        if(a * b == x && gcd(a, b) == 1){
            cout << a << " " << b << endl;
            return 0;
        }
        a--;
    }
    return 0;
}

D. Dr. Evil Underscores

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> a;
int n;

int solve(vector<int> v, int bit)
{
    if(bit < 0 || v.size() == 0) return 0;
    vector<int> z, o;
    for(int i = 0; i < v.size(); i++)
    {
        if((v[i]>>bit) & 1) z.push_back(v[i]);
        else o.push_back(v[i]);
    }
    if(o.size() == 0) return solve(z, bit-1);
    if(z.size() == 0) return solve(o, bit-1);
    return (min(solve(o, bit-1), solve(z, bit-1))|1<<bit);
}

int main()
{
    scanf("%d", &n);
    for(int i = 1, x; i++ <= n;)
        scanf("%d", &x), a.push_back(x);
    cout << solve(a, 30) << endl;
}

E. Delete a Segment

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;

int n;
struct segment{
    ll l, r;
}s[maxn];
ll ans0, ans1;
ll tmp[maxn<<1], d_[maxn<<2], d[maxn<<2];

void solve()
{
    ans0 = 0;
    ans1 = -0x3f3f3f3f;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d%d", &s[i].l, &s[i].r);
        tmp[(i<<1)-1] = s[i].l;
        tmp[i<<1] = s[i].r;
    }
    sort(tmp+1, tmp+(n<<1)+1);
    int tot = unique(tmp+1, tmp+(n<<1)+1) - tmp - 1;
    for(int i = 1; i <= n; i++)
    {
        s[i].l = lower_bound(tmp+1, tmp+1+tot, s[i].l) - tmp;
        s[i].r = lower_bound(tmp+1, tmp+1+tot, s[i].r) - tmp;
        s[i].l <<= 1ll, s[i].r <<= 1ll;
        d[s[i].l]++, d[s[i].r+1]--; //差分序列
    }
    tot <<= 1;
    for(int i = 1; i <= tot+5; i++) 
        d[i] += d[i-1];
    //初始答案
    for(int i = 0; i <= tot+5; i++) 
        ans0 += d[i] && !d[i+1];
    for(int i = 0; i <= tot+4; i++)
        if(d[i] == 1 && d[i+1] != 1)
            d_[i]++;
    for(int i = 1; i <= tot+5; i++)
        if(d[i] == 1 && d[i-1] != 1)
            d_[i]++;
    for(int i = 1; i <= tot+5; i++)
        d_[i] += d_[i-1];
    for(int i = 1; i <= n; i++)
    {
        ll t = (d_[s[i].r] - d_[s[i].l-1]) / 2;
        t -= (d[s[i].r] == 1) + (d[s[i].l] == 1);
        ans1 = max(ans1, t);
    }
    cout << ans0 + ans1 << endl;
    for(int i = 0; i <= tot+10; i++)
        d[i] = d_[i] = 0;
}

int main()
{
    int T; scanf("%d", &T);
    while(T--) solve();
    return 0;
}

F. Classical?

题意:

思路:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int n, cnt[maxn];
bool b[maxn], vis[maxn];
vector<int> d[maxn];
ll ans;

ll gcd(ll a, ll b)
{
    if(b == 0) return a;
    return gcd(b, a%b);
}

int mu[maxn], primes[maxn], tot;
void get_mu(int n)
{
    mu[1] = 1;
    for(int i = 2; i <= n; i++)
    {
        if(!vis[i])
        {
            primes[++tot] = i;
            mu[i] = -1;
        }
        for(int j = 1; primes[j] <= n/i; j++)
        {
            vis[primes[j]*i] = 1;
            if(i % primes[j] == 0) break;
            else mu[i*primes[j]] = -mu[i];
        }
    }
    //预处理出所有倍数
    for(int i = 1; i <= n; i++)
        for(int j = i; j <= n; j += i)
            d[j].push_back(i);
}

ll coprime(ll x)
{
    ll res = 0;
    for(int i = 0; i < d[x].size(); i++)
        res += cnt[d[x][i]] * mu[d[x][i]];
    return res;
}

void update(int x, int a)
{
    for(int i = 0; i < d[x].size(); i++)
        cnt[d[x][i]] += a;
}

int main()
{
    get_mu(maxn-10);

    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        ll x; scanf("%lld", &x);
        ans = max(ans, x);
        b[x] = 1;
    }

    stack<int> s;
    //枚举gcd=g
    for(int g = 1; g <= maxn-10; g++)
    {
        for(int i = (maxn-10)/g; i > 0; i--)
        {
            //没有这个数字 跳过
            if(!b[i*g]) continue;
            //c表示栈中与i互质的数的数量
            int c = coprime(i);
            while(c)
            {
                if(gcd(s.top(), i) == 1)
                {
                    ans = max(ans, 1ll*i*s.top()*g);
                    c--;
                }
                //出栈
                update(s.top(), -1);
                s.pop();
            }
            update(i, 1);
            s.push(i);
        }
        //空栈
        while(s.size())
        {
            update(s.top(), -1);
            s.pop();
        }
    }
    cout << ans << endl;
    return 0;
}

标签:gcd,int,613,ll,Codeforces,long,maxn,互质,Round
来源: https://www.cnblogs.com/zxytxdy/p/12210186.html