CF #283 div2

2014-12-18 13:40:50

思路:前三题敲慢了...导致D时间不足。

A:暴力枚举。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <stack>
 9 #include <queue>
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 #define lp (p << 1)
14 #define rp (p << 1|1)
15 #define getmid(l,r) (l + (r - l) / 2)
16 #define MP(a,b) make_pair(a,b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int INF = 1 << 30;
20 
21 int n;
22 int a[110];
23 int ans = INF;
24 
25 int main(){
26     scanf("%d",&n);
27     for(int i = 1; i <= n; ++i) scanf("%d",a + i);
28     sort(a + 1,a + n + 1);
29     for(int i = 2; i < n; ++i){
30         int d = 0;
31         for(int j = 2; j <= n; ++j){
32             if(j == i){
33                 d = max(d,a[j + 1] - a[j - 1]);
34                 ++j;
35             }
36             else d = max(d,a[j] - a[j - 1]);
37         }
38         ans = min(ans,d);
39     }
40     printf("%d\n",ans);
41     return 0;
42 }
View Code

B:暴力枚举哪一位置零。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <stack>
 9 #include <queue>
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 #define lp (p << 1)
14 #define rp (p << 1|1)
15 #define getmid(l,r) (l + (r - l) / 2)
16 #define MP(a,b) make_pair(a,b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int INF = 1 << 30;
20 
21 int n;
22 char v[1010];
23 char ans[1010];
24 char tmp[1010];
25 char tmp2[1010];
26 
27 int main(){
28 
29     scanf("%d",&n);
30     ans[1] = 'a';
31     scanf("%s",v + 1);
32     for(int i = 1; i <= n; ++i){
33         int val = 1 + '9' - v[i];
34         for(int j = 1; j <= n; ++j) tmp[j] = (v[j] - '0' + val) % 10 + '0';
35         int pos = i;
36         for(int j = 1; j <= n; ++j){
37             tmp2[j] = tmp[pos];
38             pos++;
39             if(pos > n) pos = 1;
40         }
41         if(ans[1] == 'a' || strcmp(tmp2 + 1,ans + 1) < 0)
42             strcpy(ans + 1,tmp2 + 1);
43     }
44     ans[n + 1] = '\0';
45     printf("%s\n",ans + 1);
46     return 0;
47 }
View Code

C:暴力枚举、贪心。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <stack>
 9 #include <queue>
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 #define lp (p << 1)
14 #define rp (p << 1|1)
15 #define getmid(l,r) (l + (r - l) / 2)
16 #define MP(a,b) make_pair(a,b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int INF = 1 << 30;
20 
21 int n,m,ans,st;
22 int vis[110];
23 char g[110][110];
24 
25 int main(){
26     st = 1;
27     ans = 0;
28     scanf("%d%d",&n,&m);
29     for(int i = 0; i < n; ++i) scanf("%s",g[i]);
30     int ans = INF;
31     for(int j = 0; j < m; ++j){
32         int flag = 1;
33         memset(vis,0,sizeof(vis));
34         for(int i = 1; i < n; ++i){
35             if(g[i][j] < g[i - 1][j]){
36                 flag = 0;
37                 break;
38             }
39             else if(g[i][j] > g[i - 1][j]) vis[i] = 1;
40         }
41         int res = j;
42         if(flag == 1){ //set j as start
43             for(int k = j + 1; k < m; ++k){
44                 int tag = 1;
45                 for(int i = 1; i < n; ++i){
46                     if(g[i][k] < g[i - 1][k] && !vis[i]){
47                         tag = 0;
48                         break;
49                     }
50                 }
51                 if(tag){
52                     for(int i = 1; i < n; ++i){
53                         if(g[i][k] > g[i - 1][k]) vis[i] = 1;
54                     }
55                 }
56                 else res += 1;
57             }
58             ans = min(ans,res);
59         }
60     }
61     if(ans >= INF) printf("%d\n",m);
62     else printf("%d\n",ans);
63     return 0;
64 }
View Code

D:暴力枚举t,然后二分即可。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int tmp,n,cnt,sum1[100010],sum2[100010];
 7 pair<int,int> ans[100010];
 8 
 9 int Judge(int t){
10     int s1 = 0,s2 = 0,c1 = 0,c2 = 0,last;
11     while(1){
12         int p1 = lower_bound(sum1 + 1,sum1 + n + 1,c1 + t) - sum1;
13         int p2 = lower_bound(sum2 + 1,sum2 + n + 1,c2 + t) - sum2;
14         if(p1 > n && p2 > n) break;
15         if(p2 < p1){
16             swap(p1,p2);
17             s2++,last = 2;
18         }
19         else s1++,last = 1;
20         c1 = sum1[p1];
21         c2 = sum2[p1];
22     }
23     if(c1 + c2 < n || s1 == s2 ||(s1 < s2 && last == 1) ||(s1 > s2 && last == 2)) return 0;
24     return max(s1,s2);
25 }
26 
27 int main(){
28     int v;
29     scanf("%d",&n);
30     for(int i = 1; i <= n; ++i){
31         scanf("%d",&v);
32         sum1[i] = sum1[i - 1];
33         sum2[i] = sum2[i - 1];
34         if(v == 1) sum1[i]++;
35         else sum2[i]++;
36     }
37     for(int t = 1; t <= n; ++t)
38         if(tmp = Judge(t)) ans[++cnt] = make_pair(tmp,t);
39     sort(ans + 1,ans + cnt + 1);
40     printf("%d\n",cnt);
41     for(int i = 1; i <= cnt; ++i) printf("%d %d\n",ans[i].first,ans[i].second);
42     return 0;
43 }

E:贪心,首先按照每个人的highest notes排序,小的在先,(对musical parts也排序),遍历每一个人,然后处理highest notes比他小的所有时间。(当然处理过的parts就不再处理了,就这样扫一遍),用set / sbt 实现(貌似sbt会卡),恩恩。。。考了set成员函数的应用,注意erase函数原理。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <set>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 #define MP(a,b) make_pair(a,b)
 8 typedef pair<int,int> pii;
 9 const int maxn = 100010;
10 
11 struct node{
12     int l,r,id,k;
13     inline void in(){ scanf("%d%d",&l,&r);}
14     friend bool operator < (const node & a,const node &b){ return (a.r == b.r) ? a.l < b.l : a.r < b.r;}
15 }a[maxn],b[maxn];
16 
17 int n,m;
18 int ans[maxn];
19 set<pii> s;
20 
21 int main(){
22     scanf("%d",&n);
23     for(int i = 1; i <= n; ++i) a[i].in(),a[i].id = i;
24     scanf("%d",&m);
25     for(int i = 1; i <= m; ++i) b[i].in(),b[i].id = i,scanf("%d",&b[i].k);
26     sort(a + 1,a + n + 1),sort(b + 1,b + m + 1);
27     int now = 1;
28     for(int i = 1; i <= m; ++i){
29         while(now <= n && a[now].r <= b[i].r) s.insert(MP(a[now].l,a[now].id)),now++;
30         int cnt = 0;
31         set<pii>::iterator it = s.lower_bound(MP(b[i].l,0));
32         while(cnt < b[i].k && it != s.end()){
33             ans[it->second] = b[i].id;
34             s.erase(it);
35             it++,cnt++;
36         }
37     }
38     if(now >n && s.empty()){
39         printf("YES\n");
40         for(int i = 1; i <= n; ++i) printf("%s%d",i == 1 ? "" : " ",ans[i]);
41         puts("");
42     }
43     else printf("NO\n");
44     return 0;
45 }

 

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