1004A - Sonya and Hotels - CodeForces Solution


implementation *900

Please click on ads to support us..

Python Code:

(n, d) = map(int, input().split(' '))
x = list(map(int, input().split(' ')))

hotels = []
x.append(0)

for i in range(n):
    if i == 0:
        if x[i] + d <= x[i + 1] - d:
            hotels.append(x[i] + d)
    else:
        if x[i - 1] + d <= x[i] - d:
            hotels.append(x[i] - d)
        if x[i] + d <= x[i + 1] - d:
            hotels.append(x[i] + d)


hotels.append(x[0] - d)
hotels.append(x[-2] + d)

print(len(set(hotels)))

C++ Code:

/*                       Hello Ji                                      */
#include<bits/stdc++.h>
#define ll long long 
const int MOD = 1e9 + 7;
#define pb push_back
#define endl "\n"
#define quick ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
// bool comp(pair<ll,ll> &p1,pair<ll,ll> &p2)
// {
//    return p1.second<p2.second;
// }
void solve()
{
ll n,d;
cin>>n>>d;
ll a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
ll count = 0;
for(int i=0;i<n-1;i++){
    if(a[i+1]-a[i]==2*d){
        count++;
    }
    else if(a[i+1]-a[i]>2*d){
        count = count + 2;
    }
}
cout << count + 2 << endl;
}
int32_t main()
{
  quick;
  int t = 1;
  // cin>>t;
  while(t--)
  {
    solve();
  }
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
/*
*/


Comments

Submit
0 Comments
More Questions

260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring