轮换对称式 @ 2019-09-14 16:54:09
为什么用while过不了,改成for就可以了?
这是AC的代码:
/*
date : 2019-09-14 15:49:24
problem ID :
problem WEB :
*/
#include <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
int n;
priority_queue<int , vector<int> > big;
priority_queue<int , vector<int> , greater<int> > small;
int main()
{
int now;
scanf("%d%d" , &n , &now);
big.push(now);
printf("%d\n" , now);
for (int i = 2 ; i <= n ; i++)
{
scanf("%d" , &now);
if (now >= big.top())
small.push(now);
else
big.push(now);
if (i % 2)
{
while (abs((int)big.size() - (int)small.size()) > 1)
{
if (big.size() > small.size())
{
small.push(big.top());
big.pop();
}
else
{
big.push(small.top());
small.pop();
}
}
if (big.size() > small.size())
printf("%d\n" , big.top());
else
printf("%d\n" , small.top());
}
}
return 0;
}
这是0分的代码:
/*
date : 2019-09-14 15:49:24
problem ID :
problem WEB :
*/
#include <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
int n;
priority_queue<int , vector<int> > big;
priority_queue<int , vector<int> , greater<int> > small;
int main()
{
int now;
scanf("%d%d" , &n , &now);
big.push(now);
n--;
printf("%d\n" , now);
while(n--)
{
scanf("%d" , &now);
if (now >= big.top())
small.push(now);
else
big.push(now);
if (n % 2 == 0)
{
while (abs((int)big.size() - (int)small.size()) > 1)
{
if (big.size() > small.size())
{
small.push(big.top());
big.pop();
}
else
{
big.push(small.top());
small.pop();
}
}
if (big.size() > small.size())
printf("%d\n" , big.top());
else
printf("%d\n" , small.top());
}
}
return 0;
}
by JasonZRY @ 2019-09-14 17:12:56
因为你的for是从2枚举到n,而while是从n-1(n是输入时的n)枚举到1
by muyang_233 @ 2019-09-14 17:15:25
显然您的for循环次数和while循环不一样
by JasonZRY @ 2019-09-14 17:17:35
@muyang_233 次数是一样的,因为他有n--;
by JasonZRY @ 2019-09-14 17:18:46
所以说你在这句话里奇偶性会变
AC代码:if (1 % 2)
0分代码:if (n % 2 == 0)
所以要改一改:
#include <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
int n;
priority_queue<int , vector<int> > big;
priority_queue<int , vector<int> , greater<int> > small;
int main()
{
int now;
scanf("%d%d" , &n , &now);
big.push(now);
n--;
printf("%d\n" , now);
while(n--)
{
scanf("%d" , &now);
if (now >= big.top())
small.push(now);
else
big.push(now);
if ((n + 1) % 2 == 0)
{
while (abs((int)big.size() - (int)small.size()) > 1)
{
if (big.size() > small.size())
{
small.push(big.top());
big.pop();
}
else
{
big.push(small.top());
small.pop();
}
}
if (big.size() > small.size())
printf("%d\n" , big.top());
else
printf("%d\n" , small.top());
}
}
return 0;
}
这就A了
by muyang_233 @ 2019-09-14 17:20:00
@JasonZRY ??n--是n次啊,2~n是n-1次啊
by JasonZRY @ 2019-09-14 17:23:34
@muyang_233
while是从n-1到1
一共n-1次
by muyang_233 @ 2019-09-14 17:28:35
@JasonZRY 奥对,我没看到,抱歉
by lady_GG @ 2019-09-14 20:17:03
@muyang_233 又是你
by 轮换对称式 @ 2019-09-15 12:11:45
谢谢各个神犇
@JasonZRY @muyang_233