Per wrote:it has to be non-increasing, and its sum has to be even.
What I did for that,
checked the case where the sum is not even.
sort the values in non-increasing order using O(n^2) alg.
Then checked the Erdos and Gallai alg.
and nothing more.
"Everything should be made simple, but not always simpler"
Thanks schindlersp. Got AC in 0:00.219s. I had used all the steps u said except vImp which I checked after addition of all the inputs. That is the only reason I think to get such a huge runtime.
"Everything should be made simple, but not always simpler"
Riyad wrote:
[cpp]
while(scanf("%d",&n)==1 && n){
flod = false ;//flod no need
FOR(i,1,n){scanf("%d",&d);
if(d < 0 || d>= n){flod=true ;}//no need
sum+=d;}
if(flod){printf("Not possible\n");continue ;}//no need
if(sum==0){printf("Possible\n");continue ;}//no need
if(n==1 && sum > 0){printf("Not possible\n");continue ;}
if(sum&1){printf("Not possible\n");continue ;}
qsort(d,n,sizeof(d[0]),compare);// I think it will be d+1
//then check for Erdos and Gallai algo. (you may miss here)
}
[/cpp]
check these.
"Everything should be made simple, but not always simpler"
thanx anupam bhai for u help , i got it ac . . i made the mistake in calling the qsort function .
[cpp]
while(scanf("%d",&n)==1 && n){
flod = false ;//flod no need
FOR(i,1,n){scanf("%d",&d);
if(d < 0 || d>= n){flod=true ;}//no need
sum+=d;}
if(flod){printf("Not possible\n");continue ;}//no need
if(sum==0){printf("Possible\n");continue ;}//no need
if(n==1 && sum > 0){printf("Not possible\n");continue ;}
if(sum&1){printf("Not possible\n");continue ;}
qsort(d,n,sizeof(d[0]),compare);// i made the silly mistake here, it will be d+1
//then check for Erdos and Gallai algo. (No Prob)
}[/cpp]
Regards
Riyad
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN
Per wrote:
If by greedy you mean actually constructing the graph, how fast do you handle the case
Code:
10000 9999 9999 [... and so on ...] 9999
? If not, what do you mean?
My algorithm is something close to O(n^2) and theoritically it should not pass the time limit. but what I did was :
- first sort the degrees in descending order.
- then I greedily assign, for each node, values to the next consecutive nodes.
- and then sort the remaining nodes in nlog(n) time.
that means my algo takes O(n^2log(n) ) time.
I guess the judge data was shallow and there was no data to the limit.
No wonder why everyone is not thinking of the obvious.
well i took tons of WA
and the reason was that i misunderstood the theroem and understood the sum in the right is dum of min(d,i])
i changed that and i still got WA i took the thing with fewer steps and put the program to work till n-1 and finally after 1h.30 i got ac in 0.200 you should be carefull do what you are told no smarstuff except countsort and that's all
why do i always get wa?
i've tried all the test data above and got the same answer..
[c]#include <stdio.h>
#include <stdlib.h>
#define NN 10000
int comp(const void *a, const void *b) {
return *(int *)b - *(int *)a;
}
int main(void) {
int not, j, tmp, big;
int i, n, sum, v[NN];
while (scanf("%d", &n)==1) {
if (!n) break;
for (i=sum=not=0;i<n;i++) {
scanf("%d", &v);
if (v<0||v>n)
not = 1;
sum+=v;
}
/* the sum should always be even */
if ((sum&1)||(n==1&&sum)||(not)) {
puts("Not possible");
continue;
}
/* if sum = zero, it surely is a graph */
if (!sum) {
puts("Possible");
continue;
}
/* sort it */
qsort (v, n, sizeof(int), comp);
/* Erdos and Gallai's algorithm */
for (i=tmp=0;i<n-1;i++) {
tmp+=v;
if (v>i) big = (n-i-1)*(i+1);
else big = sum-tmp;
if (tmp>i*(i+1)+big) {
not = 1;
break;
}
}
if (not) puts("Not possible");
else puts("Possible");
}
return 0;
} [/c]
In this problem we are talking about simple graph which does not have same endpoints for more than one edge, and also does not have edges with the same endpoint.