CF337

CF337

 

Training first individual race, feel their own lack of stamina, very false.

While the final did not see the problem solved all the problems, but this is my speed is too slow.

 

A

 

Order a row

 

B

 

0/1 of the cases require special consideration, it is still not OK, consider not comprehensive

 

C

 

Greedy think, must be wrong more cost-effective in the back. The question then is how to count consecutive front.

 

Let n be a multiple of k

 

F[n] = (F[n-k] + k) * 2;

 

F[n] = ((F[n-2k]+k)*2 + k) * 2;

 

Layers down to write, I found the law

 

D

 

A tree dp, there are a few details need special sentence of

 

#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
int head[N] = {0}, to[N*2], nt[N*2], tot = 0, vis[N] = {0};
void add(int x, int y)
{
    ++tot;nt[tot]=head[x];to[tot]=y;head[x]=tot;
    ++tot;nt[tot]=head[y];to[tot]=x;head[y]=tot;
}
int down[N] = {0}, up[N] = {0};
void dfs1(int now, int pre)
{
//    printf("%d --> %d\n", pre, now);
    int maxx[2];
    maxx[0] = maxx[1] = 0;
    for(int i = head[now]; i; i = nt[i])
    {
        if(to[i]==pre) continue;
        dfs1(to[i], now);
        if(vis[to[i]]) 
        {
            down[now] = max(down[now], 1);
            if(1>maxx[1])
            {
                maxx[0] = maxx[1];
                maxx[1] = 1;
            }else if(1>maxx[0])
            {
                maxx[0] = 1;
            }
        }
        if(down[to[i]]==0) continue;
        down[now] = max(down[now], down[to[i]] + 1);
        if(down[to[i]]+1>maxx[1])
        {
            maxx[0] = maxx[1];
            maxx[1] = down[to[i]]+1;
        }else if(down[to[i]]+1>maxx[0])
        {
            maxx[0] = down[to[i]]+1;
        }
    }
    
    for(int i = head[now]; i; i = nt[i])
    {
        if(to[i]==pre) continue;
        if(vis[now]) up[to[i]] = max(up[to[i]], 1);
        if(up[now]) up[to[i]] = max(up[to[i]], up[now] + 1);
        if((down[to[i]] && down[to[i]]+1==maxx[1]) || (vis[to[i]] && maxx[1]==1)) 
        {
        //    printf(">>%d %d %d %d<<\n", now, to[i], maxx[0], maxx[1]);
            if(maxx[0]) up[to[i]] = max(up[to[i]], maxx[0]+1);
        }
        else if(maxx[1]) 
        {
            up[to[i]] = max(up[to[i]], maxx[1]+1);
        }
    //    printf("debug : %d - %d\n", to[i], up[to[i]]);
    }
}
void dfs2(int now, int pre)
{
    for(int i = head[now]; i; i = nt[i])
    {
        if(pre==to[i]) continue;
        if(up[now]) up[to[i]] = max(up[to[i]], up[now]+1);
        dfs2(to[i], now);
    }
}
int main()
{
    int n, m, d, x, y;
    scanf("%d %d %d", &n, &m, &d);
    
    for(int i = 1; i <= m; i++)
    {
        scanf("%d", &x);
        vis[x] = 1;
    }
    
    for(int i = 1; i < n; i++)
    {
        scanf("%d %d", &x, &y);
        add(x, y);
    }
    
    dfs1(1, 0);
    
//    for(int i = 1; i <= n; i++)
//    {
//        printf("%d --  %d vs %d\n", i, down[i], up[i]);
//    }
//    
//    printf("\n\n");
    
    dfs2(1, 0);
    
//    for(int i = 1; i <= n; i++)
//    {
//        printf("%d --  %d vs %d\n", i, down[i], up[i]);
//    }
    
    int res = 0;
    for(int i = 1; i <= n; i++) if(up[i]<=d && down[i]<=d) res++;
    
    
    
    printf("%d\n", res);
}

 

 

E

 

It is the first to write a violence, then they will have to spend optimization, then this question pass by, to be honest, nausea.

Himself because of problems long long white hair for several

Topic done, then I think, really think he did not entirely due to white.

This (8! * 8 * 1e6 will indeed high probability T)

 

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
ll a[10], rest[10];
int n, res = 1e9, pre[10], tmp, size[10], flag = 0;
vector <int> ve[10];
map <ll, ll> mp;
int count(ll x)
{
    if(mp[x]) return mp[x];
    ll tmp = x;
    ll res = 0;
    for(ll i = 2; i * i <= x; i++)
    {
        if(x%i==0)
        {
            while(x%i==0)
            {
                x /= i;
                res++;
            }
        }
    }
    if(x>1) res++;
    return mp[tmp] = res;
}
bool prime(ll x)
{
    if(x==2 || x==3) return true;
    for(ll i = 2; i * i <= x; i++)
        if(x%i==0) return false;
    return true;
}
int isprime[10] = {0};
void cal(int now)
{
    tmp++;
    if(size[now]==0)
    {
        if(!isprime[now]) tmp += count(a[now]);
        return;
    }
    if(now)
    {
        if(rest[now]) tmp += count(rest[now]);
    }
    for(int i = 0; i < size[now]; i++)
    {
        cal(ve[now][i]);
        if(flag) return;
    }
}
void dfs(int pos)
{
    if(pos==n+1)
    {
        tmp = 0; flag = 0;
        for(int i = 0; i <= n; i++)
        {
            ve[i].clear();
            size[i] = 0;
        }
        for(int i = 1; i <= n; i++)
        {
            size[pre[i]]++;
            ve[pre[i]].push_back(i);
        }
        cal(0);
        if(size[0]==1)
        {
            tmp -= 1;
        }
        if(flag) tmp = 1e9;
        if(tmp < res)
        {
            res = tmp;
        
        }
        return ;
    }
    for(int i = 0; i < pos; i++)
    {
        if(i && rest[i]%a[pos]) continue;
        rest[i] = rest[i] / a[pos];
        pre[pos] = i;
        dfs(pos+1);
        rest[i] *= a[pos];
    }
}
int main()
{

    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    scanf("%lld", a+i);

    sort(a+1, a+n+1, greater<ll>() );
    
    for(int i = 1; i <= n; i++) 
    {
        rest[i] = a[i];
        if(prime(a[i])) isprime[i] = 1;
    }
    
    dfs(1); 

    printf("%d\n", res);
}

Guess you like

Origin www.cnblogs.com/loenvom/p/12173442.html