Codeforces Round #310 (Div. 2)简洁题解

A:原来是大水题,我还想去优化。。

     结果是abs(num('0')-num('1'));

     num表示一个符号的个数;

B:暴力模拟即可,每次判断是否能构造出答案。

C:俄罗斯套娃,套套套,捉鸡的E文。

     抛开乱七八糟的题意;

   思路就是除了1连续的不拆开,其他都拆,所以乱写就好了。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <math.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include<string>
14 using namespace std;
15 #define N 222222
16 typedef long long ll;
17 
18 int a[N];
19 
20 
21 int main()
22 {
23     int n,k;
24     cin>>n>>k;
25     int ans=0;
26 
27     for (int i=1;i<=k;i++)
28     {
29         int m;
30         cin>>m;
31         for (int j=1;j<=m;j++) cin>>a[j];
32         ans+=m-1;
33 
34         int pos=n+1;
35         for (int j=1;j<=m;j++)
36         if (a[j]==1) pos=j;
37 
38             for (int j=pos+1;j<=m;j++)
39             if (a[j]-1==j-pos)
40             ans-=2;
41 
42     }
43 
44     cout<<ans+n-1;
45     return 0;
46 }

D:我们先求出两座相邻的岛需要桥的范围是[x,y];即 node a.x,a.y;

 思路关键词:贪心;

 解析:先按y从小到大排序,相同,x大的在前。

           即:

     int cmp(node a,node b)
     {
      if (a.y==b.y) return a.x>b.x;
      return a.y<b.y;
     }
然后对于每个桥我们都丢到set 中,可能有相同元素,加一维序号;

对于node 我们在set中 找大于node a.x 最前的 然后从set中删除。

为何这么做:
因为排好序后,最前的优先级一定最高,想想。
三种情况:
【 】 ...1
【 】 ...2
【 】...3
满足1的一定满足2,满足2的不一定满足1,同理2满足,也满足3,满足3,不一定满足2
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <math.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include<string>
14 using namespace std;
15 #define N 222222
16 typedef long long ll;
17 #define mp make_pair
18 
19 struct node
20 {
21     ll x,y;
22     int flag;
23 }b[N];
24 int n,m;
25 
26 ll l[N],r[N];
27 
28 set<pair<ll,int> >S;
29 set<pair<ll,int> >::iterator it;
30 
31 struct node2
32 {
33     ll x;
34     int idx;
35 }a[N];
36 
37 int cmp(node a,node b)
38 {
39     if (a.y==b.y) return a.x>b.x;
40     return a.y<b.y;
41 }
42 
43 int ans[N];
44 
45 
46 int main()
47 {
48 
49     scanf("%d%d",&n,&m);
50     for (int i=1;i<=n;i++)
51     scanf("%I64d%I64d",&l[i],&r[i]);
52 
53     for (int i=1;i<=m;i++) {
54             scanf("%I64d",&a[i].x);
55             S.insert(mp(a[i].x,i));
56     }
57 
58     for (int i=1;i<n;i++)
59     b[i].x=l[i+1]-r[i],b[i].y=r[i+1]-l[i],b[i].flag=i;
60 
61     sort(b+1,b+n,cmp);
62 
63     for (int i=1;i<n;i++)
64     {
65 
66         it=S.lower_bound(mp(b[i].x,0));
67         if (it==S.end()||(it->first>b[i].y))
68         {
69             cout<<"No";
70             return 0;
71         }
72 
73         ans[b[i].flag]=it->second;
74 
75         S.erase(it);
76     }
77 
78     cout<<"Yes"<<endl;
79     for (int i=1;i<n;i++) cout<<ans[i]<<" ";
80     return 0;
81 }

E :不会



posted on 2015-06-28 10:54  forgot93  阅读(147)  评论(0编辑  收藏  举报

导航