A 张经理的员工
(a<b)a之前的员工去a那里,b之后的员工去b那里;a,b之间的,a+(a+b)/2前的去a那里
#include<cmath> #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<iomanip> #include<iostream> using namespace std; typedef long long ll; ll qm[100010],qn[100010];//i及之前员工的个数,i及之前员工的下标和 ll hm[100010],hn[100010];//i及之后员工的个数,i及之后员工的下标和 int main() { int n,q,i,a,b,x[100010]={0}; cin>>n>>q; for(i=0;i<n;i++){ cin>>a; x[a]++; } qn[1]=1*x[1]; qm[1]=x[1]; for(i=2;i<=100000;i++){ qn[i]=qn[i-1]+1ll*i*x[i]; qm[i]=qm[i-1]+1ll*x[i]; } hn[100000]=100000*x[100000]; hm[100000]=x[100000]; for(i=99999;i>=1;i--){ hn[i]=hn[i+1]+1ll*i*x[i]; hm[i]=hm[i+1]+x[i]*1ll; } while(q--){ cin>>a>>b; if(a>b){ swap(a,b); } ll sum1 =qm[a]*a-qn[a];//a前的员工去a那里要走距离 ll sum2 =hn[b]-hm[b]*b;//b之后的员工去b那里要走的距离 ll z=0,w=0; if(b-a>1){ i=a+(b-a)/2; z=qn[i]-qn[a]-(qm[i]-qm[a])*a;//a+(a+b)/2之前 w=b*(qm[b-1]-qm[i])-(qn[b-1]-qn[i]);//a+(a+b)/2之后 } //cout<<sum1<<' '<<sum2<<' '<<z<<' '<<w<<endl; cout<<sum1+sum2+z+w<<endl; } return 0; }
B 随机数列
求这串数的极差和方差
#include<cmath> #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<iomanip> #include<iostream> using namespace std; typedef long long ll; int main() { int k,n,a[1010],j,i,max,min; double sum,sum2; cin>>k; while(k--){ cin>>n; sum=0,sum2=0; max=-1; min=99999; for(i=0;i<n;i++){ cin>>a[i]; sum+=a[i]; if(max<a[i]){ max=a[i]; } if(min>a[i]){ min=a[i]; } } cout<<max-min<<" "; sum/=n; for(i=0;i<n;i++){ sum2+=(a[i]-sum)*(a[i]-sum); } printf("%.3f ",sum2/n); } return 0; }
D 卡拉兹函数
当n是奇数时,输出3*n+1,当n是偶数时,输出n/2
#include<cmath> #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<iomanip> #include<iostream> using namespace std; typedef long long ll; int main() { int n; cin>>n; if(n%2==0){ cout<<n/2<<endl; }else{ cout<<n*3+1<<endl; } return 0; }