intmain(){ scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); while (q--) { scanf("%d", &k); int l = 1, r = n; while (l < r) { int mid = l+r >> 1; if (a[mid] >= k) r = mid; else l = mid+1; } if (a[l] != k) { puts("-1 -1"); continue; } printf("%d ", r-1); l = 1, r = n; while (l < r) { int mid = l+r+1 >> 1; if (a[mid] <= k) l = mid; else r = mid-1; } printf("%d\n", r-1); } return0; }
[CCC2021 S3] Lunch Concert
有 N 个人,第 i 个人的速度为 Wi秒每米,听力为 Di,即能听见距离他不超过 Di 米处的音乐,初始在 Pi 位置。
你要在 c 位置处开音乐会,这个 c 由你决定且为整数。这 N 个人都会靠近你直到能听到你。你要最小化每个人移动的时间之和。
typedeflonglong LL; constint N = 200010; LL p[N], w[N], d[N]; int n;
LL f(int x){ LL cost = 0; for (int i = 1; i <= n; i++) { if (x < p[i] - d[i]) cost += w[i]*(p[i]-d[i]-x); elseif (x > p[i] + d[i]) cost += w[i]*(x-(p[i]+d[i])); } return cost; }
intmain(){ scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld%lld%lld", &p[i], &w[i], &d[i]); LL l = 0, r = 1e9; while (r-l > 10) { LL a = l + (r-l) / 3, b = r - (r-l) / 3; if (f(a) > f(b)) l = a; else r = b; } LL res = 1e18; for (int i = l; i <= r; i++) res = min(res, f(i)); printf("%lld\n", res); return0; }