Cod sursa(job #1242977)

Utilizator vlady1997Vlad Bucur vlady1997 Data 15 octombrie 2014 13:14:12
Problema Walls Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.79 kb
        #include <cstdio>
        using namespace std;
        struct wall
        {
            int hig, poz1, poz2, lat;
        };
        struct hit
        {
            int hig_tr, poz_tr, zid, cel;
        };
        wall a[100001];
        hit b[100001];
        int main()
        {
            int n, m, i, j, k, x, y;
            freopen("walls.in","r",stdin);
            freopen("walls.out","w",stdout);
            scanf("%d",&n); a[1].poz1=1;
            for (i=1; i<=n; i++)
            {
                scanf("%d%d",&a[i].lat,&a[i].hig);
                if (i!=1) a[i].poz1=a[i-1].poz1+a[i-1].lat+1;
                a[i].poz2=a[i].poz1+a[i].lat-1;
            }
            scanf("%d",&m);
            for (i=1; i<=m; i++)
            {
                bool lovit=false, dt=false;
                scanf("%d%d",&x,&y);
                b[i].poz_tr=x;
                b[i].hig_tr=y;
                for (j=n; j>=1; j--)
                {
                    if (a[j].poz2<x && a[j].hig>=b[i].hig_tr)
                    {
                        bool ok=false;
                        b[i].zid=j; lovit=true;
                        for (k=i-1; k>=1; k--)
                        {
                            if (b[k].zid==b[i].zid && b[k].hig_tr==b[i].hig_tr)
                            {
                                ok=true;
                                b[i].cel=b[k].cel-1;
                                if (b[i].cel==1)
                                {
                                    dt=true;
                                    a[b[i].zid].hig=b[i].hig_tr-1;
                                }
                                break;
                            }
                        }
                        if (ok==false)
                        {
                            b[i].cel=a[b[i].zid].lat;
                            if (b[i].cel==1)
                            {
                                dt=true;
                                a[b[i].zid].hig=b[i].hig_tr-1;
                            }
                        }
                        break;
                    }
                }
                if (lovit==true)
                {
                    int z=a[b[i].zid].poz1+b[i].cel-1;
                    printf("HIT %d %d ",z,b[i].zid);
                    if (dt==true)
                    {
                        printf("YES\n");
                    }
                    else
                    {
                        printf("NO\n");
                    }
                }
                else if (lovit==false)
                {
                    printf("MISS\n");
                }
            }
            fclose(stdin);
            fclose(stdout);
            return 0;
        }