Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Spring 2015 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week08/f/BalancingBankAccounts.rtf

{\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf540
{\fonttbl\f0\fnil\fcharset0 Consolas;\f1\fmodern\fcharset0 CourierNewPSMT;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural

\f0\fs26 \cf0 /* UVA 538 - Balancing Bank Accounts\
   Randall Head */\
\
\

\f1 #include <stdio.h>\
#include <string.h>\
\
int main() \{\
    int n, m, i, j, c, cases = 0;\
    char name[20][100];\
    char a[100], b[100];\
    while(scanf("%d %d", &n, &m) == 2) \{\
        if(n == 0 && m == 0)\
            break;\
        for(i = 0; i < n; i++)\
            scanf("%s", name[i]);\
        int cnt[20] = \{\};\
        while(m--) \{\
            scanf("%s %s %d", a, b, &c);\
            for(i = 0; i < n; i++)\
                if(!strcmp(a, name[i]))\
                    break;\
            cnt[i] += c;\
            for(i = 0; i < n; i++)\
                if(!strcmp(b, name[i]))\
                    break;\
            cnt[i] -= c;\
        \}\
        printf("Case #%d\\n", ++cases);\
        while(1) \{\
            int maxx = 0, minx = 0xffffff;\
            int idx1, idx2;\
            for(i = 0; i < n; i++) \{\
                if(cnt[i] > maxx)\
                    maxx = cnt[i], idx1 = i;\
                if(cnt[i] < minx)\
                    minx = cnt[i], idx2 = i;\
            \}\
            if(maxx == 0)   break;\
            if(maxx >= -minx) \{\
                printf("%s %s %d\\n", name[idx2], name[idx1], -minx);\
                int tmp = -minx;\
                cnt[idx1] -= tmp;\
                cnt[idx2] += tmp;\
            \} else \{\
                printf("%s %s %d\\n", name[idx2], name[idx1], maxx);\
                int tmp = maxx;\
                cnt[idx1] -= tmp;\
                cnt[idx2] += tmp;\
            \}\
        \}\
        puts("");\
    \}\
    return 0;\
\}\
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015