NC50542. 仓库建设
描述
L公司在山上有一些工厂。由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用。突然有一天,L公司的总裁L先生接到气象部门的电话,被告知三天之后将有一场暴雨,于是L先生决定紧急在某些工厂建立一些仓库以免产品被淋坏。
输入描述
第一行包含一个整数$N$,表示工厂的个数。
接下来$N$行,每行包含三个整数$X_i,$$P_i,$$C_i$,意义如题中所述。
输出描述
仅包含一个整数,为可以找到最优方案的费用。
示例1
输入:
3 0 5 10 5 3 100 9 6 10
输出:
32
说明:
在工厂1和工厂3建立仓库,建立费用为10+10=20,运输费用为,总费用32。如果仅在工厂3建立仓库,建立费用为10,运输费用为=57,总费用67,不如前者优。Java(javac 1.8) 解法, 执行用时: 744ms, 内存消耗: 99604K, 提交时间: 2021-03-06 11:14:07
// ps[i] = p[i]*x + ps[i-1]; // s[i] = s[i-1] + p[i]; // dp[i] = dp[j] + c[i] + (s[i] - s[j])*x[i] + (ps[i] - ps[j]) import java.io.*; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.locks.LockSupport; public class Main { static class Task { public static String roundS(double result, int scale) { String fmt = String.format("%%.%df", scale); return String.format(fmt, result); // DecimalFormat df = new DecimalFormat("0.000000"); // double result = Double.parseDouble(df.format(result)); } int rt(int x) { if (x != fa[x]) { int to = rt(fa[x]); // dp[x] ^= dp[fa[x]]; fa[x] = to; return to; } return x; } void combine(int x, int y, int val) { int rt1 = rt(x); int rt2 = rt(y); if (rt1 == rt2) return; fa[rt1] = rt2; // dp[rt1] = dp[x] ^ dp[y] ^ val; g--; } int fa[]; int g; static int MAXN = 10000; static Random rd = new Random(348957438574659L); static int[] ch[], val, size, rnd, cnt; static int len = 0, rt = 0; // return new node, the node below s static int rotate(int s, int d) { // child int x = ch[s][d ^ 1]; // give me grandson ch[s][d ^ 1] = ch[x][d]; // child become father ch[x][d] = s; // update size, update new son first update(s); update(x); return x; } static void update(int s) { size[s] = size[ch[s][0]] + size[ch[s][1]] + cnt[s]; } // 0 for left, 1 for right static int cmp(int x, int num) { if (val[x] == num) return -1; return num < val[x] ? 0 : 1; } static int insert(int s, int num) { if (s == 0) { s = ++len; val[s] = num; size[s] = 1; rnd[s] = rd.nextInt(); cnt[s] = 1; } else { int d = cmp(s, num); if (d != -1) { ch[s][d] = insert(ch[s][d], num); // father's random should be greater if (rnd[s] < rnd[ch[s][d]]) { s = rotate(s, d ^ 1); } else { update(s); } } else { ++cnt[s]; ++size[s]; } } return s; } static int del(int s, int num) { int d = cmp(s, num); if (d != -1) { ch[s][d] = del(ch[s][d], num); update(s); } else if (ch[s][0] * ch[s][1] == 0) { if (--cnt[s] == 0) { s = ch[s][0] + ch[s][1]; } } else { int k = rnd[ch[s][0]] < rnd[ch[s][1]] ? 0 : 1; // k points to smaller random value,then bigger one up s = rotate(s, k); // now the node with value num become the child ch[s][k] = del(ch[s][k], num); update(s); } return s; } static int getKth(int s, int k) { int lz = size[ch[s][0]]; if (k >= lz + 1 && k <= lz + cnt[s]) { return val[s]; } else if (k <= lz) { return getKth(ch[s][0], k); } else { return getKth(ch[s][1], k - lz - cnt[s]); } } static int getRank(int s, int value) { if (s == 0) return 1; if (value == val[s]) return size[ch[s][0]] + 1; if (value < val[s]) return getRank(ch[s][0], value); return getRank(ch[s][1], value) + size[ch[s][0]] + cnt[s]; } static int getPre(int data) { int ans = -1; int p = rt; while (p > 0) { if (data > val[p]) { if (ans == -1 || val[p] > val[ans]) ans = p; p = ch[p][1]; } else p = ch[p][0]; } return ans != -1 ? val[ans] : (-2147483647); } static int getNext(int data) { int ans = -1; int p = rt; while (p > 0) { if (data < val[p]) { if (ans == -1 || val[p] < val[ans]) ans = p; p = ch[p][0]; } else p = ch[p][1]; } return ans != -1 ? val[ans] : 2147483647; } static boolean find(int s, int num) { while (s != 0) { int d = cmp(s, num); if (d == -1) return true; else s = ch[s][d]; } return false; } static int ans = -10000000; static boolean findX(int s, int num) { while (s != 0) { if (val[s] <= num) { ans = num; } int d = cmp(s, num); if (d == -1) return true; else { s = ch[s][d]; } } return false; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } void linear_sort(int arr[]) { int d = 65536; ArrayDeque bucket[] = new ArrayDeque[d]; for (int j = 0; j < d; ++j) { bucket[j] = new ArrayDeque(); } for (int u : arr) { bucket[u % d].offer(u); } int pos = 0; for (int j = 0; j < d; ++j) { while (bucket[j].size() > 0) { arr[pos++] = (int)bucket[j].pollFirst(); } } for (int u : arr) { bucket[u / d].offer(u); } pos = 0; for (int j = 0; j < d; ++j) { while (bucket[j].size() > 0) { arr[pos++] = (int)bucket[j].pollFirst(); } } } int cur = 0; int h[]; int to[]; int ne[]; void add(int u, int v) { to[cur] = v; ne[cur] = h[u]; h[u] = cur++; } public boolean dfs(String cur, HashMap<String, Integer> vis, Map<String, List<String>> list, int cl) { vis.put(cur, cl); for (String hp : list.get(cur)) { if (vis.containsKey(hp)) { if (vis.get(hp) == cl) { return false; } } else { if (!dfs(hp, vis, list, 1 - cl)) { return false; } } } return true; } public class MultiSet { TreeMap<Integer, Integer> map; int ct = 0; MultiSet() { map = new TreeMap<>(); } public void remove(int key) { if (map.containsKey(key)) { int times = map.get(key); if (times == 1) { map.remove(key); } else { map.put(key, times - 1); } ct--; } } public void add(int key) { map.put(key, map.getOrDefault(key, 0) + 1); ct++; } public int last() { return map.lastKey(); } public int first() { return map.firstKey(); } public int size() { return ct; } } int color[], dfn[], low[], stack[]; int sccno[]; boolean iscut[]; int time = 0, top = 0; int scc_cnt; int dcc_cnt; List<Integer> dcc[]; int root = 0; // 无向图的强连通分量 void tarjanNonDirect(int u) { low[u] = dfn[u] = ++time; stack[top++] = u; int child = 0; for (int i = h[u]; i != -1; i = ne[i]) { int v = to[i]; if (dfn[v] == 0) { tarjanNonDirect(v); low[u] = Math.min(low[u], low[v]); if (low[v] >= dfn[u]) { if (u != root || ++child > 1) { // 不是root,直接记为cut,是root,判断是否有两个儿子 iscut[u] = true; } ++dcc_cnt; // 一个割点可能会被多个点双共享 int z = -1; do { z = stack[--top]; //dcc[dcc_cnt].add(z); } while (z != v); //dcc[dcc_cnt].add(u); } } else { low[u] = Math.min(low[u], dfn[v]); // 没有特判是否直接指向父亲 // 回边,使用dfn【v】更新low【u】,因为可能是指向父亲,而父亲的low可能比较小 } } } long dp11[][]; public long s(int l, int r, int a[]) { if (l > r) return 0; if (dp11[l][r] != 0) { return dp11[l][r]; } if (l == r) { return dp11[l][r] = a[l]; } return dp11[l][r] = Math.max((long)a[l] - s(l + 1, r, a), (long)a[r] - s(l, r - 1, a)); } int dp[][]; public void solve(int testNumber, InputReader in, PrintWriter out) { int fk = 1;int n; int q[] = new int[500010]; while(fk-->0) { try { n = in.nextInt(); }catch (Exception e){ break; } long x[] = new long[n + 1]; long p[] = new long[n + 1]; long s[] = new long[n + 1]; long c[] = new long[n + 1]; long px[] = new long[n + 1]; for (int i = 1; i <= n; ++i) { x[i] = in.nextLong(); p[i] = in.nextLong(); s[i] = s[i-1] + p[i]; c[i] = in.nextLong(); px[i] = px[i-1] + p[i]*x[i]; } long dp[] = new long[n + 1]; int st = 0; int e = 0; q[e++] = 0; for (int i = 1; i <= n; ++i) { // dp[i] = dp[j] + c[i] + (s[i] - s[j])*x[i] - (px[i] - px[j]) // dp[j] + px[j] = s[j]*x[i] + dp[i] - c[i] + px[i] - x[i]*s[i]; // dp[i] = t[i]*(c[i] - c[0]) + s * (c[n-1] -c[0]); // for(int j=0;j<i;++j){ // dp[i] = Math.min(dp[i] , dp[j] + c[i]2 + c[j]2 - 2c[i]*c[j] + s)) // dp[j] + c[j]2 = dp[i] -c[i]2 + 2*c[i] * c[j] -s; // dp[i] = Math.min(dp[i] , dp[j] + (c[i]-c[j])*(t[i]) + s *(c[n-1] - c[j])); // // } // dp[i] = dp[q[st]] + (c[i] - c[q[st]] + s); // // while(st<e && dp[q[e-1]] - c[q[e-1]] >= dp[i] - c[i]) e--; // // q[e++] = i; while (st + 1 < e && (dp[q[st + 1]] + px[q[st+1]] - dp[q[st]] - px[q[st]]) <= (s[q[st + 1]] - s[q[st]]) * (x[i])) st++; dp[i] = dp[q[st]] + c[i] + (s[i] - s[q[st]])*x[i] - (px[i] - px[q[st]]); while (st + 1 < e && (s[i] - s[q[e - 1]]) * (dp[q[e - 1]] + px[q[e-1]] - dp[q[e - 2]] - px[q[e-2]]) - (dp[i] + px[i] - dp[q[e - 1]] - px[q[e-1]]) * (s[q[e - 1]] - s[q[e - 2]]) >= 0) e--; q[e++] = i; } out.println(dp[n]); } } static long mul(long a, long b, long p) { long res = 0, base = a; while (b > 0) { if ((b & 1L) > 0) res = (res + base) % p; base = (base + base) % p; b >>= 1; } return res; } static long mod_pow(long k, long n, long p) { long res = 1L; long temp = k % p; while (n != 0L) { if ((n & 1L) == 1L) { res = mul(res, temp, p); } temp = mul(temp, temp, p); n = n >> 1L; } return res % p; } public static double roundD(double result, int scale) { BigDecimal bg = new BigDecimal(result).setScale(scale, RoundingMode.UP); return bg.doubleValue(); } } private static void solve() { InputStream inputStream = System.in; // InputStream inputStream = null; // try { // inputStream = new FileInputStream(new File("D:\\chrome_download\\exp.out")); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } OutputStream outputStream = System.out; // OutputStream outputStream = null; // File f = new File("D:\\chrome_download\\"); // try { // f.createNewFile(); // } catch (IOException e) { // e.printStackTrace(); // } // try { // outputStream = new FileOutputStream(f); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task task = new Task(); task.solve(1, in, out); out.close(); } public static void main(String[] args) { new Thread(null, () -> solve(), "1", (1 << 20)).start(); // solve(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String nextLine() { String line = null; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public char nextChar() { return next().charAt(0); } public int[] nextArray(int n) { int res[] = new int[n]; for (int i = 0; i < n; ++i) { res[i] = nextInt(); } return res; } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
C++(clang++11) 解法, 执行用时: 359ms, 内存消耗: 24188K, 提交时间: 2020-10-21 14:58:04
#include<bits/stdc++.h> #define k(i,j) 1.0*(f[j]-f[i])/(p[j]-p[i]) using namespace std; const int nn=1001021; int n,t,tt,x[nn],c[nn],q[nn]; long long ans,p[nn],f[nn]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%lld%d",x+i,p+i,c+i); for(int i=1;i<=n;i++) f[0]+=p[i]*(x[n]-x[i]),p[i]+=p[i-1]; ans=(f[0]+=c[n]); for(int i=1,j;i<n;i++){ if(t>=tt)t=tt; else while(t<tt&&k(q[t],q[t+1])<x[i]-x[n])t++; j=q[t],f[i]=f[j]-(p[i]-p[j])*(x[n]-x[i])+c[i]; for(;tt&&k(q[tt-1],q[tt])>=k(q[tt-1],i);tt--); ans=min(ans,f[q[++tt]=i]); }return printf("%lld",ans),0; }