101 Hack October'14

拖了近一个月的总结。(可能源于最近不太想做事:()

A题

给出n个长度都为n的字符串,你只可以对每个字符串分别排序,问当每个字符串按升序排序之后,每一列是否也是升序的。

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8
 9 int main() {
10     ios::sync_with_stdio(false);
11     int t;
12     cin >> t;
13     while (t--) {
14         int n;
15         cin >> n;
16         string ch1, ch2;
17         cin >> ch1;
18         sort(ch1.begin(), ch1.end());
19         bool flag = true;
20         for (int i = 1; i < n; i++) {
21             cin >> ch2;
22             sort(ch2.begin(), ch2.end());
23             for (int j = 0; j < n; j++) if (ch1[j] > ch2[j]) flag = false;
24             //if (ch1 > ch2) flag = false;
25             swap(ch1, ch2);
26         }
27         if (flag) cout << "YES\n";
28         else cout << "NO\n";
29     }
30     
31     return 0;
32 }

B题

进制转换

 1 #include <map>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 int work(int m, int d) {
10     int res = 0, t = 1;
11     while (d) {
12         if (d % 10 >= m) return -1;
13         res += (d % 10) * t;
14         d /= 10;
15         t *= m;
16     }
17     return res;
18 }
19 
20 int main() {
21     ios_base::sync_with_stdio(false);
22     int n;
23     cin >> n;
24     map<int, int> mmap;
25     for (int i = 0; i < n; i++) {
26         int m, d;
27         cin >> m >> d;
28         int x = work(m, d);
29         if (x != -1) mmap[x]++;
30     }
31     long long res = 0;
32     for (map<int, int>::iterator it = mmap.begin(); it != mmap.end(); it++) res += (long long)(it->second) * (it->second - 1) / 2;
33     cout << res << endl;
34     return 0;
35 }

C题

从N个蜡烛中选出一个子序列使得每种颜色都至少出现一次且序列中蜡烛的高度递增,问存在多少种这样的集合。

因为K较小,那么建立(1<<K)棵梳妆数组,分别维护以hi为最后一根蜡烛的方法数。

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int MAX_N = 50010;
 9 const int MOD = 1e9 + 7;
10 typedef long long LL;
11 int H[MAX_N], C[MAX_N], dp[1<<7];
12 int c[1<<7][MAX_N];
13 
14 int lowbit(int x) {
15     return x & -x;
16 }
17 
18 int sum(int id, int x) {
19     int res = 0;
20     while (x > 0) {
21         res += c[id][x]; 
22         if (res >= MOD) res -= MOD;
23         x -= lowbit(x);
24     }
25     return res;
26 }
27 
28 void add(int id, int x, int v) {
29     while (x <= MAX_N - 10) {
30         c[id][x] += v;
31         if (c[id][x] >= MOD) c[id][x] -= MOD;
32         x += lowbit(x);
33     }
34 }
35 
36 int main() {
37     ios::sync_with_stdio(false);
38     int N, K;
39     cin >> N >> K;
40     for (int i = 1; i <= N; i++) {
41         cin >> H[i] >> C[i];
42         C[i]--;
43     }
44     
45     for (int i = 1; i <= N; i++) {
46         for (int j = 0; j < (1<<K); j++) {
47             int x = j | (1<<C[i]);
48             dp[j] = sum(j, H[i] - 1);
49             //add(x, H[i], s);
50         } 
51         for (int j = 0; j < (1<<K); j++) add(j | (1<<C[i]), H[i], dp[j]);
52         add(1<<C[i], H[i], 1);
53     }
54     cout << sum((1<<K) - 1, MAX_N - 10) << endl;
55     
56     return 0;
57 }

D题

线段树优化DP

  1 /*************************************************************************
  2     > File Name: Burger_Happiness.cpp
  3     > Author: Stomach_ache
  4     > Mail: sudaweitong@gmail.com
  5     > Created Time: 2014?11?14? ??? 18?12?10?
  6     > Propose: /Hackerrank/Contest/101 Hack October 14
  7  ************************************************************************/
  8 
  9 #include <cmath>
 10 #include <string>
 11 #include <cstdio>
 12 #include <vector>
 13 #include <fstream>
 14 #include <cstring>
 15 #include <iostream>
 16 #include <algorithm>
 17 using namespace std;
 18 /*Let's fight!!!*/
 19 
 20 typedef long long LL;
 21 const LL INF = 1LL << 60;
 22 const int MOD = 1e9 + 7;
 23 const int MAX_N = 1e5 + 5;
 24 #define lson(x) (x<<1)
 25 #define rson(x) ((x<<1) | 1)
 26 struct Node {
 27       int l, r;
 28     LL lazy;
 29     LL mmax;
 30     void set(int ll, int rr) {
 31           l = ll; 
 32         r = rr;
 33         mmax = 0;
 34         lazy = 0;
 35     }
 36 };
 37 
 38 struct SegmentTree {
 39     Node tr[MAX_N*4];
 40 
 41     void build(int rt, int l, int r) {
 42           tr[rt].set(l, r);
 43         if (l != r) {
 44               int mid = (l + r) >> 1;
 45             build(lson(rt), l, mid);
 46             build(rson(rt), mid + 1, r);
 47         }
 48     }
 49 
 50     void pushdown(int rt) {
 51         tr[rt].mmax += tr[rt].lazy;
 52           if (tr[rt].l != tr[rt].r) {
 53             tr[lson(rt)].lazy += tr[rt].lazy;
 54             tr[rson(rt)].lazy += tr[rt].lazy;
 55         }
 56         tr[rt].lazy = 0;
 57     }
 58 
 59     void pushup(int rt) {
 60           tr[rt].mmax = max(tr[lson(rt)].mmax, tr[rson(rt)].mmax);
 61     }
 62 
 63     // add v to [l, r]
 64     void update(int rt, int l, int r, int v) {
 65           pushdown(rt);
 66         if (r < tr[rt].l || l > tr[rt].r) return ;
 67           if (tr[rt].l >= l && tr[rt].r <= r) {
 68               tr[rt].lazy = v;
 69             pushdown(rt);
 70         } else {
 71             update(lson(rt), l, r, v);
 72             update(rson(rt), l, r, v);
 73             pushup(rt);
 74         }
 75     }
 76 
 77     // query maximum value in [l, r]
 78     LL query(int rt, int l, int r) {
 79           if (r < tr[rt].l || l > tr[rt].r) return -INF;
 80           pushdown(rt);
 81           if (tr[rt].l >= l && tr[rt].r <= r) {
 82               return tr[rt].mmax;
 83         }
 84         return max(query(lson(rt), l, r), query(rson(rt), l, r));
 85     }
 86 };
 87 
 88 SegmentTree T1; // stores maximum f(x) + s[x - 1]
 89 SegmentTree T2; // stores maximum f(x) - s[x]
 90 
 91 int A[MAX_N], B[MAX_N], X[MAX_N];
 92 LL F[MAX_N];
 93 int main(void) {
 94       ios::sync_with_stdio(false);
 95       int N;
 96     cin >> N;
 97     vector<int> arr;
 98     for (int i = 0; i < N; i++) {
 99         cin >> X[i] >> A[i] >> B[i];
100         arr.push_back(X[i]);
101     }
102     sort (arr.begin(), arr.end());
103     arr.erase(unique(arr.begin(), arr.end()), arr.end());
104       T1.build(1, 1, N); T2.build(1, 1, N);
105     for (int i = 0; i < N; i++) {
106           X[i] = lower_bound(arr.begin(), arr.end(), X[i]) - arr.begin() + 1;
107     }
108 
109     LL res = 0;
110     for (int i = 0; i < N; i++) {
111           LL s = -T2.query(1, X[i], X[i]); // s[x], since f[x] = 0
112         LL s1 = T1.query(1, X[i], X[i]); // s[x - 1]
113         // case p < x
114         LL res1 = -s + A[i] + T1.query(1, 1, X[i]-1);
115         // case p > x
116         LL res2 = s1 + A[i] + T2.query(1, X[i]+1, N);
117         // case beginning from x
118         LL res3 = A[i];
119         F[X[i]] = max(max(res1, res2), res3);
120 
121         T1.update(1, X[i], X[i], F[X[i]]);
122         T1.update(1, X[i]+1, N, B[i]);
123 
124         T2.update(1, X[i], X[i], F[X[i]]);
125         T2.update(1, X[i], N, -B[i]);
126 
127         res = max(res, F[X[i]]);
128     }
129     cout << res << endl;
130 
131     return 0;
132 }

E题

目前是没有能力做这个题了。

posted on 2014-11-16 20:41  Stomach_ache  阅读(197)  评论(0编辑  收藏  举报

导航