Codeforces_834

A.两个方向都判断。

#include<bits/stdc++.h>
using namespace std;

string s1,s2;
map<char,int> mp;
int n;

int main()
{
    ios::sync_with_stdio(0);
    mp['v'] = 0;
    mp['<'] = 1;
    mp['^'] = 2;
    mp['>'] = 3;
    cin >> s1 >> s2 >> n;
    int x = mp[s1[0]],y = mp[s2[0]];
    n %= 4;
    if((x+n)%4 == y && (x-n+4)%4 == y)  cout << "undefined" << endl;
    else if((x+n)%4 == y)   cout << "cw" << endl;
    else    cout << "ccw" << endl;
    return 0;
}
View Code

B.统计每个字母首位,前缀和。

#include<bits/stdc++.h>
using namespace std;

string s;
map<char,int> l,r;
int n,k,sum[1000005];

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> k >> s;
    s = ' '+s;
    for(int i = 1;i <= n;i++)   r[s[i]] = i;
    for(int i = n;i >= 1;i--)   l[s[i]] = i;
    for(char c = 'A';c <= 'Z';c++)
    {
        sum[l[c]]++;
        sum[r[c]+1]--;
    }
    int maxx = 0;
    for(int i = 1;i <= n;i++)
    {
        sum[i] += sum[i-1];
        maxx = max(maxx,sum[i]);
    }
    if(maxx > k)    cout << "YES" << endl;
    else    cout << "NO" << endl;
    return 0;
}
View Code

C.只要ab是某个数的三次方,并且a和b能整除(ab)^1/3。打表map预处理开三次方。

#include<bits/stdc++.h>
using namespace std;

map<long long,int> mp;
int n;
long long a,b;

int main()
{
    ios::sync_with_stdio(0);
    for(long long i = 1;i <= 1000000;i++) mp[i*i*i] = i;
    scanf("%d",&n);
    while(n--)
    {
         scanf("%d%d",&a,&b);
         long long t = a*b;
         if(mp.count(t))
         {
             long long tt = mp[t];
             if(a%tt == 0 && b%tt == 0) printf("Yes
");
             else   printf("No
");
         }
         else   printf("No
");
    }
    return 0;
}
View Code

D.线段树更新dp。对于每一个位置,找前面最后一个相同数字的位置,将这一段的值都加一。

#include<bits/stdc++.h>
using namespace std;

int n,k,a[35005],pre[35005] = {0},lastt[35005] = {0},dp[35005];

struct xx
{
    int l,r,x,lazy;
}tree[35005*4];

void pushup(int pos)
{
    tree[pos].x = max(tree[pos<<1].x,tree[pos<<1|1].x);
}

void pushdown(int pos)
{
    if(tree[pos].lazy)
    {
        int t = tree[pos].lazy;
        tree[pos<<1].x += t;
        tree[pos<<1|1].x += t;
        tree[pos<<1].lazy += t;
        tree[pos<<1|1].lazy += t;
        tree[pos].lazy = 0;
    }
}

void build(int pos,int l,int r)
{
    tree[pos].l = l;
    tree[pos].r = r;
    tree[pos].lazy = 0;
    if(l >= r)
    {
        tree[pos].x = dp[l];
        return;
    }
    int mid = (l+r)/2;
    build(pos<<1,l,mid);
    build(pos<<1|1,mid+1,r);
    pushup(pos);
}

void update(int pos,int l,int r)
{
    if(l <= tree[pos].l && tree[pos].r <= r)
    {
        tree[pos].x++;
        tree[pos].lazy++;
        return;
    }
    pushdown(pos);
    int mid = (tree[pos].l+tree[pos].r)/2;
    if(l <= mid)    update(pos<<1,l,r);
    if(r > mid)     update(pos<<1|1,l,r);
    pushup(pos);
}

int query(int pos,int l,int r)
{
    if(l <= tree[pos].l && tree[pos].r <= r)    return tree[pos].x;
    pushdown(pos);
    int mid = (tree[pos].l+tree[pos].r)/2;
    if(r <= mid)    return query(pos<<1,l,r);
    if(l > mid)     return query(pos<<1|1,l,r);
    return max(query(pos<<1,l,r),query(pos<<1|1,l,r));
}

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> k;
    memset(lastt,-1,sizeof(lastt));
    int cnt = 0;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        pre[i] = lastt[a[i]];
        lastt[a[i]] = i;
        if(pre[i] == -1)    cnt++;
        dp[i] = cnt;
    }
    for(int kk = 2;kk <= k;kk++)
    {
        build(1,1,n);
        for(int i = kk;i <= n;i++)
        {
            update(1,max(1,pre[i]),i-1);
            dp[i] = query(1,1,i-1);
        }
    }
    cout << dp[n] << endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zhurb/p/7270078.html