Delete c/sourcecode/AlgorithmO/20230911 directory

This commit is contained in:
m1ng 2023-11-15 10:15:22 +08:00 committed by GitHub
parent f20c1a56c8
commit 367565b051
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 0 additions and 825 deletions

View file

@ -1,201 +0,0 @@
/*
E. Toocold之区间最大子段和
Description
Toocold学会了怎样求区间最大子段和,.n个数,l限定在[ll,rr], r限定在[ll2,rr2]?
Input
cas(1<=cas<=10)cas
cas的第一行是一个数n(1<=n<=100000)n个数
n个数,a1,a2,a3...an(-1000<=ai<=1000)
q(1<=q<=100000)q个询问
q行每行四个数字ll, rr, ll2, rr2.(1<=ll,rr,ll2,rr2<=n)(ll<=rr)(ll2<=rr2)(rr2>=ll)l的范围和右端点r的范围
Output
Sample Input
1
6
4 -3 1 -3 5 -2
2
1 4 2 5
1 1 3 4
Sample Output
4
2
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100015;
int n,m;
int a[MAXN],pre_sum[MAXN],suf_sum[MAXN],l1,l2,r1,r2;
struct tnode
{
int sum,lmax,rmax,ans;
int l,r;
};
tnode operator + (const tnode& a,const tnode& b)
{
tnode c;
c.l=a.l;
c.r=b.r;
c.sum=a.sum+b.sum;
c.lmax=max(a.lmax,a.sum+b.lmax);
c.rmax=max(a.rmax+b.sum,b.rmax);
c.ans=max(a.ans,b.ans);
c.ans=max(c.ans,a.rmax+b.lmax);
return c;
}
struct Segment_Tree
{
tnode t[4*MAXN];
void update(int root)
{
int ch=root<<1;
t[root]=t[ch]+t[ch+1];
}
void buildt(int root,int l,int r,int A[])
{
if(l!=r)
{
int mid=(l+r)>>1;
int ch=root<<1;
buildt(ch,l,mid,A);
buildt(ch+1,mid+1,r,A);
update(root);
}
else
{
t[root].ans=t[root].lmax=t[root].rmax=t[root].sum=A[l];
t[root].l=t[root].r=l;
}
}
tnode query(int root,int l,int r)
{
if(t[root].l==l&&t[root].r==r)
{
return t[root];
}
int mid=(t[root].l+t[root].r)>>1;
int ch=root<<1;
if(r<=mid)return query(ch,l,r);
else if(l>mid)return query(ch+1,l,r);
else return query(ch,l,mid)+query(ch+1,mid+1,r);
}
};
Segment_Tree ST1;
struct tnodes
{
int Max;
int l,r;
};
struct Segment_Tree_Max
{
tnodes t[4*MAXN];
void buildt(int root,int l,int r,int A[])
{
t[root].l=l;
t[root].r=r;
if(l!=r)
{
int mid=(l+r)>>1;
int ch=root<<1;
buildt(ch,l,mid,A);
buildt(ch+1,mid+1,r,A);
t[root].Max=max(t[ch].Max,t[ch+1].Max);
}
else t[root].Max=A[l];
}
int query(int root,int l,int r)
{
if(t[root].l==l&&t[root].r==r)
{
return t[root].Max;
}
int mid=(t[root].l+t[root].r)>>1;
int ch=root<<1;
if(r<=mid)return query(ch,l,r);
else if(l>mid)return query(ch+1,l,r);
else return max(query(ch,l,mid),query(ch+1,mid+1,r));
}
};
Segment_Tree_Max ST2,ST3;
int get_sum(int l,int r)
{
if(l>r)return 0;
return pre_sum[r]-pre_sum[l-1];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
}
ST1.buildt(1,1,n,a);
pre_sum[0]=suf_sum[n+1]=0;
for(int i=1;i<=n;++i)
{
pre_sum[i]=pre_sum[i-1]+a[i];
}
for(int i=n;i;--i)
{
suf_sum[i]=suf_sum[i+1]+a[i];
}
ST2.buildt(1,1,n,pre_sum);
ST3.buildt(1,1,n,suf_sum);
scanf("%d",&m);
while(m--)
{
scanf("%d %d %d %d",&l1,&r1,&l2,&r2);
if(r1<l2)///3 无交
{
printf("%d\n",ST3.query(1,l1,r1)-pre_sum[n]+ST2.query(1,l2,r2));
}
else if(l2<=l1&&l1<=r2&&r2<=r1)///2
{
printf("%d\n",ST1.query(1,l1,r2).ans);
}
else if(l1<=l2&&l2<=r1&&r1<=r2)///1
{
int temp=ST1.query(1,l2,r1).ans;
if(l2-1>=l1)
{
temp=max(temp,ST3.query(1,l1,l2-1)-pre_sum[n]+ST2.query(1,l2,r2));
}
if(r1+1<=r2)
{
temp=max(temp,ST2.query(1,r1+1,r2)-pre_sum[n]+ST3.query(1,l1,r1));
}
printf("%d\n",temp);
}
else if(l1<=l2&&l2<=r2&&r2<=r1)///4
{
int temp=ST1.query(1,l2,r2).ans;
if(l2-1>=l1)
{
temp=max(temp,ST3.query(1,l1,l2-1)-pre_sum[n]+ST2.query(1,l2,r2));
}
printf("%d\n",temp);
}
else if(l2<=l1&&l1<=r1&&r1<=r2)///5
{
int temp=ST1.query(1,l1,r1).ans;
if(r1+1<=r2)
{
temp=max(temp,ST2.query(1,r1+1,r2)-pre_sum[n]+ST3.query(1,l1,r1));
}
printf("%d\n",temp);
}
}
}
return 0;
}

View file

@ -1,62 +0,0 @@
/*
C.
Description
便
Input
N1<=N<=1000N个正整数M1<=M<=1000
Output
Sample Input
5
14 12 2 18 15
6
4 9 0 16 16 12
Sample Output
4 83
4 70
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1010];
int main()
{
int n;
while(cin>>n)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int ans=INT_MAX;
int pos=0;
for(int i=1;i<=n;i++)
{
int cnt=0;
for(int j=1;j<=n;j++)
{
cnt+=a[j]*(abs(j-i));
}
if(cnt<ans)
{
ans=cnt;
pos=i;
}
}
cout<<pos<<' '<<ans<<endl;
}
return 0;
}

View file

@ -1,70 +0,0 @@
/*
G.
Description
n[-1000,1000] -1000~1000
:
5 5-7 7
Input
n n
Output
Sample Input
10
-562 232 969 201 -111 378 -610 127 245 932
10
868 -838 -958 200 867 -920 -493 114 -800 757
10
-607 -260 -270 -833 560 -280 404 -542 560 -115
Sample Output
2363
2828
1970
Hint
1 232+969+201-111+378-610+127+245+932=2363 2363
2 -838+-958+200+867+-920+-493+114+-800= -2828 2828
20% n<=10
50% n<=100
70% n<=1000
100% n<=1000000
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1000005;
const long long inf=(1LL<<60);
int n;
long long pre_sum[MAXN],pre_min[MAXN],pre_max[MAXN],ans,a[MAXN];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
pre_sum[i]=pre_sum[i-1]+a[i];
}
for(int i=1;i<=n;i++)
{
pre_max[i]=max(pre_max[i-1],pre_sum[i-1]);
pre_min[i]=min(pre_min[i-1],pre_sum[i-1]);
ans=max(ans,abs(pre_sum[i]-pre_min[i]));
ans=max(ans,abs(pre_sum[i]-pre_max[i]));
}
printf("%d\n",ans);
}

View file

@ -1,75 +0,0 @@
/*
Description
N个数字,
Input
T(1<=T<=20) .
T , N(1<=N<=100000), N个整数.int范围内
Output
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
14
7
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
int n;
long long a[MAXN],sum,ansum;
pair<int,int>ans;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%lld",&a[i]);
}
bool flag=false;
for(int i=1;i<=n;++i)
{
if(a[i]>=0)flag=true;
}
if(!flag)
{
printf("0\n");
continue;
}
int pos=0;
ansum=-1;
sum=0;
for(int i=1;i<=n;++i)
{
sum+=a[i];
if(sum<0)
{
sum=0;
pos=i;
}
else
{
if(sum>ansum)
{
ansum=sum;
ans=make_pair(pos+1,i);
}
else if(sum==ansum)
{
ans=min(ans,make_pair(pos+1,i));
}
}
}
printf("%lld\n",ansum);
}
return 0;
}

View file

@ -1,51 +0,0 @@
/*
C.
Description
n个整数a1,a2,anai,ai+1,aji<=i<=j<=n的子段和的最大值0From为1To为na1,a2,a3,a4,a5,a6=-2,11,-4,13,-5,-2a2+a3+a4=20i=2,j=41
Input
nn<=1000n
Output
Sample Input
6
-2 11 -4 13 -5 -2
Sample Output
From=2,To=4
MaxSum=20
*/
#include <stdio.h>
int a[1001],n,max,from,to,s;
int main()
{
int i,j,n;
while(1==scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
from=1;
to=n;
max=0;
for(i=1;i<=n;i++)
{
s=0;
for(j=i;j<=n;j++)
{
s+=a[j];
if(max<s)
{
max=s;
from=i;
to=j;
}
}
}
printf("From=%d,To=%d\n",from,to);
printf("MaxSum=%d\n",max);
}
return 0;
}

View file

@ -1,92 +0,0 @@
/*
D.
Description
n个整数a1a2a3an1<=i<=j<=n1<=n<=100000ai+ai+1+ai+2+aj的最大值n个整数都为负数
Input
nn个整数
Output
i和jSample Output
Sample Input
6
2 -5 11 -4 13 -2
Sample Output
From=3,To=5
MaxSum=20
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
int n;
long long a[MAXN],sum,ansum;
pair<int,int>ans;
const long long inf=(1LL)<<61;
void div_algorithm(int l,int r)
{
if(l>r)return;
int mid=(l+r)>>1;
long long lsum=0,rsum=0,lmax=-inf,rmax=-inf;
int lpos,rpos;
for(int i=mid;i>=l;--i)
{
lsum+=a[i];
if(lmax<lsum)
{
lmax=lsum;
lpos=i;
}
else if(lmax==lsum)
{
lpos=i;
}
}
for(int i=mid;i<=r;++i)
{
rsum+=a[i];
if(rmax<rsum)
{
rmax=rsum;
rpos=i;
}
}
if(ansum<lmax+rmax-a[mid])
{
ansum=lmax+rmax-a[mid];
ans=make_pair(lpos,rpos);
}
else if(ansum==lmax+rmax-a[mid])
{
ans=min(ans,make_pair(lpos,rpos));
}
div_algorithm(l,mid-1);
div_algorithm(mid+1,r);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;++i)
{
scanf("%lld",&a[i]);
}
bool flag=false;
for(int i=1;i<=n;++i)
{
if(a[i]>=0)flag=true;
}
if(!flag)
{
printf("From=0,To=0\nMaxSum=0\n");
continue;
}
ansum=-1;
sum=0;
div_algorithm(1,n);
printf("From=%d,To=%d\nMaxSum=%lld\n",ans.first,ans.second,ansum);
}
return 0;
}

View file

@ -1,54 +0,0 @@
/*
A.
Description
n个整数a1,a2,anai,ai+1,aj 1<=i<=j<=n的子段和的最大值0a1,a2,a3,a4,a5,a6=-2,11,-4,13,-5,-2a2+a3+a4=20i=2,j=41
n<=100n的范围规定修改为n<=200
Input
nn个整数
Output
From=xxx,To=xxxMaxSum=xxxx
Sample Input
6
-2 11 -4 13 -5 -2
Sample Output
From=2,To=4
MaxSum=20
*/
#include <stdio.h>
int a[1001],n,max,from,to,s;
int main()
{
int i,j,n;
while(1==scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
from=1;
to=n;
max=0;
for(i=1;i<=n;i++)
{
s=0;
for(j=i;j<=n;j++)
{
s+=a[j];
if(max<s)
{
max=s;
from=i;
to=j;
}
}
}
printf("From=%d,To=%d\n",from,to);
printf("MaxSum=%d\n",max);
}
return 0;
}

View file

@ -1,54 +0,0 @@
/*
B.
Description
n个整数a1,a2,anai,ai+1,aj i<=i<=j<=n的子段和的最大值00a1,a2,a3,a4,a5,a6=-2,11,-4,13,-5,-2a2+a3+a4=20i=2,j=41
n<=200n的范围规定修改为n<=2000
Input
nn个整数
Output
From=xxx,To=xxxMaxSum=xxxx
Sample Input
6
-2 11 -4 13 -5 -2
Sample Output
From=2,To=4
MaxSum=20
*/
#include <stdio.h>
int a[1001],n,max,from,to,s;
int main()
{
int i,j,n;
while(1==scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
from=1;
to=n;
max=0;
for(i=1;i<=n;i++)
{
s=0;
for(j=i;j<=n;j++)
{
s+=a[j];
if(max<s)
{
max=s;
from=i;
to=j;
}
}
}
printf("From=%d,To=%d\n",from,to);
printf("MaxSum=%d\n",max);
}
return 0;
}

View file

@ -1,28 +0,0 @@
#
'''
E. 穿错衣服问题
Description
SmallBeer曾经当过兵他常常回忆起刚入伍时的情景记得那时周末只有一天休息而且部队规定休息天外出营区只能有三分之一人数所以大家都很珍惜周末外出的机会一旦外出肯定要尽情享受一天的乐趣但部队规定下午五点必须归队因此大家外出后回来都很匆忙其中最有意思的是早上外出前大家都把军装洗完晾好下午归队时赶紧穿上但由于时间紧常常发生穿错衣服的事件现在请你这个编程高手想办法算算所有人都穿错的情况有多少种
Input
有多组测试数据对于每一组测试数据输入只有一行即总人数n(1<=n<=1000)
Output
对于每一组的输入输出只有一行即所有人都穿错的情况数
Sample Input
1
2
Sample Output
0
1
'''
a=[0,0,1]
for i in range(3,1010):
a.append((i-1)*(a[i-2]+a[i-1]))
while True:
try:
n=int(input())
print(a[n])
except:
break

View file

@ -1,59 +0,0 @@
/*
B.
Description
NIT篮球协会要在学校内选一批篮球宝贝
Input
n和mn表示学生总数m表示需要选拔的篮球宝贝的人数1<=n<=200001<=m<=50F和T表示F<=<=T150<=F<=T<=180n行是学生的数据S表示性别S=0S=1H表示身高G表示年级1<=G<=4
Output
m个满足要求的学生的输入顺序号No such student!
Sample Input
2
3 1
160 165
1 181 2
0 162 3
0 163 2
3 2
160 165
1 180 1
0 165 1
0 163 3
Sample Output
3
No such student!
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
scanf("%d",&t);
int a,b,c;
while(t--)
{
int n,m,l,r;
cin>>n>>m>>l>>r;
int ans=0;
for(int i=0;i<n;i++)
{
cin>>a>>b>>c;
if(m==0)continue;
if(a==0&&b<=r&&b>=l&&c<=2)
{
m--;
}
if(!m)
{
ans=i+1;
}
}
if(!m)cout<<ans<<endl;
else cout<<"No such student!"<<endl;
}
return 0;
}

View file

@ -1,31 +0,0 @@
#
'''
D. 自增自减运算
Description
小胖子正在学习汇编语言汇编语言里面有自增和自减指令叫inc和dec指令其实C语言里面也有自增和自减运算的++运算但这些指令或者运算都是有范围的现在小胖子碰到了很长很长的整数小胖子脑子一转很快就想出来现在请你也实现这个自增和自减运算
Input
输入有多组第一行是测试数据的组数对于每一组测试数据共有两行其中第一行是一个整数M0表示自减运算1表示自增运算第二行是一个很长的整数它的位数不超过10000位输入一定符合数学规定的要求并且不会有多余的字符
Output
输出只有一行
Sample Input
2
0
100
1
200
Sample Output
99
201
'''
t=int(input())
for i in range(t):
m=int(input())
a=int(input())
if m==0:
a-=1
else:
a+=1
print(a)

View file

@ -1,48 +0,0 @@
/*
A.
Description
NIT学校前不久刚结束了一次大一学生的英语测试
Input
n21<=n<=2000n个整数n个学生的英语成绩0<=<=100
Output
2
Sample Input
2
30
65 71 94 83 62 77 81 96 80 70 66 72 95 84 63 78 82 97 81 71 64 70 93 82 61 76 80 95 79 69
22
77 81 96 80 70 66 72 95 84 63 78 82 97 81 71 64 70 93 82 61 76 80
Sample Output
26
19.10
33
18.86
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
double a[2010];
int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
double sum=0;
for(int i=0;i<n;i++)
{
scanf("%lf",&a[i]);
sum+=a[i];
}
sort(a,a+n);
reverse(a,a+n);
printf("%.0f\n",a[0]-a[19]);
printf("%.2f\n",a[0]-sum/n);
}
return 0;
}