
1 #include2 #define N 10000005 3 using namespace std; 4 inline int read(){ 5 register int x=0,f=1;char ch=getchar(); 6 while(!isdigit(ch)) f=ch=='-'?-1:1,ch=getchar(); 7 while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-48,ch=getchar(); 8 return x*f; 9 } 10 11 char v[200000005]; 12 int pr[20000005]; 13 bool jud[N],lz[N];//jud:0:大根堆 1:小根堆 14 struct node{int c,f;}; 15 bool operator < (const node &a,const node &b){return a.c<b.c;} 16 17 struct DaHeap{ 18 node heap[N]; 19 int cnt; 20 void push(int V,int Fm){ 21 heap[++cnt]=(node){V,Fm}; 22 int x=cnt; 23 while(x>1){ 24 int now=x>>1; 25 if(heap[now]<heap[x]) swap(heap[x],heap[now]); 26 x=now; 27 } 28 } 29 node top(){return heap[1];} 30 void pop(){ 31 heap[1]=heap[cnt--]; 32 int x=1; 33 while((x<<1)<=cnt){ 34 int now=x<<1; 35 if((now|1)<=cnt&&heap[now] 1]) now++; 36 if(heap[x]<heap[now]) swap(heap[now],heap[x]); 37 x=now; 38 } 39 } 40 }da; 41 struct XiaoHeap{ 42 node heap[N]; 43 int cnt; 44 void push(int V,int Fm){ 45 heap[++cnt]=(node){V,Fm}; 46 int x=cnt; 47 while(x>1){ 48 int now=x>>1; 49 if(heap[x]<heap[now]) swap(heap[x],heap[now]); 50 x=now; 51 } 52 } 53 node top(){return heap[1];} 54 void pop(){ 55 heap[1]=heap[cnt--]; 56 int x=1; 57 while((x<<1)<=cnt){ 58 int now=x<<1; 59 if((now|1)<=cnt&&heap[now|1] ; 60 if(heap[now]<heap[x]) swap(heap[now],heap[x]); 61 x=now; 62 } 63 } 64 }xi; 65 int sizx,sizd;//小根堆的siz,大根堆的siz 66 //保证大根堆的个数永远是k/2 67 void Pre(){ 68 const long long M=2e8; 69 for(int i=2;i<=M;++i){ 70 if(!v[i]) v[i]=1,pr[++pr[0]]=i; 71 for(int j=1;j<=pr[0];++j){ 72 if(1LL*pr[j]*i>M) break; 73 v[i*pr[j]]=1; 74 if(i%pr[j]==0) break; 75 } 76 } 77 } 78 double ans; 79 int n,k,w; 80 void Get(){ 81 while(lz[xi.top().f]) xi.pop(); 82 while(lz[da.top().f]) da.pop(); 83 if(k&1) ans+=xi.top().c; 84 else ans+=1.0*(xi.top().c+da.top().c)/2; 85 } 86 int S[N],a[N],hk;//half of k 87 int main(){ 88 n=read(),k=read(),w=read();hk=k/2; 89 Pre(); 90 for(int i=1;i<=n;++i) S[i]=1LL*pr[i]*i%w; 91 for(int i=1;i<=n;++i) a[i]=S[i]+S[i/10+1]; 92 for(int i=1;i<=k;++i) da.push(a[i],i),jud[i]=0,sizd++; 93 node x; 94 while(sizd!=hk) x=da.top(),da.pop(),jud[x.f]=1,xi.push(x.c,x.f),sizd--,sizx++; 95 Get(); 96 for(int i=k+1;i<=n;++i){//每次添加i,删除i-k 97 int P=i-k;lz[P]=1; 98 if(jud[P]==0) sizd--; 99 else sizx--; 100 101 int V=a[i]; 102 while(lz[xi.top().f]) xi.pop(); 103 while(lz[da.top().f]) da.pop(); 104 if(V>=xi.top().c){//大于小根堆的堆首元素 105 xi.push(V,i); 106 jud[i]=1; 107 sizx++; 108 } 109 else{ 110 da.push(V,i); 111 jud[i]=0; 112 sizd++; 113 } 114 if(sizd>hk){ 115 sizd--;sizx++; 116 node T=da.top();da.pop(); 117 jud[T.f]=1; 118 xi.push(T.c,T.f); 119 } 120 else if(sizd<hk){ 121 sizd++;sizx--; 122 node T=xi.top();xi.pop(); 123 jud[T.f]=0; 124 da.push(T.c,T.f); 125 } 126 Get(); 127 } 128 printf("%.1lf\n",ans); 129 return 0; 130 }

1 #include2 #define N 100005 3 using namespace std; 4 inline int read(){ 5 register int x=0,f=1;char ch=getchar(); 6 while(!isdigit(ch)) f=ch=='-'?-1:1,ch=getchar(); 7 while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-48,ch=getchar(); 8 return x*f; 9 } 10 int n,k,Max;long long ans[2]; 11 int buc[N],a[N],lsh[N]; 12 int main(){ 13 n=read(),k=read(); 14 for(int i=1;i<=n;++i) a[i]=read(),lsh[++lsh[0]]=a[i]; 15 sort(lsh+1,lsh+n+1);lsh[0]=unique(lsh+1,lsh+n+1)-(lsh+1); 16 for(int i=1;i<=n;++i) a[i]=lower_bound(lsh+1,lsh+lsh[0]+1,a[i])-lsh; 17 for(register int i=1;i<=k;++i){ 18 register int p=read(); ans[0]=0;ans[1]=0; Max=0; 19 for(register int j=1;j<=p;++j) buc[a[j]]++,Max=max(Max,a[j]); 20 ans[1]+=lsh[Max];buc[Max]--; 21 if(!buc[Max]) while(!buc[Max]) Max--; 22 23 for(register int j=1;j j){ 24 if(j+p<=n){ 25 if(a[j+p]>=Max){ans[j&1^1]+=lsh[a[j+p]];continue;} 26 else buc[a[j+p]]++; 27 } 28 ans[j&1^1]+=lsh[Max];buc[Max]--; 29 if(!buc[Max]) while(!buc[Max]) Max--; 30 } 31 printf("%lld\n",ans[1]-ans[0]); 32 } 33 }

1 #include2 #define int long long 3 #define N 100005 4 using namespace std; 5 inline int read(){ 6 register int x=0,f=1;char ch=getchar(); 7 while(!isdigit(ch)) f=ch=='-'?-1:1,ch=getchar(); 8 while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-48,ch=getchar(); 9 return x*f; 10 } 11 int n,v,ans; 12 int p[N],num[N],up[2][N][105],dw[2][N][105];//x点有没有放面包屑 13 vector<int>g[N]; 14 void dfs(int x,int prt){ 15 up[0][x][0]=0,up[1][x][1]=num[x],dw[0][x][0]=0,dw[1][x][1]=num[x]; 16 for(auto &to:g[x]) if(to!=prt){ 17 dfs(to,x); 18 for(int i=0;i<=v;++i) 19 ans=max(ans,max(max(dw[0][x][i],dw[1][x][i]-p[to])+max(up[0][to][v-i],up[1][to][v-i]), 20 max(up[0][x][i],up[1][x][i])+max(dw[0][to][v-i],dw[1][to][v-i]-p[x]))); 21 //我认为i需要到v因为可能会有一条链更优的情况 UPD:可以在后面直接统计 22 for(int i=1;i<=v;++i){ 23 up[0][x][i]=max(up[0][x][i],max(up[0][to][i],up[1][to][i])); 24 up[1][x][i]=max(up[1][x][i],max(up[0][to][i-1]+num[x]-p[to],up[1][to][i-1]+num[x]-p[to])); 25 dw[0][x][i]=max(dw[0][x][i],max(dw[0][to][i],dw[1][to][i]-p[x])); 26 dw[1][x][i]=max(dw[1][x][i],max(dw[0][to][i-1]+num[x],dw[1][to][i-1]-p[x]+num[x])); 27 } 28 } 29 30 for(int i=0;i<=v;++i) ans=max(ans,max(max(up[0][x][i],up[1][x][i]),max(dw[0][x][i],dw[1][x][i]))); 31 } 32 signed main(){ 33 n=read(),v=read(); 34 for(int i=1;i<=n;++i) p[i]=read(); 35 for(int i=1;i i){ 36 int u=read(),v=read(); 37 num[u]+=p[v];num[v]+=p[u]; 38 g[u].push_back(v);g[v].push_back(u); 39 } 40 dfs(1,0); 41 printf("%lld\n",ans); 42 }
我有几个问题:
-
为什么中位数要排序啊啊啊啊
-
为什么分数差不是绝对值啊啊啊
-
为什么用clock越交分越少啊啊啊