UVa10763 Foreign Exchange

网上大佬的swap大法太厉害啦%%%

我比较笨, 只能暴力模拟了.

但是直接模拟上去内存肯定不够的, 于是套个Hash就行啦

手写Hash(2530ms的大常数...)

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 const int MAXN = 5e5 + 20;
 7 const int MOD  = 6e5;
 8 inline int read()
 9 {
10     int x = 0; char ch = getchar();
11     while(!isdigit(ch)) ch = getchar();
12     while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
13     return x;
14 }
15 
16 int N;
17 int a[MAXN], b[MAXN];
18 
19 namespace Hash
20 {
21     struct node
22     {
23         int from, to;
24         node *nxt; bool del;
25         node() : from(0), to(0), nxt(NULL), del(false) {}
26     }*fir[MOD], pool[MAXN + MOD], *pit = pool;
27 
28     inline node *newnode()
29     {
30         return new (pit++) node();
31     }
32 
33     void init()
34     {
35         pit = pool;
36         memset(pool, 0, sizeof(pool));
37         memset(fir, 0, sizeof(fir));
38     }
39 
40     inline void insert(int from, int to)
41     {
42         int pos = from % MOD;
43         node *cur = newnode();
44         cur->nxt = fir[pos];
45         cur->from = from;
46         cur->to = to;
47         fir[pos] = cur;
48     }
49 
50     inline bool find_erase(int from, int to)
51     {
52         int pos = from % MOD;
53         for(node *it = fir[pos]; it; it = it->nxt)
54             if(it->from == from && it->to == to)
55             {
56                 if(it->del == false) {
57                     it->del = true;
58                     return true;
59                 }
60             }
61         return false;
62     }
63 };
64 
65 int main()
66 {
67     while(cin>>N, N)
68     {
69         Hash::init();
70         for(int i = 1; i <= N; i++)
71         {    
72             a[i] = read(), b[i] = read();
73             Hash::insert(a[i], b[i]);
74         }
75         bool flag = true;
76         for(int i = 1; i <= N; i++)
77         {
78             if(!Hash::find_erase(b[i], a[i]))
79             {
80                 flag = false;
81                 break;
82             }
83         }
84         if(!flag) puts("NO");
85         else puts("YES");
86     }
87     return 0;
88 }

multimap维护的Hash(1080ms, stl大法好啊!!)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef pair<int, int> P;
const int MAXN = 5e5 + 20;
const int MOD = 250000;
inline int read()
{
    int x = 0; char ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return x;
}

int N;
int a[MAXN], b[MAXN];

int main()
{
    while(cin>>N, N)
    {
        multimap<int, int> M;
        for(int i = 1; i <= N; i++)
        {    
            a[i] = read(), b[i] = read();
            M.insert(P(a[i], b[i]));
        }

        bool flag = true;
        multimap<int, int>::iterator it;
        for(int i = 1; i <= N; i++)
        {
            bool cur = false;
            for(it = M.find(b[i]); (it != M.end() && it->first == b[i]); ++it)
            {
                if(it->second == a[i]){
                    M.erase(it);
                    cur = true;
                    break;
                }
            }
            if(!cur) {
                flag = false;
                break;
            }
        }
        if(!flag) puts("NO");
        else puts("YES");
    }
    return 0;
}

神牛们的multimap(320ms)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 multimap<int, int> m;
 5 int n, a, b, c;
 6 
 7 int main()
 8 {
 9     while (~scanf("%d", &n) && n)
10     {
11         m.clear();
12         int ask = 0;
13         for (int i = 1; i <= n; i++)
14         {
15             scanf("%d%d", &a, &b);
16             multimap<int, int>::iterator it;
17             bool can = false;
18             for (it = m.find(b); it != m.end() && it->first == b;it++)
19             {
20                 if (it->second==a) 
21                 {
22                     m.erase(it);
23                     can = true;
24                     break;
25                 }
26             }
27             if (!can)
28                 m.insert(pair<int, int>(a, b));
29         }
30         if (m.empty())
31             printf("YES\n");
32         else
33             printf("NO\n");
34     }
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/wsmrxc/p/9242242.html
今日推荐