• 1521 一维战舰


    思路:依次把x【】数组的元素加入set,再二分出当前入set元素在set中的上界r和下界l,则这段区间能放的最多的战舰数量就由原来的(r-l)/(a+1)减少为(x[i]-l)/(a+1)+(r-x[i])/(a+1),

    每次判断加入新元素后能放最多战舰数sum与k的大小即可。

     1 #include <iostream>
     2 #include <queue>
     3 #include <stack>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <map>
     7 #include <set>
     8 #include <bitset>
     9 #include <algorithm>
    10 #include <cmath>
    11 #include <cstring>
    12 #include <cstdlib>
    13 #include <string>
    14 #include <sstream>
    15 #include <time.h>
    16 #define x first
    17 #define y second
    18 #define pb push_back
    19 #define mp make_pair
    20 #define lson l,m,rt*2
    21 #define rson m+1,r,rt*2+1
    22 #define mt(A,B) memset(A,B,sizeof(A))
    23 #define mod 1000000007
    24 using namespace std;
    25 typedef long long LL;
    26 const int N=2e5+10;
    27 const int inf = 0x3f3f3f3f;
    28 const LL INF=0x3f3f3f3f3f3f3f3fLL;
    29 int x[N];
    30 set<int> Q;
    31 int solve(int l,int k)
    32 {
    33     if(l<k)return 0;
    34     else  return 1+(l-k)/(k+1);
    35 }
    36 int main()
    37 {
    38 #ifdef Local
    39     freopen("data.txt","r",stdin);
    40 #endif
    41     int n,k,a,m,p,sum=0,flag=0,ans;
    42     cin>>n>>k>>a;
    43     cin>>m;
    44     for(int i=1;i<=m;i++)scanf("%d",&x[i]);
    45     Q.insert(0);Q.insert(n+1);
    46     sum=solve(n,a);
    47     for(int i=1;i<=m;i++)
    48     {
    49         set<int>::iterator it1,it2,it3,it;
    50         Q.insert(x[i]);
    51         it1=Q.find(x[i]);
    52         it1--;
    53         it2=Q.find(x[i]);
    54         it2++;
    55         sum=sum-solve(*(it2)-*(it1)-1,a)+solve(x[i]-*(it1)-1,a)+solve(*(it2)-x[i]-1,a);
    56         if(sum<k){
    57             flag=i;
    58             break;
    59         }
    60 
    61     }
    62     if(flag)cout<<flag<<endl;
    63     else cout<<-1<<endl;
    64 #ifdef Local
    65     cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    66 #endif
    67 }
    View Code
  • 相关阅读:
    Oracle 添加主键和索引
    Oracle中查询主键、外键、sequence、表基本信息等
    Spring工作原理
    Ehcache 缓存使用
    socket编程-java
    oracle触发器详解
    单例模式的几种写法
    [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡
    LeetCode Top Interview Questions
    [LeetCode] 131. Palindrome Partitioning 回文分割
  • 原文地址:https://www.cnblogs.com/27sx/p/6264949.html
Copyright © 2020-2023  润新知