# JLU第二次数据结构上机实验解题报告

6 篇文章 0 订阅

T1:数列查询

f(n) = f(n-1)*11/10，f[1]=10.

INPUT

OUTPUT

q行，每行1个整数，表示f(i)的值。查询的值都在32位整数范围内。

Sample

INPUT

3
1
2
3

OUTPUT

10
11
12

Time limit

10ms

Memory limit

1MB

Data range

1≤q≤10000

Solution

for(int i=2;i<=100000;i++){
f[i] = f[i - 1] * 11 / 10;
if(f[i] < 0)break;
printf("%d %d\n",i,f[i]);
}

177 119388179
178 131326996
179 144459695
180 158905664
181 174796230
182 192275853
183 211503438

TIME LIMIT: 10 ms

#include <bits/stdc++.h>

using namespace std;

int tot;
int f[100005];

int main(){
int q;
scanf("%d",&q);
f[1] = 10;
for(int i=2;i<=100000;i++)
f[i] = f[i - 1] * 11 / 10;
while(q--){
int x;scanf("%d",&x);
printf("%d\n",f[x]);
}
return 0;
}

	for(int i=1;i<=n;i++){
x += 2;
}
	for(int i=1;i<=n;i++){
x += 1;
x += 1;
}

printf/scanf 比 cin/cout 快

T2:稀疏矩阵之和

INPUT

OUTPUT

Sample

INPUT

10 10 3
2 2 2
5 5 5
10 10 20
10 10 2
2 2 1
6 6 6

OUTPUT

10 10 4
2 2 3
5 5 5
6 6 6
10 10 20

Time limit

100ms

Memory limit

10MB

Data range

10≤N、M≤50000，t≤min(N,M)

Solution

#include <bits/stdc++.h>

using namespace std;

struct Node{
int row,col;
long long val;
Node* rig;
};

Node* rhead[100005];

int n1,m1,t1;
int n2,m2,t2;
int sum;

int main(){
scanf("%d%d%d",&n1,&m1,&t1);
for(int i=1;i<=100000;i++){
rhead[i] = (Node*)malloc(sizeof(Node));
rhead[i] -> row = i;
rhead[i] -> col = -1;
rhead[i] -> val = 0;
rhead[i] -> rig = NULL;
}
for(int i=1;i<=t1;i++){
int r,c,val;
scanf("%d%d%d",&r,&c,&val);
Node* it = (Node*)malloc(sizeof(Node));
it -> row = r;
it -> col = c;
it -> val = val;
Node *now = rhead[r];
Node *las = NULL;
if(now -> rig == NULL)now -> rig = it,it -> rig = now,sum++;
else {
las = now;
now = now -> rig;
while(now -> col < c && now -> rig != rhead[r])las = now,now = now -> rig;
if(now -> col == c)now -> val += val;
else if(now -> rig == rhead[r] && now -> col < c)now -> rig = it,it -> rig = rhead[r],sum++;
else las -> rig = it,it -> rig = now,sum++;
}
}
scanf("%d%d%d",&n2,&m2,&t2);
if(n1 != n2 || m1 != m2)return printf("Illegal!"),0;
for(int i=1;i<=t2;i++){
int r,c,val;
scanf("%d%d%d",&r,&c,&val);
Node* it = (Node*)malloc(sizeof(Node));
it -> row = r;
it -> col = c;
it -> val = val;
Node *now = rhead[r];
Node *las = NULL;
if(now -> rig == NULL)now -> rig = it,it -> rig = now,sum++;
else {
las = now;
now = now -> rig;
while(now -> col < c && now -> rig != rhead[r])las = now,now = now -> rig;
if(now -> col == c){
now -> val += val;
if(now -> val == 0){
sum--;
las -> rig = now -> rig;
}
}
else if(now -> rig == rhead[r] && now -> col < c)now -> rig = it,it -> rig = rhead[r],sum++;
else las -> rig = it,it -> rig = now,sum++;
}
}
printf("%d %d %d\n",n1,m1,sum);
for(int i=1;i<=n1;i++){
if(rhead[i] -> rig == NULL)continue;
for(Node* j=rhead[i]->rig;j != rhead[i];j=j->rig){
if(j -> val == 0)continue;
printf("%d %d %lld\n",j -> row,j -> col,j -> val);
}
}
return 0;
}

T3:文字编辑

A i j表示把编号为i的汉字移动编号为j的汉字之前；

B i j表示把编号为i的汉字移动编号为j的汉字之后；

Q 0 i为询问编号为i的汉字之前的汉字的编号；

Q 1 i为询问编号为i的汉字之后的汉字的编号。

INPUT

OUTPUT

Sample

INPUT

1
9999 4
B 1 2
A 3 9999
Q 1 1
Q 0 3

OUTPUT

4
9998

Time limit

1000ms

Memory limit

2MB

Data range

1≤T≤9999，2≤n≤9999，1≤m≤9999

Solution

#include <bits/stdc++.h>

using namespace std;

int bef[100005];
int aft[100005];

int main(){
int T;scanf("%d",&T);
while(T--){
int n,m;scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
bef[i] = i - 1,aft[i] = i + 1;
bef[1] = n;
aft[n] = 1;
while(m--){
char ch[10];
scanf("%s",ch);
if(ch[0] == 'A'){
int x,y;scanf("%d%d",&x,&y);
aft[bef[x]] = aft[x];
bef[aft[x]] = bef[x];
bef[x] = bef[y];
aft[bef[y]] = x;
aft[x] = y;
bef[y] = x;
}
else if(ch[0] == 'B'){
int x,y;scanf("%d%d",&x,&y);
aft[bef[x]] = aft[x];
bef[aft[x]] = bef[x];//a y x b
aft[x] = aft[y];
bef[aft[y]] = x;
aft[y] = x;
bef[x] = y;
}
else {
int opt;scanf("%d",&opt);
int x;scanf("%d",&x);
if(opt == 1)printf("%d\n",aft[x]);
else printf("%d\n",bef[x]);
}
}
}
return 0;
}

T4:幸福指数

INPUT

OUTPUT

Sample

INPUT

7
6 4 5 1 4 5 6

OUTPUT

60
1 3

Time limit

100ms

Memory limit

64MB

Data range

1≤n≤100000 0≤Hi≤1000000

Solution

1.单调栈里的元素具有单调性

2.元素加入栈前，会将栈顶所有破坏单调性的元素都删除

3.利用单调栈可以找到元素向左遍历第一个比他小/大的元素

4.时间复杂度O(n)

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

#define int long long

int read(){
int x = 0,y = 1;
char ch = getchar();
while(ch > '9' || ch < '0')
if(ch == '-')y = -1,ch = getchar();
else ch = getchar();
while(ch >= '0' && ch <= '9')x = x * 10 + ch - '0',ch = getchar();
return x * y;
}

struct Node{
int high,wide;
int l,r;
}stac[100005];

Node NewNode(int x,int y,int ll,int rr){
Node v;
v.high = x,v.wide = y;
v.l = ll,v.r = rr;
return v;
}

int n,T;
int a[100005];
int top;
int ans,mxl,mxr;

signed main(){
scanf("%d",&n);
for(int i=1;i<=n+1;i++){
if(i != n + 1)a[i] = read();
else a[i] = 0;
if(a[i] >= stac[top].high)stac[++top] = NewNode(a[i],a[i],i,i);
else {
int wid = 0,l,r = stac[top].r;
while(a[i] < stac[top].high && top){
wid += stac[top].wide;
l = stac[top].l;
if(ans < stac[top].high * wid){
ans = stac[top].high * wid;
mxl = l,mxr = r;
} else if(ans == stac[top].high * wid){
if(mxr - mxl < r - l)mxl = l,mxr = r;
else if(mxr - mxl == r - l && l < mxl)mxl = l,mxr = r;
}
top--;
}
Node t = NewNode(a[i],wid + a[i],l,i);
if(ans < t.wide * t.high){
ans = t.wide * t.high;
mxl = l,mxr = i;
} else if(ans == t.wide * t.high){
if(mxr - mxl < i - l)mxl = l,mxr = i;
else if(mxr - mxl == i - l && l < mxl)mxl = l,mxr = i;
}
stac[++top] = t;
}
}
if(!mxl && !mxr){printf("0\n1 %d",n);}
else printf("%lld\n%lld %lld",ans,mxl,mxr);
return 0;
}

• 0
点赞
• 1
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

05-31 22
12-01 175
12-04 448
©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。