0x05 排序

离散化

void discrete() {
    sort(a+1, a+n+1);
    /*for (int i=1; i<=n; ++i)
        if (i==1|| a[i]!=a[i-1])
            b[++m]=a[i]; */
    //STL实现
    m=unique(a+1, a+n+1)-a-1; 
} 
void query(int x) {
    return lower_bound(b+1, b+m+1, x)-b;
}

 

【例题】CF670C Cinema

排序并离散化,然后直接统计。

 

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 using namespace std;
 7 const int maxn=200010;
 8 int n, m, cnt=0, a[maxn], b[maxn], c[maxn], lang[600010], speak[600010];
 9 int happy[maxn], less_happy[maxn];
10 
11 void discrete() {
12     sort(lang+1, lang+cnt+1);
13     cnt=unique(lang+1, lang+cnt+1)-lang-1;
14 }
15 int query(int x) {
16     return lower_bound(lang+1, lang+cnt+1, x)-lang;
17 } 
18 
19 void solve() {
20     //统计每种语言会的珂学家的数量
21     for (int i=1; i<=n; ++i) {
22         int s=query(a[i]);
23         speak[s]++;
24     } 
25     
26     int maxh=0, maxlh=0, ans=1;
27     //依次考虑每一部电影 
28     for (int i=1; i<=m; ++i) {
29         int sound=query(b[i]), sub=query(c[i]);
30         happy[i]=speak[sound];
31         less_happy[i]=speak[sub]; 
32         if (maxhi; }
33         else if (maxh==happy[i]&&maxlh<less_happy[i]) {
34             maxh=happy[i]; maxlh=less_happy[i]; ans=i;
35         }
36     }
37     
38     printf("%d\n", ans);
39 }
40 
41 int main() {
42     scanf("%d", &n);
43     for (int i=1; i<=n; ++i) {
44         scanf("%d", &a[i]);
45         lang[++cnt]=a[i];
46     }
47     scanf("%d", &m);
48     for (int i=1; i<=m; ++i) {
49         scanf("%d", &b[i]);
50         lang[++cnt]=b[i];
51     }
52     for (int i=1; i<=m; ++i) {
53         scanf("%d", &c[i]);
54         lang[++cnt]=c[i];
55     }
56     discrete();
57     /*printf("%d\n", cnt);
58     for (int i=1; i<=cnt; ++i)
59         printf("%d ", lang[i]);*/
60     
61     solve();
62     return 0;
63 }

 

中位数

【例题】CH0501 货仓选址

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 using namespace std;
 7 const int maxn=100010;
 8 int n, a[maxn];
 9 
10 int main() {
11     scanf("%d", &n);
12     for (int i=1; i<=n; ++i)
13         scanf("%d", &a[i]);
14     sort(a+1, a+n+1);
15     int ans=0;
16     if (n&1) {
17         int x=a[(n+1)/2];
18         for (int i=1; i<=n; ++i)
19             ans+=abs(a[i]-x);
20     }
21     else {    
22         int x=a[n/2];
23         for (int i=1; i<=n; ++i)
24             ans+=abs(a[i]-x);
25     }
26     printf("%d\n", ans);
27     return 0;
28 }

【例题】BZOJ3032/CH0502 七夕祭

不想写了,直接去看书。。。贴代码。

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 #define int long long
 7 using namespace std;
 8 const int maxn=100000+10;
 9 int n, m, t, ans=0, x[maxn], y[maxn], p[maxn], q[maxn];
10 
11 signed main() {
12     scanf("%lld%lld%lld", &n, &m, &t);
13     for (int i=1; i<=t; ++i) {
14         int a, b;
15         scanf("%lld%lld", &a, &b);
16         x[a]++;
17         y[b]++;
18     }
19     if ((t%m!=0)&&(t%n!=0)) {
20         printf("impossible\n");
21         return 0;
22     }
23     for (int i=1; i<=n; ++i)
24         x[i]-=t/n;
25     for (int i=1; i<=m; ++i)
26         y[i]-=t/m;
27     if (t%n==0) {
28         for (int i=1; i<=n; ++i)
29             p[i]=p[i-1]+x[i];
30         sort(p+1, p+1+n);
31         for (int i=1; i<=n; ++i)
32             ans+=abs(p[i]-p[(n+1)/2]);
33     }
34     if (t%m==0) {
35         for (int i=1; i<=m; ++i)
36             q[i]=q[i-1]+y[i];
37         sort(q+1, q+1+m);
38         for (int i=1; i<=m; ++i)
39             ans+=abs(q[i]-q[(m+1)/2]);
40     }
41     if ((t%n==0)&&(t%m==0)) printf("both ");
42     else if (t%n==0) printf("row ");
43     else printf("column ");
44     printf("%lld\n", ans);
45     return 0;
46 }

【例题】POJ3784 Running Median

维护一个对顶堆即可。

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 #include 
 8 #include 
 9 using namespace std;
10 const int maxn=100000+10;
11 int ans[maxn];
12 priority_queue<int> p;
13 priority_queue<int , vector<int>, greater<int> > q;
14 vector<int> g;
15 
16 int main() {
17     //freopen("a.txt", "r", stdin);
18     int T;
19     cin>>T;
20     while (T--) {
21         while (!p.empty()) p.pop();
22         while (!q.empty()) q.pop();
23         g.clear();
24         
25         int c, n, x, cnt=0;
26         scanf("%d%d", &c, &n);
27         printf("%d %d\n", c, (n+1)/2);
28         
29         for (int i=1; i<=n; ++i) {
30             int x;
31             scanf("%d", &x);
32             if (i==1) q.push(x);
33             else {
34                 int mid=q.top();
35                 if (x<mid) p.push(x);
36                 else q.push(x);
37             }
38             while (q.size()<p.size()) {
39                 q.push(p.top());
40                 p.pop();
41             }
42             while (q.size()>p.size()+1) {
43                 p.push(q.top());
44                 q.pop();
45             }
46             if (i&1) {
47                 printf("%d", q.top());
48                 ++cnt;
49                 if (cnt%10)
50                     putchar(' ');
51             }
52             if (cnt==10) { printf("\n"); cnt=0; }
53         }
54         if (T!=0)printf("\n");
55     }
56     return 0;
57 }

 

你可能感兴趣的:(0x05 排序)