1. 程式人生 > >Codeforces Round #523 (Div. 2) B. Views Matter 思維

Codeforces Round #523 (Div. 2) B. Views Matter 思維

題解

題目大意 給一排貨物的堆積高度 在保證俯檢視和右檢視不發生變化的情況下最多能移除多少貨物 貨物可以浮空

先將高度排序 儘量每個位置只保留一個貨物 則這個位置可以移除的數量為a[i] - 1 這個貨物的高度為h 每次向右一個格h儘量增加一個高度 到最後將h與a[N]的高度差從答案中扣除

AC程式碼

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int
MAXN = 1e5 + 10; int a[MAXN]; int main() { #ifdef LOCAL freopen("C:/input.txt", "r", stdin); #endif int N, M; cin >> N >> M; for (int i = 1; i <= N; i++) scanf("%d", &a[i]); sort(a + 1, a + N + 1); ll ans = 0; int h = 0; //高度 for (int i = 1; i <= N; i++) { h = min(h + 1
, a[i]); //儘量每一向右一個格子增加一個高度 ans += max(0, a[i] - 1); //每列使用一個方塊其它都為答案 } cout << ans + h - a[N] << endl; //最後一個格子減去剩餘高度差 return 0; }