Codeforces Round #241 (Div. 2) D

Source: Internet
Author: User

Link: D. Population Size


Some numbers must be divided into blocks so that each segment is an equal-difference series, but some numbers are-1, which indicates any number (but must be greater than 0). A question must be divided into at least a few digits.

Idea: greedy. Each time two adjacent numbers are found, and the number of-1 before the first number is recorded, the tolerances can be determined, and then the tolerance can be used to determine whether the previous-1 can be filled in, if it cannot be ans, it will be 1 more, and then it will start from the second definite digit position. If it can, it will use the tolerances to locate the last position that can be placed, and then start from that position next time.

There are many details, and the code is frustrated:

#include 
 
  #include 
  
   const int N = 200005;__int64 n, i, j;__int64 a[N];int main() {__int64 ans = 0;scanf("%I64d", &n);for (i = 0; i < n; i++)scanf("%I64d", &a[i]);i = 0;__int64 s1 = 0;while (i < n) {s1 = 0;while (a[i] == -1 && i < n) {s1++;i++;}__int64 s = i;if (i < n)i++;while (a[i] == -1 && i < n) {i++;}if (i == n) {ans++;break;}__int64 e = i, d;if ((a[e] - a[s]) % (e - s) == 0) {d = (a[e] - a[s]) / (e - s);}else {ans++;continue;}if (d > 0 && s1 > (a[s] - 1) / d) {ans++;i = e;continue;}__int64 sum = a[s];for (j = s + 1; j < n; j++) {sum += d;if (sum < 1) {i = j;ans++;break;}if (a[j] != -1 && sum != a[j]) {i = j;ans++;break;}}if (j == n) {i = n;ans++;}}printf("%I64d\n", ans);return 0;}
  
 


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.