Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "weight"
- using namespace std;
- int typetest;
- inline void fastIOfileinput(){
- ios_base:: sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if(fopen(task".inp", "r")){
- freopen(task".inp", "r", stdin);
- freopen(task".out", "w", stdout);
- }
- if(fopen(task".in", "r")){
- freopen(task".in", "r", stdin);
- freopen(task".out", "w", stdout);
- }
- typetest = 0;
- }
- const int N = 4e5 + 2;
- int m, n;
- int a[N], l[N], r[N], h[N], k[N];
- stack<int> s, q;
- int maxn[1000003];
- int minn[1000003];
- inline void Enter(){
- cin >> n;
- for(int i = 1; i <= n; ++i)
- cin >> a[i];
- }
- inline void solve(){
- for(int i = 1; i <= n; ++i){
- while(!s.empty() && a[i] <= a[s.top()])
- s.pop();
- l[i] = (s.empty() ? 0 : s.top());
- s.push(i);
- while(!q.empty() && a[i] >= a[q.top()])
- q.pop();
- h[i] = (q.empty() ? 0 : q.top());
- q.push(i);
- }
- while(!s.empty()) s.pop();
- while(!q.empty()) q.pop();
- for(int i = n; i > 0; --i){
- while(!s.empty() && a[i] <= a[s.top()])
- s.pop();
- r[i] = (s.empty() ? n + 1 : s.top());
- s.push(i);
- while(!q.empty() && a[i] >= a[q.top()])
- q.pop();
- k[i] = (q.empty() ? n + 1 : q.top());
- q.push(i);
- }
- ll ans = 0;
- for(int i = 1; i <= n; ++i){
- // cerr << l[i] << " " << r[i] << " " << h[i] << " " << k[i] << " ";
- ans -= (ll)(i - max(l[i], maxn[a[i]]) ) * (r[i] - i) * a[i] - (ll)a[i];
- maxn[a[i]] = i;
- // cerr << "\n" << ans;
- ans += (ll)(i - max(minn[a[i]], h[i]) ) * (k[i] - i) * a[i] - (ll)a[i];
- // cerr << "\n" << ans << "\n";
- minn[a[i]] = i;
- }
- cout << ans;
- }
- #define task "project"
- using namespace std;
- int typetest;
- inline void fastIOfileinput(){
- ios_base:: sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if(fopen(task".inp", "r")){
- freopen(task".inp", "r", stdin);
- freopen(task".out", "w", stdout);
- }
- if(fopen(task".in", "r")){
- freopen(task".in", "r", stdin);
- freopen(task".out", "w", stdout);
- }
- typetest = 0;
- }
- const int N = 4e5 + 2;
- int h, ss, d, n, k;
- int a[N];
- inline void Enter(){
- cin >> n >> h >> ss >> d;
- for(int i = 1; i <= n; ++i)
- cin >> a[i];
- k = (int) ceil((ld)(h + d) * 1.0/ ss);
- }
- inline void solve(){
- ll ans = (ll)(h) * a[1];
- int b[N];
- b[1] = a[1];
- deque<int> s;
- for(int i = 1; i <= min(k, n); ++i){
- while(!s.empty() && a[s.back()] <= a[i])
- s.pop_back();
- s.pb(i);
- }
- for(int i = 1; i <= n; ++i){
- while(!s.empty() && a[s.back()] <= a[min(i + k, n)])
- s.pop_back();
- while(!s.empty() && s.front() <= i)
- s.pop_front();
- if(i + k <= n ) s.pb(i + k);
- int M = a[s.front()];
- // cerr << s.front() << " " << M << "\n";
- ans += (ll) b[i] * ss;
- if(M >= b[i]){
- b[i + 1] = b[i];
- if(a[i + 1] > b[i]){
- ans += (ll) h * (a[i + 1] - b[i]);
- b[i + 1] = a[i + 1];
- }
- }
- else{
- ans += (ll) d * (b[i] - M);
- b[i + 1] = M ;
- }
- }
- cout << ans + (ll) b[n] * d << "\n";
- for(int i = 1; i <= n; ++i)
- cout << b[i] << " ";
- }
- #define task "cocktail"
- using namespace std;
- int typetest;
- inline void fastIOfileinput(){
- ios_base:: sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if(fopen(task".inp", "r")){
- freopen(task".inp", "r", stdin);
- freopen(task".out", "w", stdout);
- }
- if(fopen(task".in", "r")){
- freopen(task".in", "r", stdin);
- freopen(task".out", "w", stdout);
- }
- typetest = 0;
- }
- const int N = 1e3 + 2;
- int n, k;
- int a[N];
- queue<int> s;
- bool ck[2001];
- int trace[2001];
- int ans[2001];
- inline void Enter(){
- cin >> n >> k;
- for(int i = 1; i <= n; ++i)
- cin >> a[i], a[i] -= k;
- }
- void traces(int i){
- if(i - a[trace[i]] == 0){
- ++ans[trace[i]];
- return;
- }
- ++ans[trace[i]];
- traces(i - a[trace[i]]);
- }
- inline void solve(){
- for(int i = 1; i <= n; ++i)
- if(a[i] > -1){
- s.push(a[i]);
- trace[a[i]] = i;
- ck[a[i]] = 1;
- }
- bool ok = 0;
- while(!s.empty()){
- int c = s.front();
- s.pop();
- if(c == 0)
- {ok = 1; break;}
- for(int i = 1; i <= n; ++i)
- if(c + a[i] <= 2000 && c + a[i] > -1 && !ck[c + a[i]]){
- s.push(c + a[i]);
- ck[c + a[i]] = 1;
- trace[c + a[i]] = i;
- }
- }
- if(!ok) cout << "NO";
- else{
- cout << "YES\n";
- traces(0);
- for(int i = 1; i <= n; ++i)
- cout << ans[i] << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment