神秘森林 O(2n)时间复杂度题解
2025-01-28 09:18:19
发布于:香港
2阅读
0回复
0点赞
只要一个输入循环 一个判断循环 所以时间复杂度为O(2n) 不会TLE
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n][2];
for(int i = 0; i <= n - 1; i++)
{
cin >> a[i][0] >> a[i][1];
}
for(int i = 0; i <= n - 1; i++)
{
if(a[i][0] > a[i][1])
cout << "-1" << endl;
else if(a[i][1] < 0)
cout << "-1" << endl;
else if(a[i][1] == 0)
cout << abs(a[i][0]) << endl;
else
cout << (a[i][1] - a[i][0]) + a[i][1] << endl;
}
}
这里空空如也
有帮助,赞一个