Comet OJ - Contest #5

Comet OJ - Contest #5

總有一天,我會拿掉給\(dyj\)的小裙子的.ios

A

顯然git

\(ans = min(cnt_1/3,cnt_4/2,cnt5)\)

B

咱們能夠感性理解一下,最大的知足條件的\(x\)不會太大數組

由於當\(x\)愈來愈大時\(f(x)\)的增加速度比\(x\)的增加速度慢得多優化

其實能夠證實,最大的知足的\(x\)不會超過\(100\)spa

由於沒有任何一個三位數的各位之和大於等於\(50\)指針

因此咱們就直接預處理\(1-99\)全部的合法的code

暴力枚舉便可ci

其實賽後題解說知足條件的\(x\)只有\(17\)\(18\)get

#include<cstdio>
#include<cstring>
#include<cctype>
#include<iostream>
#include<algorithm>
using namespace std;
//int a[N];
bool book[32131];
long long n;
inline int work(int x){
    int ans = 0;
    while(x){
        ans += x % 10;
        x /= 10;    
    }
    return ans;
}
int main(){
    int T;
    for(int i = 2;i <= 100;++i){
        if(work(i) == i / 2) book[i] = 1;   
    }
    cin >> T;
    while(T--){
        int res = 0;
        scanf("%lld",&n);   
        for(int i = 2;i <= 100;++i)
        if(book[i] == 1 && n % i == 0) res++;
        printf("%d\n",res);
    }
    return 0;   
}

C

一棵根爲\(1\)的樹,咱們要給每一個點染色,\(a_i\)表示在第\(i\)的點染色前,全部深度大於\(a_i\)的點都不能有顏色,求字典序最小的染色順序.\((n <=5*10^5)\)string

保證\(a_i>=deep_i\)

咱們抽象一下,發現你直接將全部的同深度的點當作一個,直接線段樹優化建圖跑\(DAG\)便可.可是我不會線段樹優化建圖

咱們試想一下,將同深度的點當作一個點,跑\(DAG\)的大致思路是沒有錯的.問題就是若是不能線段樹優化建圖,就很是難作.由於咱們不知道那個點的入度爲\(0\)

以後發現,咱們能夠開樹狀數組維護每一個點的度數,區間修改,單點查詢.

咱們發現,每次加入的點的深度必定是遞增的.

由於若是\(i\)\(j\)有限制,\(i\)\(j + 1\)也必定有限制

以後咱們能夠維護一個指針,每次入隊時查詢當前深度是否入度爲\(0\)

另外因爲要求字典序最小,因此要用小根堆.

時間複雜度\(O(nlogn)\)

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N = 5e5 + 3;
vector <int> G[N];
vector <int> g[N];
int deep[N];
int n,maxdeep;
bool vis[N];
int fa[N];
int a[N];
//inline 
struct BIT{
    int c[N];
    inline void add(int x,int v){
        for(;x <= n;x += x & -x) c[x] += v;
    }   
    inline int query(int x){
        int res = 0;
        for(;x;x -= x & -x) res += c[x];
        return res; 
    }
}T;
inline int read(){
    int v = 0,c = 1;char ch = getchar();
    while(!isdigit(ch)){
        if(ch == '-') c = -1;
        ch = getchar();
    }
    while(isdigit(ch)){
        v = v * 10 + ch - 48;
        ch = getchar(); 
    }
    return v * c;
}
inline void dfs(int x,int f,int dep){
    deep[x] = dep;
    maxdeep = max(maxdeep,dep);
    fa[x] = f;
    for(int i = 0;i < (int)G[x].size();++i){
        int y = G[x][i];
        if(y == f) continue;
        dfs(y,x,dep + 1);
    }
}
inline void work(){
    priority_queue <int,vector<int>,greater<int> > q;
    int now = 0;
    for(int i = 1;i <= n;++i)
        if(T.query(deep[i]) == 0) vis[i] = 1,q.push(i),now = max(now,deep[i]);
    now++;
    while(!q.empty()){
        int k = q.top();q.pop();
        printf("%d ",k);
        T.add(a[k] + 1,-1);
        while(now <= maxdeep && T.query(now) == 0){
            for(int i = 0;i < (int)g[now].size();++i)
            q.push(g[now][i]);
            now++;
        }
    }
}
int main(){
    n = read();
    for(int i = 1;i < n;++i){
        int x = read(),y = read();
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int i = 1;i <= n;++i) a[i] = read();
    dfs(1,0,1);
    for(int i = 1;i <= n;++i) {
        T.add(a[i] + 1,1);
        g[deep[i]].push_back(i);
    }
    work();
    return 0 ;
}
相關文章
相關標籤/搜索