• 2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017)


    Drawing Borders

    很多构造方法,下图可能是最简单的了

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e6+10;
    struct Point{ int x,y; };
    Point a[maxn];  int numa=0;
    Point b[maxn];  int numb=0;
    vector<pair<double,double> > va;
    vector<pair<double,double> > vb;
    void checka()
    {
        va.push_back({3000,3000});
        va.push_back({3000,2999});
        for(int i=numa;i>=1;i--)
        {
            va.push_back( { a[i].x*1.000-0.3,2999} );
            va.push_back( { a[i].x*1.000-0.3,a[i].y*1.000+0.1} );
            va.push_back( { a[i].x*1.000+0.1,a[i].y*1.000+0.1} );
            va.push_back( { a[i].x*1.000+0.1,a[i].y*1.000-0.1} );
            va.push_back( { a[i].x*1.000-0.3,a[i].y*1.000-0.1} );
            while(i-1>=1 && a[i-1].x==a[i].x)
            {
                i--;
                va.push_back( { a[i].x*1.000-0.3,a[i].y*1.000+0.1} );
                va.push_back( { a[i].x*1.000+0.1,a[i].y*1.000+0.1} );
                va.push_back( { a[i].x*1.000+0.1,a[i].y*1.000-0.1} );
                va.push_back( { a[i].x*1.000-0.3,a[i].y*1.000-0.1} );
            }
            va.push_back( {a[i].x*1.0000-0.3,-1200} );
            va.push_back( {a[i].x*1.0000-0.39,-1200} );
            va.push_back( {a[i].x*1.0000-0.39,2999});
        }
        va.push_back({-3000,2999});
        va.push_back({-3000,3000});
    }
    void checkb()
    {
        vb.push_back({-3000,-3000});
        vb.push_back({-3000,-2999});
        for(int i=1;i<=numb;i++)
        {
            vb.push_back( { b[i].x*1.000+0.3,-2999} );
    
    
            vb.push_back( { b[i].x*1.000+0.3,b[i].y*1.000-0.1} );
            vb.push_back( { b[i].x*1.000-0.1,b[i].y*1.000-0.1} );
            vb.push_back( { b[i].x*1.000-0.1,b[i].y*1.000+0.1} );
            vb.push_back( { b[i].x*1.000+0.3,b[i].y*1.000+0.1} );
            while(i+1<=numb && b[i+1].x==b[i].x)
            {
                i++;
                vb.push_back( { b[i].x*1.000+0.3,b[i].y*1.000-0.1} );
                vb.push_back( { b[i].x*1.000-0.1,b[i].y*1.000-0.1} );
                vb.push_back( { b[i].x*1.000-0.1,b[i].y*1.000+0.1} );
                vb.push_back( { b[i].x*1.000+0.3,b[i].y*1.000+0.1} );
            }
            vb.push_back( { b[i].x*1.000+0.3,1200} );
            vb.push_back( { b[i].x*1.000+0.39,1200} );
            vb.push_back( { b[i].x*1.000+0.39,-2999} );
        }
        vb.push_back({3000,-2999});
        vb.push_back({3000,-3000});
    }
    bool upa(Point a,Point b){if(a.x==b.x) return a.y<b.y;return   a.x<b.x;}
    bool upb(Point a,Point b){if(a.x==b.x) return a.y<b.y;return   a.x<b.x;}
    int main()
    {
        int n ; scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int x,y,z; scanf("%d %d %d",&x,&y,&z);
            if(z==1) a[++numa].x=x,a[numa].y=y;
            if(z==2) b[++numb].x=x,b[numb].y=y;
        }
        sort(a+1,a+1+numa,upa);   checka();
        sort(b+1,b+1+numb,upb);   checkb();
        printf("%d
    ",va.size()); for(int i=0;i<va.size();i++) printf("%.4f %.4f
    ",va[i].first,va[i].second);
        printf("%d
    ",vb.size()); for(int i=0;i<vb.size();i++) printf("%.4f %.4f
    ",vb[i].first,vb[i].second);
    }
    View Code

    Buildings

    polya定理

    代码:

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int mod=1e9+7;
    int quick(int a,int n)
    {
        int ans=1;
        while(n)
        {
            if(n%2==0) {a=a%mod*a%mod;n=n/2; }
            else       {ans=ans%mod*a%mod;   n--;   }
        }
        return ans;
    }
    int p[500+10];
    int32_t main()
    {
       int n,m,c; cin>>n>>m>>c;//  m  temp;
       int temp=quick(c,n*n); //cout<<temp<<endl;
       p[0]=1;
       for(int i=1;i<= m;i++) p[i]=p[i-1]%mod*temp%mod;
       int a=0;
       for(int i=0;i<m;i++)  a+=p[__gcd(i,m)],a=a%mod;
       int ans=a*quick(m,mod-2)%mod;
       cout<<ans<<endl;
    }
    View Code

    Joyride

    dp + 记忆化搜索

    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define y1 y11
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define pdd pair<double, double>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //head
    
    const int N = 1e3 + 10;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    LL dp[N][N];
    vector<int> g[N];
    int x, n, m, T, u, v;
    int t[N], p[N];
    LL dfs(int u, int m) {
        if(m > x) return INF;
        if(~dp[u][m]) return dp[u][m];
        if(u == 1 && m == x) return dp[u][m] = 0;
        dp[u][m] = INF;
        for (int v : g[u]) {
            dp[u][m] = min(dp[u][m], dfs(v, m+T+t[v]) + p[v]);
        }
        dp[u][m] = min(dp[u][m], dfs(u, m+t[u])+p[u]);
        return dp[u][m];
    }
    int main() {
        mem(dp, -1);
        scanf("%d", &x);
        scanf("%d %d %d", &n, &m, &T);
        for (int i = 1; i <= m; ++i) scanf("%d %d", &u, &v), g[u].pb(v), g[v].pb(u);
        for (int i = 1; i <= n; ++i) scanf("%d %d", &t[i], &p[i]);
        LL tmp = dfs(1, t[1]) + p[1];
        if(t[1]*2 > x || tmp >= INF) printf("It is a trap.
    ");
        else printf("%lld
    ", tmp);
        return 0;
    }
    View Code

    Pants On Fire

    拓扑排序

    代码:

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    
    /*
    
    ⊂_ヽ
      \\ Λ_Λ  来了老弟
       \('ㅅ')
        > ⌒ヽ
       /   へ\
       /  / \\
       レ ノ   ヽ_つ
      / /
      / /|
     ( (ヽ
     | |、\
     | 丿 \ ⌒)
     | |  ) /
    'ノ )  Lノ
    
    */
    
    using namespace std;
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    //typedef __int128 bll;
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
    
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define boost ios::sync_with_stdio(false);cin.tie(0)
    #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
    #define max3(a,b,c) max(max(a,b), c);
    #define min3(a,b,c) min(min(a,b), c);
    
    
    const ll oo = 1ll<<17;
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 1e9+7;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    const double PHI=0.61803399;    //黄金分割点
    const double tPHI=0.38196601;
    
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    inline void cmax(int &x,int y){if(x<y)x=y;}
    inline void cmax(ll &x,ll y){if(x<y)x=y;}
    inline void cmin(int &x,int y){if(x>y)x=y;}
    inline void cmin(ll &x,ll y){if(x>y)x=y;}
    
    /*-----------------------showtime----------------------*/
                string str[10];
                map<string ,int>mp;
                vector<int>G[509];
                int fa[509],du[509],dig[509];
                int find(int x) {
                    if(fa[x] == x) return x;
                    return fa[x] = find(fa[x]);
                }
                int dfs(int s,int t){
                    int flag = 0;
                    for(int i=0; i<G[s].size(); i++){
                        int v = G[s][i];
                        if(v == t) flag = 1;
                        flag = (flag || dfs(v, t));
                    }
                    return flag;
                }
    int main(){
                int n,m;
                scanf("%d%d", &n, &m);
                int tot = 0;
                rep(i, 1, 508) fa[i] = i;
                rep(i, 1, n) {
                    rep(j, 1, 5) cin>>str[j];
                    int u,v;
                    if(mp.count(str[1]))  u = mp[str[1]];
                    else {
                        ++tot; u = tot;
                        mp[str[1]] = tot;
                    }
    
                    if(mp.count(str[5]))  v = mp[str[5]];
                    else {
                        ++tot; v = tot;
                        mp[str[5]] = tot;
                    }
    
                    int fx = find(u), fy = find(v);
                    fa[fx]=  fy;
                    G[u].pb(v);
                }
    
    
                for(int i=1; i<=m; i++){
                     rep(j, 1, 5) cin>>str[j];
                     if(mp.count(str[1]) == 0 || mp.count(str[5]) == 0) {
                        puts("Pants on Fire");
                        continue;
                     }
                     int u = mp[str[1]];
                     int v = mp[str[5]];
                     if(find(u) != find(v) ) puts("Pants on Fire");
                     else if(dfs(u, v)) puts("Fact");
                     else if(dfs(v, u))puts("Alternative Fact");
                     else puts("Pants on Fire");
                }
                return 0;
    }
    View Code

    Perpetuum Mobile

    spfa判正权环

    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define y1 y11
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define pdd pair<double, double>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //head
    
    const int N = 800 + 10;
    double d[N];
    struct edge {
        int to;
        double w;
    };
    vector<edge> g[N];
    bool vis[N];
    bool spfa(int u) {
        vis[u] = true;
        for (edge v : g[u]) {
            if(d[u] + v.w < d[v.to]) {
                d[v.to] = d[u] + v.w;
                if(vis[v.to]) return true;
                if(spfa(v.to)) return true;
            }
        }
        vis[u] = false;
        return false;
    }
    int main() {
        int n, m, u, v;
        double w;
        scanf("%d %d", &n, &m);
        for (int i = 1; i <= m; ++i) {
            scanf("%d %d %lf", &u, &v, &w);
            g[u].pb({v, -log(w)});
        }
        for (int i = 1; i <= n; ++i) d[i] = 0;
        for (int i = 1; i <= n; ++i) if(spfa(i)) {
            puts("inadmissible");
            return 0;
        }
        puts("admissible");
        return 0;
    }
    View Code

    Plug It In

    枚举三个插座的位置,拆点,然后二分图匹配

    代码:

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    
    /*
    
    ⊂_ヽ
      \\ Λ_Λ  来了老弟
       \('ㅅ')
        > ⌒ヽ
       /   へ\
       /  / \\
       レ ノ   ヽ_つ
      / /
      / /|
     ( (ヽ
     | |、\
     | 丿 \ ⌒)
     | |  ) /
    'ノ )  Lノ
    
    */
    
    using namespace std;
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    //typedef __int128 bll;
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
    
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define boost ios::sync_with_stdio(false);cin.tie(0)
    #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
    #define max3(a,b,c) max(max(a,b), c);
    #define min3(a,b,c) min(min(a,b), c);
    
    
    const ll oo = 1ll<<17;
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 1e9+7;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    const double PHI=0.61803399;    //黄金分割点
    const double tPHI=0.38196601;
    
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    inline void cmax(int &x,int y){if(x<y)x=y;}
    inline void cmax(ll &x,ll y){if(x<y)x=y;}
    inline void cmin(int &x,int y){if(x>y)x=y;}
    inline void cmin(ll &x,ll y){if(x>y)x=y;}
    
    /*-----------------------showtime----------------------*/
                const int maxn = 1509;
                int pt[maxn],vis[maxn];
                struct node{
                    int cnt;
                    int id;
                }a[maxn];
                queue<int>que;
                vector<int>mp[maxn];
                int col = 0;
                bool dfs(int u){
                    if(vis[u] == col) return false;
                    vis[u] = col;
                    for(int i=0; i<mp[u].size(); i++){
                        int v = mp[u][i];
                        if(pt[v] ==0 || dfs(pt[v])) {
                            pt[v] = u;
                            return true;
                        };
                    }
                    return false;
                }
                int in[maxn];
    
                bool cmp(node a,node b){
                    return a.cnt < b.cnt;
                }
    int main(){
                int n,m,k;
                scanf("%d%d%d", &n, &m, &k);
                rep(i, 1, m) a[i].id = i;
                for(int i=1; i<=k; i++){
                    int u,v;
                    scanf("%d%d", &u, &v);
                    mp[v].pb(u);
                    a[v].cnt++;
                }
    
                sort(a+1, a+1+m, cmp);
                int ans = 0;
                for(int i=1; i<=m; i++) {
                       // memset(vis, 0, sizeof(vis));
                        col++;
                        if(dfs(a[i].id) == 0) que.push(a[i].id);
                        else ans++;
                }
                int mx = 0;
                while(!que.empty()){
                    int u = que.front(); que.pop();
                    for(int i=0; i<mp[u].size(); i++)
                        {
                            int v = mp[u][i];
                            in[v]++;
                            mx = max(mx, in[v]);
                        }
                }
                mx = min(2, mx);
                ans += mx;
                printf("%d
    ", ans);
                return 0;
    }
    View Code

    Water Testing

    皮克定理

    代码:

    #include<bits/stdc++.h>
    #define int long long
    #define MAX(a,b,c) max(a,max(b,c))
    #define MIN(a,b,c) min(a,min(b,c))
    #define pb push_back
    #define fi first
    #define se second
    typedef long long ll;
    typedef long long LL;
    typedef unsigned long long ull;
    typedef unsigned long long uLL;
    using namespace std;
    const int maxn=1e6+10;
    const int mod=1e9+7;
    struct Point{int x;int y;}pa[maxn];
    int  Cross(Point A,Point B) { return A.x*B.y-A.y*B.x;   }  // 叉积
    /*bool anglecmp(Point a, Point b)
    {
        if(a.y <= 0 && b.y > 0) return true;
        if(a.y > 0 && b.y <= 0) return false;
        if(!a.y && !b.y) return a.x < b.x;
        return Cross(a,b) > 0;
    }*/
    int work(Point a,Point b)
    {
        return __gcd( abs(a.x-b.x),abs(b.y-a.y) )-1;
    }
    int32_t main()
    {
        int n; scanf("%lld",&n);
        for(int i=1;i<=n;i++)  scanf("%lld %lld",&pa[i].x,&pa[i].y);
        //sort(pa+1,pa+1+n,anglecmp);
        int ans=0;
        for(int i=1;i<n;i++)
        ans+=Cross(pa[i],pa[i+1]);
        ans+=Cross(pa[n],pa[1]);
        ans=abs(ans);
        int x=n;
        for(int i=1;i<n;i++)
        x+=work(pa[i],pa[i+1]);
        x+=work(pa[n],pa[1]);
        int a=(ans-x+2)/2;
        printf("%lld
    ",a);
    }
    View Code

    Ratatoskr

    枚举根,求最长链,然后取最小

    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define y1 y11
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define pdd pair<double, double>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define debug(x) cerr << #x << " = " << x << "
    ";
    
    
    const int N = 85;
    vector<int> g[N];
    int dfs(int u, int o) {
        int res = 1;
        for (int v : g[u]) {
            if(v != o) res = max(res, dfs(v, u)+1);
        }
        return res;
    }
    int main() {
        int n, a, b, c, u, v;
        scanf("%d", &n);
        scanf("%d %d %d", &a, &b, &c);
        for (int i = 1; i < n; ++i) scanf("%d %d", &u, &v), g[u].pb(v), g[v].pb(u);
        int ans = n;
        for (int i = 1; i <= n; ++i) {
            if(i == b || i == c) ans =min(ans, dfs(i, i)-1);
            else ans = min(dfs(i, i), ans);
        }
        printf("%d
    ", ans);
        return 0;
    }
    View Code

    Uberwatch

    dp

    代码:

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    
    /*
    
    ⊂_ヽ
      \\ Λ_Λ  来了老弟
       \('ㅅ')
        > ⌒ヽ
       /   へ\
       /  / \\
       レ ノ   ヽ_つ
      / /
      / /|
     ( (ヽ
     | |、\
     | 丿 \ ⌒)
     | |  ) /
    'ノ )  Lノ
    
    */
    
    using namespace std;
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    //typedef __int128 bll;
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
    
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define boost ios::sync_with_stdio(false);cin.tie(0)
    #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
    #define max3(a,b,c) max(max(a,b), c);
    #define min3(a,b,c) min(min(a,b), c);
    
    
    const ll oo = 1ll<<17;
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 1e9+7;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    const double PHI=0.61803399;    //黄金分割点
    const double tPHI=0.38196601;
    
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    inline void cmax(int &x,int y){if(x<y)x=y;}
    inline void cmax(ll &x,ll y){if(x<y)x=y;}
    inline void cmin(int &x,int y){if(x>y)x=y;}
    inline void cmin(ll &x,ll y){if(x>y)x=y;}
    
    /*-----------------------showtime----------------------*/
                const int maxn = 3e5+9;
                int a[maxn],dp[maxn],res[maxn];
    int main(){
                int n,m;
                scanf("%d%d", &n, &m);
                rep(i, 1, n) scanf("%d", &a[i]);
    
                rep(i, m+1, n) {
                    dp[i] = max(dp[i],res[i-m] + a[i]);
                    res[i] = max(res[i-1], dp[i]);
                }
                printf("%d
    ", res[n]);
                return 0;
    }
    View Code

    Word Clock

    旅行商问题变形,状压dp实现

    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define y1 y11
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define pdd pair<double, double>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    
    const int N = 19;
    const int INF = 0x3f3f3f3f;
    int h, w, n;
    string t[N], s[N];
    int d[N][N], up;
    pii dp[N][1<<N];
    int pre[N][1<<N];
    pii dfs(int u, int st) {
        if(~dp[u][st].fi) return dp[u][st];
        pii res = {INF, INF};
        for (int i = 0; i < n; ++i) {
            if(i == u) continue;
            if(st & (1<<i)) {
                pii p = dfs(i, st^(1<<u));
                p.se += d[i][u];
                if(p.se > w) p.fi++, p.se = s[u].size();
                if(p < res) {
                    res = p;
                    pre[u][st] = i;
                }
            }
        }
        if(res.fi == INF) res.fi = 0, res.se = s[u].size();
        return dp[u][st] = res;
    }
    int main() {
        fio;
        cin >> h >> w >> n;
        int tot = 0;
        for (int i = 0; i < n; ++i) cin >> t[i];
        for (int i = 0; i < n; ++i) {
            bool f = true;
            if(t[i].size() > w) return 0*puts("impossible");
            for (int j = 0; j < n; ++j) {
                if(i == j) continue;
                if(t[j].find(t[i]) == string::npos) continue;
                f = false;
            }
            if(f) s[tot++] = t[i];
        }
        n = tot;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if(i == j) continue;
                int mn = min(s[i].size(), s[j].size());
                d[i][j] = s[j].size();
                for (int k = 1; k <= mn; ++k) {
                    if(s[i].substr(s[i].size()-k, k) == s[j].substr(0, k)) d[i][j] = s[j].size() - k; ///!!!!!!!!!
                }
            }
        }
        up = 1<<n;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < up; ++j)
            dp[i][j] = {-1, -1}, pre[i][j] = -1;
        }
        for (int i = 0; i < n; ++i) {
            pii p = dfs(i, up-1);
            if(p.fi < h) {
                vector<string> res (h, string(w, 'X'));
                vector<int> p;
                int now = i, st = up-1;
                while(~now) {
                    p.pb(now);
                    now = pre[now][st];
                    st ^= 1<<p.back();
                }
                reverse(p.begin(), p.end());
                pii pos = {0, 0};
                for (int i = 0; i < p.size(); ++i) {
                    int x = p[i];
                    if(i == 0) {
                        for (int j = 0; j < s[x].size(); ++j) res[pos.fi][pos.se+j] = s[x][j];
                        pos.se += s[x].size();
                    }
                    else {
                        int u = p[i-1];
                        if(pos.se + d[u][x] > w) {
                            pos.fi++;
                            pos.se = 0;
                            for (int j = 0; j < s[x].size(); ++j) res[pos.fi][pos.se+j] = s[x][j];
                            pos.se = s[x].size();
                        }
                        else {
                            int sz = s[x].size();
                            for (int j = 0; j < d[u][x]; ++j) res[pos.fi][pos.se+j] = s[x][sz-d[u][x]+j];
                            pos.se += d[u][x];
                        }
                    }
                }
                for (int i = 0; i < h; ++i) cout << res[i] << endl;
                return 0;
            }
        }
        return 0*puts("impossible");
    }
    View Code

    You Are Fired

    排序

    代码:

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    
    /*
    
    ⊂_ヽ
      \\ Λ_Λ  来了老弟
       \('ㅅ')
        > ⌒ヽ
       /   へ\
       /  / \\
       レ ノ   ヽ_つ
      / /
      / /|
     ( (ヽ
     | |、\
     | 丿 \ ⌒)
     | |  ) /
    'ノ )  Lノ
    
    */
    
    using namespace std;
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    //typedef __int128 bll;
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
    
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define boost ios::sync_with_stdio(false);cin.tie(0)
    #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
    #define max3(a,b,c) max(max(a,b), c);
    #define min3(a,b,c) min(min(a,b), c);
    
    
    const ll oo = 1ll<<17;
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 1e9+7;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    const double PHI=0.61803399;    //黄金分割点
    const double tPHI=0.38196601;
    
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    inline void cmax(int &x,int y){if(x<y)x=y;}
    inline void cmax(ll &x,ll y){if(x<y)x=y;}
    inline void cmin(int &x,int y){if(x>y)x=y;}
    inline void cmin(ll &x,ll y){if(x>y)x=y;}
    
    /*-----------------------showtime----------------------*/
                const int maxn = 1e4+9;
                struct node{
                    string name;
                    ll x;
                }a[maxn];
                bool cmp(node a, node b){
                    return a.x > b.x;
                }
    int main(){
                ll n, d, k;
                cin>>n>>d>>k;
                rep(i,1, n)
                {
                    cin>>a[i].name>>a[i].x;
                }
                sort(a+1,a+1+n,cmp);
    
                ll sum = 0;
                rep(i,1, k) sum += a[i].x;
                if(sum < d) puts("impossible");
                else {
                    printf("%d
    ", k);
                    rep(i, 1, k) {
                        cout<<a[i].name<<", YOU ARE FIRED!"<<endl;
                    }
    
                }
                return 0;
    }
    View Code
  • 相关阅读:
    Oracle SQL FAQ
    miniasp(no encode)
    请看用javascript设置和读取cookie的简单例子
    asp流下载(Stream)
    (企业公司)网站开发方案
    asp发消息并代多个附件上传(多对多关系)
    tabpage1
    crystal report (asp调用水晶报表实例)
    上海万千文化传播有限公司(网站项目策划书)
    访问和更新Cookies集合
  • 原文地址:https://www.cnblogs.com/widsom/p/10471536.html
Copyright © 2020-2023  润新知