Codeforces Round #686 (Div. 3)

Codeforces Round #686 (Div. 3)

温暖的Div3

 

A - Special Permutation

构造一个序列每个数字和他的下标不等

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define ls u<<1
#define rs u<<1|1
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define REP(i,a,n) for(int i=a;i>=n;--i)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _, n;

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}


int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    
    _ = read();
    while (_--) {
        n = read();
        if (n & 1) {
            cout << "3 1 2 ";
            for (int i = 4; i <= n; ++i) {
                if (i & 1)
                    cout << i << " " << i - 1 << " ";
            }
        } else {
            for (int i = 1; i <= n; ++i) {
                if (i & 1) continue;
                    cout << i << " " << i - 1 << " ";
            }
        }
        puts("");
    }

    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

B - Unique Bid Auction

求最小的唯一元素的下标,不存在就输出-1

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define ls u<<1
#define rs u<<1|1
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define REP(i,a,n) for(int i=a;i>=n;--i)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _, n;
int id[N], num[N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}


int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    
    _ = read();
    while (_--) {
        n = read();
        for (int i = 1; i <= n; ++i) {
            id[i] = 0;
            num[i] = 0;
        }
        rep(i, 1, n) {
            int x;
            x = read();
            id[x] = i;
            num[x]++;
        }
        int pos = -1;
        for (int i = 1; i <= n; ++i) {
            if (num[i] == 1) {
                pos = id[i];
                break;
            }
        }
        cout << pos << '\n';
    }

    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

C - Sequence Transformation

先自己选好一个x ,然后每次操作选择一个区间,可以清楚区间内所有元素(注意选择的区间内不可以出现x,即不可以删出x)

要求输出最少的操作次数使得最终序列仅有x

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define ls u<<1
#define rs u<<1|1
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define REP(i,a,n) for(int i=a;i>=n;--i)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 2e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _, n;
int a[N], num[N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}


int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    
    _ = read();
    while (_--) {
        n = read();
        int cnt = 0;
        rep(i, 1, n) {
            num[i] = 0;
            int x;
            x = read();
            if (x == a[cnt]) continue;
            a[++cnt] = x;
        }
        rep(i, 1, cnt) {
            num[a[i]]++;
        }
        int minn = INF;
        rep(i, 1, cnt) {
            int sum = num[a[i]] + 1;
            if (a[1] == a[i]) sum--;
            if (a[cnt] == a[i]) sum--;
            minn = min(minn, sum);
        }
        cout << minn << '\n';
    }

    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

D - Number into Sequence

输入n,要求构造一个序列,使得序列的每个元素都大于1,且前面的元素可以被后面所有的元素整除并且要满足序列中所有元素的乘积为n。

显然考虑唯一分解

对于只有一个质因数的数,只需全部输出即可

否则可以发现,质因数之间一定是互质的,所以我们先输出出现次数最多的质因数,输出个数为其出现次数减一,最后在输出一个数,为剩余质因素乘积再乘上一个最大质因数

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define ls u<<1
#define rs u<<1|1
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define REP(i,a,n) for(int i=a;i>=n;--i)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _;
ll n, m;
ll p[N], c[N];
vector<ll>ans;

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void divide(ll x) {
    m = 0;
    for (ll i = 2; i * i <= x; i ++) {
        if (x % i == 0) {
            m++;
            c[m] = 0;
            p[m] = i;
            while (x % i == 0) {
                c[m]++;
                x /= i;
            }
        }
    }
    if (x > 1) {
        p[++m] = x;
        c[m] = 1;
    }
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //     freopen("in.txt","r",stdin);
    //     freopen("out.txt","w",stdout);
    // #endif

    _ = read();
    while (_--) {
        m = 0;
        n = read();
        divide(n);
        if (m == 1) {
            cout << c[1] << '\n';
            for (int i = 1; i <= c[1]; ++i) cout << p[1] << " ";
            puts("");
        } else {
            int pos, maxx = -1;
            for (int i = 1; i <= m; ++i) {
                if (c[i] > maxx) {
                    maxx = c[i];
                    pos = i;
                }
            }
            ans.clear();
            ll sum = 1;
            for (int i = 1; i <= maxx - 1; ++i) ans.push_back(p[pos]), sum *= p[pos];
            ans.push_back(n / sum);
            cout << ans.size() << '\n';
            for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " ";
            puts("");
        }
    }

    #ifndef ONLINE_JUDGE
        system("pause");
    #endif
}
View Code

 

E - Number of Simple Paths

题目给出的图是一个基环树,分类讨论

对于环上的点,每两个点之间都有两条路径,贡献为cnt*(cnt -1)

对于树上的点,每两个点之间都有一条路径,贡献为cnt*(cnt -1)/ 2

最后考虑某一棵树上的点和非树上的点贡献

一种是树上点和环上点,另一种是树上点和别的树上的点,都是任意两个点之间都有两条简单路径

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define ls u<<1
#define rs u<<1|1
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define REP(i,a,n) for(int i=a;i>=n;--i)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const ll N = 2e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
ll _, n, idx, cnt, num;
ll e[M], ne[M], h[N];
ll deg[N], a[N], vis[N];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void init() {
    idx = 0;
    cnt = 0;
    for (int i = 0; i <= n; ++i) {
        h[i] = -1;
        vis[i] = 0;
        deg[i] = 0;
    }
}

void add(int a, int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

void dfs(int u, int fa) {
    num++;
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (v == fa || vis[v] == 1) continue;
        dfs(v, u);
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    
    _ = read();
    while (_--) {
        n = read();
        init();
        rep(i, 1, n) {
            int u, v;
            u = read(); v = read();
            add(u, v);
            add(v, u);
            deg[u]++;
            deg[v]++;
        }

        queue<int>q;
        rep(i, 1, n) {
            a[i] = deg[i];
            if (deg[i] == 1) q.push(i);
        }

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            vis[u] = 1;
            for (int i = h[u]; ~i; i = ne[i]) {
                int v = e[i];
                a[v]--;
                if (a[v] == 1) q.push(v);
            }
        }
        rep(i, 1, n) {
            vis[i] = 1 - vis[i];
            if (vis[i] == 1) cnt++;
        }
        
        ll ans = cnt * (cnt - 1);
        // printf("ans1 = %lld\n", ans);
        rep(i, 1, n) {
            if (vis[i] == 0) continue;
            num = 0;
            dfs(i, -1);
            // printf("num = %lld\n", num);
            ans = ans + num * (num - 1) / 2;
            ans = ans + (num - 1) * (cnt - 1) * 2;
            ans = ans + (num - 1) * (n - num + 1 - cnt);
        }
        cout << ans << '\n';
    }
    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

F - Array Partition

显然有单调性,枚举前缀最大值和后缀最大值,再搞一颗线段树维护一下。

枚举x的左端点,后面那部分二分一下

为什么说的这么模糊呢,因为是看的题解

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define ls u<<1
#define rs u<<1|1
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define REP(i,a,n) for(int i=a;i>=n;--i)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int _, n;
int a[N], b[N], arr[N];

struct node {
    int l, r, maxx, minn;
}tr[N<<2];

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void pushup(int u) {
    tr[u].maxx = max(tr[ls].maxx, tr[rs].maxx);
    tr[u].minn = min(tr[ls].minn, tr[rs].minn);
}

void build(int u, int l, int r) {
    tr[u] = {l, r};
    if (l == r) {
        tr[u] = {l, l, a[l], a[l]};
        return;
    }
    int mid = l + r >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
    pushup(u);
}

int qmax(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) {
        return tr[u].maxx;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    int ans = 0;
    if (mid >= l) ans = max(ans, qmax(ls, l, r));
    if (mid + 1 <= r) ans = max(ans, qmax(rs, l, r));
    return ans;
}

int qmin(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) {
        return tr[u].minn;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    int ans = INF;
    if (mid >= l) ans = min(ans, qmin(ls, l, r));
    if (mid + 1 <= r) ans = min(ans, qmin(rs, l, r));
    return ans;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif

    _ = read();
    while (_--) {
        n = read();
        rep(i, 1, n) a[i] = read(), b[i] = 0;

        b[n + 1] = 0;
        REP(i, n, 1) b[i] = max(b[i + 1], a[i]);


        build(1, 1, n);
        
        int Max = 0;
        int f = 0;
        for (int i = 1; i < n; ++i) {
            Max = max(Max, a[i]);
            int l = i, r = n;

            while (l + 1 < r) {
                int mid = l + r >> 1;
                int Min = qmin(1, i + 1, mid);
                if (Max > Min) r = mid;
                else if (Max < Min) l = mid;
                else {
                    if (Max > b[mid + 1]) r = mid;
                    else if (Max < b[mid + 1]) l = mid;
                    else {
                        f = mid;
                        break;
                    }
                }
            }
            if (f) {
                puts("YES");
                cout << i << " " << f - i << " " << n - f << endl;
                break;
            }
        }
        if (!f) puts("NO");
    }
    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}
View Code

 

 

#include <stdio.h>#include <iostream>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <map> #include <stack>#include <sstream>#include <set>// #pragma GCC optimize(2)
//#define int long long#define ls u<<1#define rs u<<1|1#define rep(i,a,n) for(int i=a;i<=n;i++)#define REP(i,a,n) for(int i=a;i>=n;--i)#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);#define mm(i,v) memset(i,v,sizeof i);#define mp(a, b) make_pair(a, b)#define pi acos(-1)#define fi first#define se second
using namespace std;typedef long long ll;typedef double db;typedef pair<ll, ll > PII;priority_queue< PII, vector<PII>, greater<PII> > que;stringstream ssin; //  ssin << string   while ( ssin >> int)const ll LINF = 0x7fffffffffffffffll;
const ll N = 2e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;ll _, n, idx, cnt, num;ll e[M], ne[M], h[N];ll deg[N], a[N], vis[N];
inline ll read() {    char c=getchar();ll x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}    return x*f;}
void init() {    idx = 0;    cnt = 0;    for (int i = 0; i <= n; ++i) {        h[i] = -1;        vis[i] = 0;        deg[i] = 0;    }}
void add(int a, int b) {    e[idx] = b;    ne[idx] = h[a];    h[a] = idx++;}
void dfs(int u, int fa) {    num++;    for (int i = h[u]; ~i; i = ne[i]) {        int v = e[i];        if (v == fa || vis[v] == 1) continue;        dfs(v, u);    }}
int main(){    #ifndef ONLINE_JUDGE        freopen("in.txt","r",stdin);        freopen("out.txt","w",stdout);    #endif        _ = read();    while (_--) {        n = read();        init();        rep(i, 1, n) {            int u, v;            u = read(); v = read();            add(u, v);            add(v, u);            deg[u]++;            deg[v]++;        }
        queue<int>q;        rep(i, 1, n) {            a[i] = deg[i];            if (deg[i] == 1) q.push(i);        }
        while (!q.empty()) {            int u = q.front();            q.pop();            vis[u] = 1;            for (int i = h[u]; ~i; i = ne[i]) {                int v = e[i];                a[v]--;                if (a[v] == 1) q.push(v);            }        }        rep(i, 1, n) {            vis[i] = 1 - vis[i];            if (vis[i] == 1) cnt++;        }                ll ans = cnt * (cnt - 1);        // printf("ans1 = %lld\n", ans);        rep(i, 1, n) {            if (vis[i] == 0) continue;            num = 0;            dfs(i, -1);            // printf("num = %lld\n", num);            ans = ans + num * (num - 1) / 2;            ans = ans + (num - 1) * (cnt - 1) * 2;            ans = ans + (num - 1) * (n - num + 1 - cnt);        }        cout << ans << '\n';    }    // #ifndef ONLINE_JUDGE    //     system("pause");    // #endif}

 

posted @ 2020-11-26 14:55  violet72  阅读(33)  评论(0编辑  收藏  举报