题解:
第一题
#include<bits/stdc++.h> using namespace std; #define ll long long const int M = 1e5 + 10; ll a[M], b[M], ans; priority_queue <ll, vector<ll> , greater<ll> > Q; int main(){ freopen("buy.in","r",stdin); freopen("buy.out","w",stdout); int n; scanf("%d", &n); for(int i = 1; i <= n; i++)scanf("%I64d", &a[i]); for(int i = 1; i <= n; i++)scanf("%I64d", &b[i]); for(int i = 1; i <= n; i++){ Q.push(a[i]); if(!Q.empty()){ int u = Q.top(); if(u < b[i]) { ans += b[i] - u; Q.pop(); Q.push(b[i]); } } } printf("%I64d ",ans); }
第二题:
#include<bits/stdc++.h> using namespace std; #define ll long long; const int M = 1e5 + 10; int n, a[M], sum[M]; struct Node{ int mx, tag; Node *ls, * rs; void up(){ mx = max(ls->mx, rs->mx); } void down(){ if(tag){ ls->tag += tag; rs->tag += tag; ls->mx += tag; rs->mx += tag; } tag = 0; } }pool[M << 2], *tail = pool, *root; Node * build(int lf = 1, int rg = n){ Node * nd = ++tail; if(lf == rg); else { int mid = (lf + rg) >> 1; nd->ls = build(lf, mid); nd->rs = build(mid + 1, rg); } return nd; } #define Ls nd->ls, lf, mid #define Rs nd->rs, mid+1, rg void insert(int pos, int val, Node * nd = root, int lf = 1, int rg = n){ if(lf == rg)nd->mx = val; else { int mid = (lf + rg) >> 1; if(pos <= mid)insert(pos, val, Ls); else insert(pos, val, Rs); nd->up(); } } void modify(int L, int R, int val, Node * nd = root, int lf = 1, int rg = n){ if(L <= lf && rg <= R){ nd->mx += val; nd->tag += val; } else { nd->down(); int mid = (lf + rg) >> 1; if(L <= mid)modify(L, R, val, Ls); if(R > mid) modify(L, R, val, Rs); nd->up(); } } int query(int L, int R, Node * nd = root, int lf = 1, int rg = n){ if(L <= lf && rg <= R)return nd->mx; else { nd->down(); int mid = (lf + rg) >> 1; int ans = -2e9; if(L <= mid)ans = query(L, R, Ls); if(R > mid) ans = max(ans, query(L, R, Rs)); return ans; } } int main(){ freopen("invest.in","r",stdin); freopen("invest.out","w",stdout); int s, e; int ans = -2e9; scanf("%d%d%d", &n, &s, &e); root = build(); for(int i = 1; i <= e; i++){ scanf("%d", a + i); sum[i] = sum[i - 1] + a[i]; insert(i, sum[i]); } int llf = 1, lf = s, rg = e, now = 1; ans = max(ans, query(lf, rg)); //cout<<ans<<endl; for(int i = e+1; i <= n; i++){ scanf("%d", a + i); sum[i] = sum[i - 1] + a[i]; lf++, rg++; insert(rg, sum[i] - sum[i - e]); modify(llf+1, rg - 1, -a[now]); llf++; now++; int p = query(lf, rg); ans = max(ans, p); //cout<<ans<<endl; } for(int i = llf; i + s - 1 <= n; i++){ modify(llf+1, n, -a[llf]); llf++; ans = max(ans, query(i+s-1, n)); } printf("%d ",ans); return 0; }
#include<stdio.h> int a[100002],k[100002]; int max(int x,int y) { return x>y?x:y; } int qin() { char ch; int in=0; bool flag=0; while(ch!='-'&&!(ch>='0'&&ch<='9')) ch=getchar(); if(ch=='-') { flag=1; ch=getchar(); } do { in=in*10+ch-'0'; ch=getchar(); } while(ch>='0'&&ch<='9'); if(flag) in*=-1; return in; } int co() { freopen("invest.in","r",stdin); freopen("invest.out","w",stdout); int n,i,t,head,tail,s,ans=-10000000,j; scanf("%d%d%d",&n,&s,&t); for(i=1;i<=n;i++) a[i]=a[i-1]+qin(); head=tail=1; for(i=s;i<=n;i++) { while(head<tail&&i-k[head]>t) head++; while(head<tail&&a[i-s]<a[k[tail-1]]) tail--;//k[a[i]]????? k[tail++]=i-s; ans=max(ans,a[i]-a[k[head]]); } printf("%d ",ans); } int ccc=co(); int main(){; }
第三题:
#include<bits/stdc++.h> using namespace std; const int M = 1e6 + 10; #define ll long long const ll mod = 19930726; char c[M], s[ M << 1 ]; ll cnt[M]; int len, pal[M << 1]; ll ksm(ll a, ll b){ ll ret = 1; for( ; b; b >>= 1, a = a * a % mod){ if(b & 1) ret = ret * a % mod; } return ret; } void init(){ len = strlen(c); s[0] = '$'; for(int i = 0; i < len; i++){ s[i * 2 + 1] = '#'; s[i * 2 + 2] = c[i]; } s[len * 2 + 1] ='#'; s[len * 2 + 2] ='@'; int L = len * 2 + 1; int id = 1, Maxid = 0; for(int i = 1; i <= L; i++){ if(Maxid >= i)pal[i] = min(Maxid - i + 1, pal[2 * id - i]); else pal[i] = 1; while(s[i - pal[i]] == s[i + pal[i]])pal[i]++; if(i + pal[i] - 1 > Maxid) id = i, Maxid = i + pal[i] - 1; if((i&1) == 0)cnt[(pal[i] * 2 - 1) / 2]++; } } int main(){ freopen("rehearse.in","r",stdin); freopen("rehearse.out","w",stdout); int n; ll k, ans = 1, sum = 0; cin>>n>>k; scanf("%s", c); init(); int limit = n & 1 ? n : n - 1; for(int i = limit; i > 0; i -= 2){ if(i != 1)cnt[i - 2] += cnt[i]; if(sum + cnt[i] <= k)ans = (ans * ksm(i, cnt[i])) % mod; else ans = (ans * ksm(i, k - sum)) % mod; sum += cnt[i]; if(sum >= k)break; } if(sum >= k)cout<<ans<<endl; else printf("-1 "); }
今天复制freopen, 两个stdin, 惨痛教训