1. 程式人生 > >2017-2018 ACM-ICPC, Asia Daejeon Regional Contest

2017-2018 ACM-ICPC, Asia Daejeon Regional Contest

const 最小割 gif 否則 struct 後綴 tor getchar int()

題目傳送門

只打了三個小時。

A. Broadcast Stations

B. Connect3

補題:zz

題解:

C. Game Map

技術分享圖片
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=100100;
vector<int>ve[100010];
unordered_map<int,int>mp[100010];
int c[100010],de[100010
]; struct s { int de,id; }z[100010]; inline bool comp(s a,s b) { return a.de <b.de; } int main(){ int n,m,i,z1,z2,si,j,ans; while(~scanf("%d %d",&n,&m)) { for(i = 0;i <= n;i++) { ve[i].clear(); mp[i].clear(); c[i]
= 1; z[i].de = 0; z[i].id = i; de[i] = 0; } for(i = 0;i < m;i++) { scanf("%d %d",&z1,&z2); if(z1 > z2) { int r = z1; z1 = z2; z2 = r; }
if(z1 != z2 && !mp[z1][z2]) { mp[z1][z2] = 1; ve[z1].push_back(z2); ve[z2].push_back(z1); z[z1].de++; z[z2].de++; de[z1]++; de[z2]++; } } sort(z,z + n,comp); ans = 1; for(i = 0;i < n;i++) { si = ve[z[i].id].size(); //printf(" %d %d\n",z[i].id,z[i].de); for(j = 0;j < si;j++) { //printf(" %d %d %d\n",z[i].de,z[ve[z[i].id][j]].de,ve[z[i].id][j]); if(z[i].de > de[ve[z[i].id][j]]) { c[z[i].id] = max(c[z[i].id],c[ve[z[i].id][j]] + 1); } } //printf(" %d\n",c[z[i].id]); ans = max(ans,c[z[i].id]); } printf("%d\n",ans); } }
View Code

D. Happy Number

技術分享圖片
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#define ll long long
#define maxn 4001000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int n,len;
map<int,int> mp;
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
void init()
{
    scanf("%d",&n); 
    len=1;
    mp[n]=1;
}
void work()
{
    while(1)
    {
        int x=n,ans=0;
        while(x>0)
        {
            ans+=(x%10)*(x%10);
            x/=10; 
        }
        if(ans==1) 
        {
            printf("HAPPY");
            return;
        }
        if(mp[ans]!=0) 
        {
            printf("UNHAPPY");
            return;
        }
        else mp[ans]=++len;
        n=ans;
    }
}
int main()
{
    init();
    work();
}
View Code

E. How Many to Be Happy

補題:kk

  一開始想了個假算法,沒被隊友hack,然後wa3?最近好像經常寫假算法

  對於這樣最小生成樹的題的選邊問題,我們一定要考慮克魯斯卡爾的過程,對於一條邊權為$w$的邊,如果他要在最小生成樹上,那麽前提條件是權值比他小的邊不會把u到v所屬的兩個結合連接在一起,也就是說,現在我們要去掉一些權值小於$w$的邊,讓u和v分在不同的兩個集合裏面,想到這裏就會想到最小割的模型,對於每條邊,都把權值小於他的邊加入到圖裏,然後求一個最小割就可以了。

  所以,最小生成樹的題一定要考慮克魯斯卡爾!這真是一個神奇的算法,還有對數據範圍要敏感,$n=100$也應該想到網絡流。

技術分享圖片
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;

const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int maxn = 510;

struct Edge {
    int to, flow, nxt;
    Edge(){}
    Edge(int to, int nxt, int flow):to(to),nxt(nxt), flow(flow){}
}edge[maxn << 2];

int head[maxn], dep[maxn];
int S, T;
int N, n, m, tot,ans;

void Init(int n)
{
    N = n;
    for (int i = 0; i < N; ++i) head[i] = -1;
    tot = 0;
}

void addv(int u, int v, int w, int rw = 0)
{
    edge[tot] = Edge(v, head[u], w); head[u] = tot++;
    edge[tot] = Edge(u, head[v], rw); head[v] = tot++;
}

bool BFS()
{
    for (int i = 0; i < N; ++i) dep[i] = -1;
    queue<int>q;
    q.push(S);
    dep[S] = 1;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; ~i; i = edge[i].nxt)
        {
            if (edge[i].flow && dep[edge[i].to] == -1)
            {
                dep[edge[i].to] = dep[u] + 1;
                q.push(edge[i].to);
            }
        }
    }
    return dep[T] < 0 ? 0 : 1;
}

int DFS(int u, int f)
{
    if (u == T || f == 0) return f;
    int w, used = 0;
    for (int i = head[u]; ~i; i = edge[i].nxt)
    {
        if (edge[i].flow && dep[edge[i].to] == dep[u] + 1)
        {
            w = DFS(edge[i].to, min(f - used, edge[i].flow));
            edge[i].flow -= w;
            edge[i ^ 1].flow += w;
            used += w;
            if (used == f) return f;
        }
    }
    if (!used) dep[u] = -1;
    return used;
}

int Dicnic()
{
    int ans = 0;
    while (BFS())
    {
        ans += DFS(S, INF);
    }
    return ans;
}
struct node{
    int u,v,w;
    friend bool operator<(const node &a,const node &b)
    {
        return a.w<b.w;
    }
}a[maxn];
int main(){
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
        }
        ans=0;
        sort(a+1,a+1+m);
        for(int i=1;i<=m;i++)
        {
            Init(n+1);
            for(int j=1;j<i;j++)
            {
                if(a[j].w>=a[i].w)break;
                addv(a[j].u,a[j].v,1);
                addv(a[j].v,a[j].u,1);
            }
            S=a[i].u,T=a[i].v;
            ans+=Dicnic();
    //        printf("debug\n");
        }
        printf("%d\n",ans);
    }
}
View Code

F. Philosphoer‘s Walk

技術分享圖片
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=510;
struct s
{
    int a,b;
}in;
int Pow[110];
int Node[5][2] = {0,0,1,1,1,2,2,2,2,1};
inline void init(void)
{
    int i,tmp = 1;
    for(i = 0;i <= 30;i++)
    {
        Pow[i] = tmp;
        tmp <<= 1;
    }
}
inline s f(int num,int step)
{
    int pos,st;
    s jk;
    if(num == 1)
    {
        s tmp;
        tmp.a = Node[step][0];
        tmp.b = Node[step][1];
        return tmp;
    }
    pos = (step - 1) / (Pow[num - 1] * Pow[num - 1]);
    st = (step - 1) % (Pow[num - 1] * Pow[num - 1]) + 1;
    s nex = f(num - 1,st);
    if(pos == 0)
    {
        jk.a = 0 + nex.b;
        jk.b = 0 + nex.a;
    }
    else if(pos == 1)
    {
        jk.a = 0 + nex.a;
        jk.b = Pow[num - 1] + nex.b;
    }
    else if(pos == 2)
    {
        jk.a = Pow[num - 1] + nex.a;
        jk.b = Pow[num - 1] + nex.b;
    }
    else
    {
        jk.a = Pow[num] + 1 - nex.b;
        jk.b = Pow[num - 1] + 1 - nex.a;
    }
    return jk;
}
int main(){
    int n,m,i;
    init();
    while(~scanf("%d %d",&n,&m))
    {
        for(i = 1;i <= 30;i++)
        {
            if(Pow[i] == n)
            {
                break;
            }
        }
        in = f(i,m);
        printf("%d %d\n",in.a,in.b);
    }
}
View Code

G. Rectilinear Regions

H. Rock Paper Scissors

技術分享圖片
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn (1<<18)
#define pi 3.141592653589793238462643383
using namespace std;

struct complex
{
    double re,im;
    complex(double r=0.0,double i=0.0) {re=r,im=i;}
    void print() {printf("%.lf ",re);}
} a[maxn*2],b[maxn*2],W[2][maxn*2];

int N,na,nb,rev[maxn*2],n,m;
char s1[101010],s2[101010];
double c[maxn*2],ans;
complex operator +(const complex&A,const complex&B) {return complex(A.re+B.re,A.im+B.im);}
complex operator -(const complex&A,const complex&B) {return complex(A.re-B.re,A.im-B.im);}
complex operator *(const complex&A,const complex&B) {return complex(A.re*B.re-A.im*B.im,A.re*B.im+A.im*B.re);}

void FFT(complex*a,int f)
{
    complex x,y;
    for (int i=0; i<N; i++) if (i<rev[i]) swap(a[i],a[rev[i]]);
    for (int i=1; i<N; i<<=1)
        for (int j=0,t=N/(i<<1); j<N; j+=i<<1)
            for (int k=0,l=0; k<i; k++,l+=t) x=W[f][l]*a[j+k+i],y=a[j+k],a[j+k]=y+x,a[j+k+i]=y-x;
    if (f) for (int i=0; i<N; i++) a[i].re/=N;
}

void work()
{
    for (int i=0; i<N; i++)
    {
        int x=i,y=0;
        for (int k=1; k<N; x>>=1,k<<=1) (y<<=1)|=x&1;
        rev[i]=y;
    }
    for (int i=0; i<N; i++) W[0][i]=W[1][i]=complex(cos(2*pi*i/N),sin(2*pi*i/N)),W[1][i].im=-W[0][i].im;
}
void doit()
{
    work(),FFT(a,0),FFT(b,0);
    for (int i=0; i<N; i++) a[i]=a[i]*b[i];
    FFT(a,1);
    for (int i=0; i<na+nb-1; i++) 
    {
        c[i]+=a[i].re;
        if(i>=m-1) ans=max(ans,c[i]);
        //printf("%d %d\n",i,c[i]);
    }
}

void popo()
{   
    scanf("%d%d",&n,&m);
    scanf("%s",s1);
    scanf("%s",s2);
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    na=n;nb=m;
    for(int i=0;i<n;i++)    
        if(s1[i]==R) a[i].re=1;
    for(int i=0;i<m;i++)
        if(s2[i]==P) b[m-i-1].re=1;
    for (N=1; N<na||N<nb; N<<=1); N<<=1;
    doit();
    //printf("------\n");
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(int i=0;i<n;i++)    
        if(s1[i]==P) a[i].re=1;
    for(int i=0;i<m;i++)
        if(s2[i]==S) b[m-i-1].re=1;
    for (N=1; N<na||N<nb; N<<=1); N<<=1;
    doit();
    //printf("------\n");
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(int i=0;i<n;i++)    
        if(s1[i]==S) a[i].re=1;
    for(int i=0;i<m;i++)
        if(s2[i]==R) b[m-i-1].re=1;
    for (N=1; N<na||N<nb; N<<=1); N<<=1;
    doit();
}


int main()
{
    popo();
    printf("%.lf\n",ans);
}
View Code

I. Slot Machines

補題kk

  我們把這個數組倒過來,現在變成了我們要去掉末尾的一段,使得前面部分循環,並且不完整的循環節在數列的前方。對於兩段循環來說,開頭一部分肯定是一樣的(否則怎麽能叫循環呢),如果有一段循環不完整,那麽開頭一部分肯定是一樣的,而這裏循環的開頭就是倒過來的數組的前綴,後面是一個完整的數組,前綴和後綴相同,我們想到了什麽呢?kmp!

  但是這裏我們只需要$fail$就可以了。

  先對倒過來的數組$fail$一遍,得到$f$數組,然後循環長度就變成了$i-f[i]$,對此我們稍微證明一下為什麽。

  對於$i-f[i]>=f[i]$,也就是相等的前綴和後綴不重疊,那麽這必然是一個循環長度。

  對於$i-f[i]<f[i]$,也就是相等的前綴喝後綴是重疊的,那麽我們就要稍微理解一下,設$len=i-f[i]$那麽必然有$s{f[i]->f[i]+len]}==s{f[i]-len->f[i]}$,然後一步一步的畫過來,這個也是循環節(這部分還是畫圖好理解些)

技術分享圖片
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1000010;
int n;
int a[maxn],f[maxn];
void fail(){
    f[0]=-1;
    for(int j=1;j<n;j++)
    {
        for(int i=f[j-1];;i=f[i])
        {
            if(a[j]==a[i+1]){
                f[j]=i+1;
                break;
            }else if(i==-1)
            {
                f[j]=-1;
                break;
            }
        }
    }
}
int main(){
    while(cin>>n)
    {
        for(int i=n-1;i>=0;i--)
        {
            scanf("%d",&a[i]);
        }
        fail();
        int k=n,p=1;
        for(int i=0;i<n;i++)
        {
            int kk=n-i-1;
            int pp=i-f[i];
            if(kk+pp<k+p){
                k=kk,p=pp;
            }else if(kk+pp==k+p&&pp<p){
                k=kk,p=pp;
            }
        }
        printf("%d %d\n",k,p);
        
    }
}
View Code

J. Strongly Matchable

K. Untangling Chain

L. Vacation Plans

2017-2018 ACM-ICPC, Asia Daejeon Regional Contest