CodeForces 982 D Shark

Shark

题意:一个研究员观察了一条鲨鱼n天的运动,然后这条鲨鱼他只会往前走,不会回到去过的地方,现在有一个k,,如果鲨鱼当天游过的距离 >= k, 代表的鲨鱼在这天会前往下一个地点,现在求鲨鱼在每个停留的地点所待的时间是一样的,然后在上面那个情况下使得鲨鱼所待得地点数目最多,然后再地点数目的情况下保证K最小。

题解:从小到大处理元素,每次将k = 当前元素+1,因为只有k比一个元素大了才会有新的停留地点,然后对于所有出现过的元素鲨鱼都会在这个点停留,然后我们需要对每一段连续的停留片段计算出他的天数,然后check一下是否合法,我们可以用并查集将连续的片段合并记录长度来优化check,用set记录有几个片段,然后当片段数目 > 答案中的岛就check一下,如果成立,更新答案。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const LL mod = 1e9+7;
16 const int N = 1e5+10;
17 int n;
18 int pre[N];
19 int cnt[N];
20 int vis[N];
21 int ss[N];
22 struct Node{
23     int t;
24     int id;
25 }A[N];
26 bool cmp(Node x,Node y){
27     return x.t < y.t;
28 }
29 set<int>s ;
30 bool check(){
31     int ttt = *s.begin();
32     return ss[cnt[ttt]] == s.size();
33 }
34 int Find(int x){
35     if(x == pre[x]) return x;
36     return pre[x] = Find(pre[x]);
37 }
38 int main(){
39     scanf("%d", &n);
40     int ans = INF;
41     int len = 1;
42     for(int i = 1; i <= n; i++){
43         cnt[i] = 1;
44         pre[i] = i;
45     }
46     for(int i = 1; i <= n; i++){
47         scanf("%d", &A[i].t);
48         ans = min(ans,A[i].t+1);
49         A[i].id = i;
50     }
51     sort(A+1,A+1+n,cmp);
52     for(int i = 1; i <= n; i++){
53         int now = A[i].id;
54         vis[now] = 1;
55         int f = 0;
56         if(now+1 <= n && vis[now+1]){
57             int t = Find(now), y = Find(now+1);
58             pre[t] = y;
59             ss[cnt[y]]--;
60             cnt[y] += cnt[t];
61             ss[cnt[y]]++;
62             f = 1;
63         }
64         if(now-1 >= 1 && vis[now-1]){
65             int t = Find(now), y = Find(now-1);
66             if(f == 1) s.erase(t),ss[cnt[t]]--;
67             ss[cnt[y]]--;
68             cnt[y] += cnt[t];
69             ss[cnt[y]]++;
70             pre[t] = y;
71             f = 2 ;
72         }
73         if(f == 0) s.insert(now), ss[cnt[now]]++;
74         if(s.size() > len && check()){
75             len = s.size();
76             ans = A[i].t+1;
77         }
78     }
79     printf("%d", ans);
80     return 0;
81 }
View Code

猜你喜欢

转载自www.cnblogs.com/MingSD/p/9055171.html
982