Bestcoder #21

2014-12-07 01:06:20

A题,水。。。吐槽下题解的逗比做法- =

B题:转为离线后排序,用滚动变量来做。

 1 /*************************************************************************
 2     > File Name: 1002.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sat 06 Dec 2014 08:12:31 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 const ll mod = 1000000007;
27 
28 ll ta[100010],t,f;
29 int cnt,n;
30 struct node{
31     int val,pos;
32 }b[100010];
33 
34 bool cmp1(node a,node b){
35     return a.val < b.val;
36 }
37 
38 bool cmp2(node a,node b){
39     return a.pos < b.pos;
40 }
41 
42 int main(){
43     t = f = 1;
44     while(scanf("%d",&n) != EOF){
45         b[++cnt].val = n;
46         b[cnt].pos = cnt;
47     }
48     sort(b + 1,b + cnt + 1,cmp1);
49     for(int i = 1; i <= cnt; ++i){
50         for(int j = b[i - 1].val + 1; j <= b[i].val; ++j){
51             f = (f * j) % mod;
52             t = (t * f) % mod;
53         }
54         ta[b[i].pos] = t;
55     }
56     sort(b + 1,b + cnt + 1,cmp2);
57     for(int i = 1; i <= cnt; ++i){
58         printf("%I64d\n",ta[i]);
59     }
60     return 0;
61 }

C题,线段树啥的?

D题:用线段树 / BIT 优化LIS到nlogn,dp[i]表示以i为终点的最长LIS长度,同时要存下这个最长LIS的最右起点(思考)

  调了半天....发现最后有个乘法爆int T T!

 1 /*************************************************************************
 2     > File Name: 1004.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sat 06 Dec 2014 08:17:34 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 const int maxn = 100010;
27 
28 int n;
29 int v[maxn];
30 int cp[maxn];
31 
32 typedef pair<int,int> pii;
33 
34 pii dp[maxn];
35 
36 struct BIT{
37     pii c[maxn];
38     //void Init(){
39     //    fill_n(c + 1,n,pii(0,0));
40     //}
41     int Lowbit(int x){
42         return x & (-x);
43     }
44     void Update(int x,pii t){
45         while(x <= maxn){
46             c[x] = max(t,c[x]);
47             x += Lowbit(x);
48         }
49     }
50     pii Getmax(int x){
51         pii res(0,0);
52         while(x > 0){
53             res = max(res,c[x]);
54             x -= Lowbit(x);
55         }
56         return res;
57     }
58 };
59 
60 int main(){
61     while(scanf("%d",&n) != EOF){
62         BIT B;
63         //B.Init();
64         for(int i = 1; i <= n; ++i){
65             scanf("%d",&v[i]);
66             cp[i] = v[i];
67         }
68         sort(cp + 1,cp + n + 1);
69         int sz = unique(cp + 1,cp + n + 1) - cp - 1;
70         int tmax = -1;
71         for(int i = 1; i <= n; ++i){
72             int p = lower_bound(cp + 1,cp + sz + 1,v[i]) - cp;
73             pii tmp = B.Getmax(p - 1);
74             if(tmp.first == 0) tmp.second = i;
75             tmp.first++;
76             B.Update(p,tmp);
77             dp[i] = tmp;
78             tmax = max(tmax,tmp.first);
79         }
80         ll ans = 0;
81         int pos = 0;
82         for(int i = 1; i <= n; ++i){
83             if(dp[i].first == tmax){
84                 ans += (ll)(dp[i].second - pos) * (ll)(n - i + 1);
85                 pos = dp[i].second;
86             }
87         }
88         printf("%I64d\n",ans);
89     }
90     return 0;
91 }

 

posted @ 2014-12-07 01:09  Naturain  阅读(121)  评论(0编辑  收藏  举报