답안 #1052762

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1052762 2024-08-10T20:27:31 Z whyalwaysmezzz Stove (JOI18_stove) C++14
0 / 100
2 ms 344 KB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define BIT(mask,i) ((mask >> i) & 1ll )
#define endl '\n'  
#define all(x) x.begin(),x.end()
#define ii pair <int,int> 
using namespace std;
template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y) {
            x = y;
            return true;
        } else return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y) {
            x = y;
            return true;
        } else return false;
    }
template<class T>
    T Abs(const T &x) {
        return (x < 0 ? -x : x);
    }
const long long oo = 1e18;
const int N = 1e6+5;
const int MOD = 1e9+7;
int a[N];
int n,k;
int check(int x)
{
	int cnt = 0;
	FOR(i,1,n-1)
	{
		if (a[i+1] - a[i] > x)
			cnt++;
	}	
	return cnt;
}
void PhongCach()
{
	
	cin >> n >> k;
	FOR(i,1,n) cin >> a[i];
	int l = 0;
	int res = oo;
	int r = 1e9;
	while (r - l > 1)
	{
		int m = r + l >> 1;
		if (check(m) < k)
		{
			r= m;
			res = min(res,m);
		}
		else l = m;
	}
	int ans =1;
	priority_queue <int> V;
	FOR(i,1,n-1)
	{
		if (a[i+1] - a[i] == 1) {ans+=1;continue;}
		if (a[i+1] - a[i] > res)
			ans += 1,k--;
		else V.push(-a[i+1]+a[i]);
	}
	while (k > 1)
	{
		k--;
		V.pop();
		ans++;
	}
	while (V.size()) ans-=V.top(),V.pop();
	cout << ans;
    return;
}
#define TASK "stove"
signed main()
{
    ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	freopen(TASK".inp","r",stdin);
	freopen(TASK".out","w",stdout);
    int Sty1e = 1;
  //  cin >> Sty1e;
    while (Sty1e--) PhongCach();
    return 0;		

}

Compilation message

stove.cpp: In function 'void PhongCach()':
stove.cpp:55:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |   int m = r + l >> 1;
      |           ~~^~~
stove.cpp: In function 'int main()':
stove.cpp:88:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |  freopen(TASK".inp","r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
stove.cpp:89:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |  freopen(TASK".out","w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -