51nod 1521 一维战舰
2016-10-22 17:06
337 查看
思路:插入一个miss的时候计算下减少的可以放的船的数量,模拟下就好了。注意船不能接触(至少隔开一个位置)。
用的set找左右值,慢了点
不用set,直接标记找左右,快多了。
用的set找左右值,慢了点
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define PI acos(-1.0) #define INF 0x3f3f3f3f typedef long long LL; typedef unsigned long long ULL; int m,n,k,a; int q[200005]; set<int>s; int main() { scanf("%d%d%d",&n,&k,&a); scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%d",q+i); } s.insert(0); s.insert(n+1); int ans=-1; int num=(n+1)/(a+1); for(int i=1;i<=m;i++){ s.insert(q[i]); int l=*--s.find(q[i]); int r=*++s.find(q[i]); int t1=(r-l)/(a+1); int t2=(q[i]-l)/(a+1)+((r-q[i])/(a+1)); //cout<<t1<<endl<<t2<<endl; num=num-(t1-t2); //cout<<num<<endl; if(num<k){ ans=i;break; } } printf("%d\n",ans); return 0; }
不用set,直接标记找左右,快多了。
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<limits.h> #include<iostream> #include<queue> #include<math.h> #include<stack> #include<vector> #include<algorithm> using namespace std; #define maxn 100005 typedef long long ll; struct node { ll x,y,id; }a[maxn]; bool flag[maxn]; int main() { ll n,m,i,j,k,x,y,t,ans=0; scanf("%lld%lld%lld",&n,&x,&y); scanf("%lld",&m); ll num=(n+1)/(y+1);//总共能放的数量 //printf("%lld\n",num); for(i=1;i<=m;i++) { scanf("%lld",&t); flag[t]=1; ll left=t-1,right=t+1; while(left>=1 && flag[left]==0) left--; while(right<=n && flag[right]==0) right++; //printf("%lld %lld\n",left,right); num-=(right-left)/(y+1)-(t-left)/(y+1)-(right-t)/(y+1); if(num<x && ans==0) { ans=i; break; } //printf("%lld\n",num); } if(ans==0)printf("-1\n"); else printf("%lld\n",ans); }
相关文章推荐
- 51NOD 1521 一维战舰
- 51Nod 1521 一维战舰
- 51nod——1521一维战舰 利用二分法
- 51Nod-1521-一维战舰
- 51Nod 1521 一维战舰
- 51nod 1521 一维战舰
- 51nod-【1521 一维战舰】
- 51nod 1521 一维战舰 模拟
- 51nod 1521 一维战舰【set】
- 【CodeForces】567D - One-Dimensional Battle Ships & 【51Nod】1521 - 一维战舰(STL - set & 二分)
- 51nod 1521 一维战舰
- 51nod 1521 一维战舰
- 51 NOD 1521 一维战舰(并查集)
- 1521 一维战舰 水题
- 51 nod 1521 一维战舰 时间复杂度O(n),同 Codeforces 567D. One-Dimensional Battle Ships 有详细注释
- 51 Nod 1521 一维战舰
- 51nod1521 一维战舰
- 51Nod - 1521 STL +二分
- 51Nod 1521 一维战舰
- java获取一维数组中连续数字分成几个连续的数字数组