1 条题解
-
0
贪心
照亮值计算:
对于朝西(W)的路灯,它能照亮左侧的所有路灯,其照亮值为左侧路灯的数量(即 )。
对于朝东(E)的路灯,它能照亮右侧的所有路灯,其照亮值为右侧路灯的数量(即 )。
关键观察:
改变一个路灯的朝向可能会增加照亮值。
对于朝西的路灯,改为朝东后,照亮值从 变为 ,增益为 。
对于朝东的路灯,改为朝西后,照亮值从 变为 ,增益为 。
贪心策略:
计算每个路灯改变朝向后的增益。
将增益从大到小排序。
对于每个 ,选择前 个增益最大的改变,总照亮值等于初始总照亮值加上前 个增益之和。
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=2e5+10; int a[N]; void solve() { int t; // cin >> t; t=1; while(t--) { int n; cin >> n; string s; cin >> s; long long ans=0; for(int i=1; i<=n; i++) { if(s[i-1]=='W') a[i]=max(0, n-i - (i-1)), ans+=i-1; else a[i] =max(0, (i-1) - (n-i)), ans+=n-i; } sort(a+1, a+1+n, greater<int>()); for(int i=1; i<=n; i++) { ans+=a[i]; cout << ans <<' '; } cout << '\n'; } } int main() { // ios::sync_with_stdio(false); // cin.tie(0), cout.tie(0); solve(); return 0; }
- 1
信息
- ID
- 537
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 8
- 已通过
- 1
- 上传者