题解:典型的二分
1 #pragma warning(disable:4996) 2 #include<map> 3 #include<queue> 4 #include<string> 5 #include<cstdio> 6 #include<cstring> 7 #include<iostream> 8 #include<algorithm> 9 #define ll long long 10 using namespace std; 11 12 const int maxn = 20005; 13 14 ll s; 15 int n, p; 16 int a[maxn]; 17 18 bool check(int mid) { 19 int pos = lower_bound(a + 1, a + n + 1, mid) - a; 20 int ans = 0; 21 for (int i = pos; i <= n; i++) ans += (a[i] - mid); 22 if (ans > s / p) return false; 23 return true; 24 } 25 26 int Solve() { 27 int l = 1, r = a[n]; 28 for (int i = 1; i <= 30; i++) { 29 int mid = (l + r) >> 1; 30 if (check(mid)) r = mid; 31 else l = mid + 1; 32 } 33 return l; 34 } 35 36 int main() 37 { 38 while (scanf("%d", &n) != EOF) { 39 for (int i = 1; i <= n; i++) scanf("%d", &a[i]); 40 sort(a + 1, a + n + 1); 41 scanf("%d%lld", &p, &s); 42 printf("%d ", Solve()); 43 } 44 return 0; 45 }
题解:数据比较小的时候建图,跑DFS统计总的污染值即可。
感受:题意不明!!!!!书替换后不会对以前的结果造成影响。所以~~要n何用。
1 #pragma warning(disable:4996) 2 #include<vector> 3 #include<string> 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 #define ll long long 9 using namespace std; 10 11 const ll INF = 1000000000000000; 12 13 int n, m; 14 int p[30], pos[30]; 15 16 vector<int> book[30]; 17 18 bool use[30]; 19 20 ll dp[30]; 21 22 void DFS(int u) { 23 use[u] = 1; 24 dp[u] = p[u]; 25 for (int i = 0; i < book[u].size(); i++) { 26 int v = book[u][i]; 27 if (!use[v]) { 28 DFS(v); 29 dp[u] += dp[v]; 30 } 31 } 32 } 33 34 int main() 35 { 36 while (cin >> n) { 37 for (int i = 1; i <= n; i++) pos[i] = i; 38 for (int i = 1; i <= n; i++) cin >> p[i]; 39 40 cin >> m; 41 int k = n; 42 for (int i = 1; i <= m; i++) { 43 int op; 44 cin >> op; 45 if (op == 0) { 46 int x, v; 47 cin >> x >> v; 48 pos[x] = (++k); 49 p[k] = v; 50 } 51 else { 52 int x, l, r; 53 cin >> x >> l >> r; 54 for (int i = l; i <= r; i++) book[pos[x]].push_back(pos[i]); 55 } 56 } 57 58 ll ans = INF; 59 for (int i = 1; i <= k; i++) { 60 memset(use, 0, sizeof(use)); 61 memset(dp, 0, sizeof(dp)); 62 DFS(i); 63 ans = min(ans, dp[i]); 64 } 65 cout << ans << endl; 66 67 for (int i = 1; i <= k; i++) book[i].clear(); 68 } 69 return 0; 70 }