Codeforces Round #Pi (Div. 2)--d one-dimensional Battle Ships

Source: Internet
Author: User

The meaning of the title is:

There is now a square with a length of N and a width of 1, which can be enlarged by a small 1*a boat, and then entered as N,k,a, respectively, the size of the flat, the number of ships, the length of the boat.

A man named Alice has put the ship's position on the map.

Then Bob could have a total of M attacks, and then his point for each attack was XI, but Alice would not tell him that there was no shot (that is, every time he thought he was Miss) and asked you if Bob could speculate that Alice was lying in the first few attacks, and if not, output-1.

Each of the two adjacent ships here cannot be bordered by one another.

Ideas:

Use set to maintain the interval of each section.

First, we can put a maximum of sum= (n+1)/(a+1) boat.

How did this formula come about?  We can think this: because the ship can not border, so when there are K-boat, N at least k* (a+1)-1; Then you can get the answer back.

Then every time we attack a point T, it's equivalent to separating the segments from each other, and we're going to find the nearest left and right endpoint of the t1,t2, because they also have a t in between, so it's equivalent to T1,t2 being split.

Then the current number of ships can be obtained by another formula: sum=sum-(T2-T1)/(a+1) + (T-T1)/(a+1) + (T2-T)/(a+1); Here's the equivalent of reducing the part of the original Then add the maximum number of boats that can be accommodated by the interval after separation.

When sum<k, it means that Alice is lying at the moment.

Here you find the left and right endpoints with a binary lookup.

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < vector> #include <queue> #include <set> #include <map> #include <stack>using namespace std;# Define me rng_58#define MAXN 200055set<int> st;set<int>::iterator it;int main () {int n,k,a,m,i;scanf ("%d%d% D ", &n,&k,&a); scanf ("%d ", &m); St.insert (0); St.insert (n+1); int sum= (n+1)/(a+1); int ans=-1;for (i=1;i<=m;i++) {int t;scanf ("%d", &t); It=st.upper_bound (t); int t1=*it;it--;//Why this can be reduced here, learn C + + after the solution; int T2=*it;st.insert (t);//Here just put the T in it!!! Be careful!!! sum=sum-(T1-T2)/(a+1) + (T-T2)/(a+1) + (T1-T)/(a+1); if (sum<k) {ans=i;break;}} printf ("%d\n", ans);} /*5000 1660 2201 100 18 102 300 81 19 25 44 88 1337 4999 1054 1203 91 16 164 914 1419 1487*/

Admire the people who can think out. Wish I can become the same like U one day!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces Round #Pi (Div. 2)--d one-dimensional Battle Ships

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.