T637969 转变
错误代码
#include<bits/stdc++.h>
using namespace std;
int n, m, a[105];
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
if(n <= m){
for(int i = 1; i <= n; i++){
cout << 0 << " ";
}
}
else{
for(int i = m + 1; i <= n; i++){
cout << a[i] << " ";
}
for(int i = 1; i <= n - m + 1; i++){
cout << 0 << " ";
}
}
return 0;
} 错误原因: 在
for(int i = 1; i <= n - m + 1; i++){
cout << 0 << " ";
}中, 因为。 所以,正确代码为:
#include<bits/stdc++.h>
using namespace std;
int n, m, a[105];
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
if(n <= m){//判断操作次数是否大于数组长度,如果大于等于就全输出零。
for(int i = 1; i <= n; i++){
cout << 0 << " ";
}
}
else{//如果不大于色输出从m + 1到n中数组的数,在输出m个零。
for(int i = m + 1; i <= n; i++){
cout << a[i] << " ";
}
for(int i = 1; i <= m; i++){
cout << 0 << " ";
}
}
return 0;
} T638156 落落的去维护数组
错误代码
#include<bits/stdc++.h>
using namespace std;
int n, m, a[200005], p, x, y;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
cin >> m;
for(int i = 1; i <= m; i++){
cin >> p;
if(p == 1){
cin >> x;
for(int j = 1; j <= n; j++){
a[j] = x;
}
}
else if(p == 2){
cin >> x >> y;
a[x] += y;
}
else{
cin >> x;
cout << a[x] << '\n';
}
}
return 0;
}错误原因:时空复杂度过高。
正确代码
#include<bits/stdc++.h>
using namespace std;
long long n, m, a[200005], ans[200005], p, x, y, t, k;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> ans[i];
}
cin >> m;
for(int i = 1; i <= m; i++){
cin >> p;
if(p == 1){
cin >> x;
t = i;//记录最后一次进行1操作的时间。
k = x;//记录最后一次1操作时的值。
}
else if(p == 2){
cin >> x >> y;
if(t == 0){//如果没有进行过一操作。
ans[x] += y;
}
else if(a[x] < t){//如果一操作比这个操作的时间晚。
ans[x] = y;
}
else if(a[x] > t){//如果一操作比这个操作的时间早。
ans[x] += y;
}
a[x] = i;
}
else{
cin >> x;
if(t == 0){//如果没有进行过一操作
cout << ans[x] << '\n';
}
else if(a[x] < t){//如果一操作比二操作的时间晚。
cout << k << '\n';
}
else if(a[x] > t){//如果一操作比二
操作的时间早。
cout << ans[x] + k << '\n';
}
}
}
return 0;
}总结: 对三个操作进行优化,大大减少了时空复杂度。
交叉区间
错误代码
#include<bits/stdc++.h>
using namespace std;
struct P{
int l, r;
}a[200005];
int n, ans, cnt;
bool cmp(const P &x, const P &y){
return x.l < y.l;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i].l >> a[i].r;
}
sort(a + 1, a + n + 1, cmp);
cnt = a[1].r;
for(int i = 2; i <= n; i++){
if(cnt > a[i].l && cnt < a[i].r){
ans += a[i].r - cnt;
cnt = a[i].r;
}
else if(cnt > a[i].r){
ans += a[i].r - a[i].l;
}
else{
cnt = a[i].r;
}
}
cout << ans;
return 0;
}错误原因:计算成了所有区间一共有多少个点重合。
正确代码
#include<bits/stdc++.h>
using namespace std;
struct P{
int l, r;
}a[500005];
long long n, sum, l[500005], r[500005];
bool cmp(const int &x, const int &y){
return x < y;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i].l >> a[i].r;
}
for(int i = 1; i <= n; i++){
l[i] = a[i].l;
r[i] = a[i].r;
}
sort(l + 1, l + n + 1);
sort(r + 1, r + n + 1);
for(int i = 1; i <= n; i++){
sum += n- (upper_bound(l + 1, l + n + 1, a[i].r) - l) + 1;//算出 a[i].r > a[i].l 时所有不相交的答案
sum += (lower_bound(r + 1, r + n + 1, a[i].l) - r) - 1;//算出 a[i].r < a[i].l 时所有不相交的答案
}
cout << (n * (n - 1) / 2 - sum / 2);//用所有的答案减不相交的答案得相交的答案。
return 0;
}总结: 以后读题要读仔细。