Saturday, June 7, 2014

codeforces Testing Round #10, problem: (A) Forgotten Episode solution

codeforces Testing Round #10, problem: (A) Forgotten Episode: http://codeforces.com/problemset/problem/440/A

codeforces Testing Round #10, problem: (A) Forgotten Episode solution: http://ideone.com/R5D3Tw

#include <iostream>
#include <cstdio>

using namespace std;

int a[100003]={0};

int main() {
    int n, b;
    a[0]=1;
    scanf("%d", &n);
    for(int i=1; i<n; i++) scanf("%d", &b), a[b]++;
    for(int i=0; i<=n; i++) if(a[i]==0) printf("%d", i);
    return 0;
}


version 2:

it gave me WA:
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int n, a;
    long long int sum=0, total;
    scanf("%d", &n);
    for(int i=1; i<n; i++) scanf("%d", &a), sum+=a;
    total=(n*(n+1))/2;
    total-=sum;
    printf("%I64d", total);
    return 0;
}


while change from int to long long int gave AC: http://ideone.com/9l0AVw
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    long long int sum=0, total, n, a;
    scanf("%I64d", &n);
    for(int i=1; i<n; i++) scanf("%I64d", &a), sum+=a;
    total=(n*(n+1))/2;
    total-=sum;
    printf("%I64d", total);
    return 0;
}

note on version 2: just use total of all numbers from 1 to n: http://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF

version 3: http://ideone.com/6Moq07
#include <iostream>
using namespace std;

int main() {
    int a, n, bin_sum=0;
    cin>>n;
    for(int i=1; i<n; i++) cin>>a, bin_sum^=a, bin_sum^=i;
    bin_sum^=n;
    cout<<bin_sum;
    return 0;
}


note on version 3: i used xor. http://codeforces.com/blog/entry/12534. but it gave WA when i used scanf() and it gave AC when i used cin>>.

No comments:

Post a Comment