• + 0 comments

    In C language The solution is look like :

    include

    include

    include

    define MAX_TOWNS 50

    define MAX_OFFICES 50

    define MAX_PACKAGES 100

    define MAX_NAME_LENGTH 32

    define MAX_ID_LENGTH 16

    typedef struct { char id[MAX_ID_LENGTH]; int weight; } Package;

    typedef struct { int min_weight, max_weight; int num_packages; Package packages[MAX_PACKAGES]; } PostOffice;

    typedef struct { char name[MAX_NAME_LENGTH]; int num_offices; PostOffice offices[MAX_OFFICES]; } Town;

    /** * Find a town by name. */ Town *find_town_by_name(Town towns[], int n, char *name) { for (int i = 0; i < n; i++) { if (strcmp(towns[i].name, name) == 0) { return &towns[i]; } } return NULL; }

    /** * Print all packages in a given town. */ void print_town_packages(Town *town) { printf("%s:\n", town->name); for (int i = 0; i < town->num_offices; i++) { printf("\t%d:\n", i); for (int j = 0; j < town->offices[i].num_packages; j++) { printf("\t\t%s\n", town->offices[i].packages[j].id); } } }

    /** * Transfer packages from one post office to another. */ void transfer_packages(PostOffice *src, PostOffice *dest) { Package temp[MAX_PACKAGES]; int temp_count = 0, new_count = dest->num_packages;

    // Process all packages
    for (int i = 0; i < src->num_packages; i++) {
        if (src->packages[i].weight >= dest->min_weight && src->packages[i].weight <= dest->max_weight) {
            dest->packages[new_count++] = src->packages[i];
        } else {
            temp[temp_count++] = src->packages[i];
        }
    }
    
    // Restore rejected packages to the source office
    for (int i = 0; i < temp_count; i++) {
        src->packages[i] = temp[i];
    }
    src->num_packages = temp_count;
    dest->num_packages = new_count;
    

    }

    /** * Find the town with the most packages. */ void find_most_packages_town(Town towns[], int n) { int max_count = -1, max_index = -1;

    for (int i = 0; i < n; i++) {
        int total_packages = 0;
        for (int j = 0; j < towns[i].num_offices; j++) {
            total_packages += towns[i].offices[j].num_packages;
        }
    
        if (total_packages > max_count) {
            max_count = total_packages;
            max_index = i;
        }
    }
    
    if (max_index != -1) {
        printf("Town with the most number of packages is %s\n", towns[max_index].name);
    }
    

    }

    /** * Main function to read input and process queries. */ int main() { int n; if (scanf("%d", &n) != 1 || n < 1 || n > MAX_TOWNS) { fprintf(stderr, "Invalid number of towns.\n"); return 1; }

    Town towns[MAX_TOWNS];
    
    // Read town data
    for (int i = 0; i < n; i++) {
        if (scanf("%s", towns[i].name) != 1) return 1;
        if (scanf("%d", &towns[i].num_offices) != 1 || towns[i].num_offices < 1 || towns[i].num_offices > MAX_OFFICES) {
            return 1;
        }
    
        for (int j = 0; j < towns[i].num_offices; j++) {
            PostOffice *office = &towns[i].offices[j];
            if (scanf("%d %d %d", &office->num_packages, &office->min_weight, &office->max_weight) != 3) return 1;
    
            if (office->num_packages < 0 || office->num_packages > MAX_PACKAGES) {
                return 1;
            }
    
            for (int k = 0; k < office->num_packages; k++) {
                if (scanf("%s %d", office->packages[k].id, &office->packages[k].weight) != 2) return 1;
            }
        }
    }
    
    int q;
    if (scanf("%d", &q) != 1 || q < 1) {
        fprintf(stderr, "Invalid number of queries.\n");
        return 1;
    }
    
    // Process queries
    for (int i = 0; i < q; i++) {
        int query_type;
        if (scanf("%d", &query_type) != 1) return 1;
    
        if (query_type == 1) {
            char town_name[MAX_NAME_LENGTH];
            if (scanf("%s", town_name) != 1) return 1;
    
            Town *town = find_town_by_name(towns, n, town_name);
            if (town) print_town_packages(town);
    
        } else if (query_type == 2) {
            char town1[MAX_NAME_LENGTH], town2[MAX_NAME_LENGTH];
            int index1, index2;
            if (scanf("%s %d %s %d", town1, &index1, town2, &index2) != 4) return 1;
    
            Town *src_town = find_town_by_name(towns, n, town1);
            Town *dest_town = find_town_by_name(towns, n, town2);
    
            if (src_town && dest_town && index1 < src_town->num_offices && index2 < dest_town->num_offices) {
                transfer_packages(&src_town->offices[index1], &dest_town->offices[index2]);
            }
    
        } else if (query_type == 3) {
            find_most_packages_town(towns, n);
        }
    }
    
    return 0;
    

    }