C++ 当下面的代码出现错误时,如何优化它?

C++ 当下面的代码出现错误时,如何优化它?,c++,c++11,C++,C++11,维克兰特参加了他的大学联欢会,作为一个真正的美食家,他直接去了食品角,希望能得到一些美味佳肴。有N个 食物角的摊位编号从1到N,第i个摊位前站着Ai人。每个摊位需要一分钟为一个人服务。维克兰特将 最初,他排队等候第一个摊位,但作为一个不耐烦的家伙,如果他没有得到服务,他会在每分钟后继续排队等候下一个摊位。 (如果他排在最后一个摊位的队伍中,他将加入第一个摊位的队伍) 你得告诉那个卖维克兰特的小吃摊。 输出: 对于每个测试用例,在新行中打印将提供Vikrant的食品摊 限制条件: 1.≤ T≤ 1

维克兰特参加了他的大学联欢会,作为一个真正的美食家,他直接去了食品角,希望能得到一些美味佳肴。有N个 食物角的摊位编号从1到N,第i个摊位前站着Ai人。每个摊位需要一分钟为一个人服务。维克兰特将 最初,他排队等候第一个摊位,但作为一个不耐烦的家伙,如果他没有得到服务,他会在每分钟后继续排队等候下一个摊位。 (如果他排在最后一个摊位的队伍中,他将加入第一个摊位的队伍)

你得告诉那个卖维克兰特的小吃摊。 输出: 对于每个测试用例,在新行中打印将提供Vikrant的食品摊

限制条件: 1.≤ T≤ 100 1.≤ N≤ 105 0≤ 艾岛≤ 106

Examples:
Input:
2
4
1 4 2 1
2
4 4
Output:
3
1

Explanation:
Testcase 1: At T = 0, he stands at stall 1, it is occupied till T=1 so he moves so stall 2, where he waits for 1 minute after which he moves to stall 3 at T = 2,
 which is empty at that point since the 2 people waiting at that stall are already serves by T = 2, so he gets serves at this stall.
Testcase 2: The progression is explained by the states given below.
4 4 T=0
3 3 T=1
2 2 T=2
1 1 T=3
0 0 T=4
#包括
使用名称空间std;
int main()
{
int t;
cin>>t;
而(t--)
{
int n;
cin>>n;
int A[n];
对于(int i=0;i>A[i];
int T=0;
while(A[T]!=0)
{
对于(int i=0;i=n)
T=T%n;
}

cout基本上,您的算法是
O(Amax x N)
,其中
Amax
是最大的
A[.]

这可以通过减少每个
A
值来避免,仅当它被访问时,而不是一直被访问。我在后面提供了这个想法的实现。在最坏的情况下,新的复杂性是
O(Amax+N)

但是,代码中也存在逻辑错误。此行:

while(A[T] != 0)
应该是

while(A[T] > 0)
原因是该值可能变为负值,这意味着没有人不再在这里出席

令人困惑的是,这个错误有两个影响:有时答案不好,通常花费的时间更长

#include <iostream>
#include <vector>

int main() {
    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;
        std::vector<int> A(n);
        for (int i = 0; i < n ; ++i)
            std::cin >> A[i];
        int T = 0, i = 0;
        while (1) {
             if ((A[i] - T) <= 0) break;
             T++;
             i++;
             if (i == n) i = 0;
        }
        std::cout << i + 1 << "\n";
    }
    return 0;
}
#包括
#包括
int main(){
int t;
标准:cin>>t;
而(t--){
int n;
标准:cin>>n;
std::载体A(n);
对于(int i=0;i>A[i];
int T=0,i=0;
而(1){

如果((A[i]-T)我认为您需要告诉我们代码应该做什么以及运行它时会发生什么(错误消息/日志),建议阅读:“所以不知道输入值可以有多大”
Constraints:1≤ T≤ 100 1 ≤ N≤ 105 0 ≤ 艾岛≤ 106
你知道如何使用调试器吗?你知道如何使用调试器吗?@Megha98确实如此。如果它通过了测试,请告诉我们。
#include <iostream>
#include <vector>

int main() {
    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;
        std::vector<int> A(n);
        for (int i = 0; i < n ; ++i)
            std::cin >> A[i];
        int T = 0, i = 0;
        while (1) {
             if ((A[i] - T) <= 0) break;
             T++;
             i++;
             if (i == n) i = 0;
        }
        std::cout << i + 1 << "\n";
    }
    return 0;
}