POJ 2442 Sequence

Source: Internet
Author: User

SequenceTime Limit: 6000 MS Memory Limit: 65536 KTotal Submissions: 5658 Accepted: 1722 Description Given m sequences, each contains n non-negative integer. now we may select one number from each sequence to form a sequence with m integers. it's clear that we may get n ^ m this kind of sequences. then we can calculate the sum of numbers in each sequence, and get n ^ m values. what we need is the small Est n sums. cocould you help us? Input The first line is an integer T, which shows the number of test cases, and then T test cases follow. the first line of each case contains two integers m, n (0 <m <= 100, 0 <n <= 2000 ). the following m lines indicate the m sequence respectively. no integer in the sequence is greater than 10000. output For each test case, print a line with the smallest n sums in increasing order, which is separ Ated by a space. sample Input 12 31 2 32 2 3 Sample Output 3 3 4 Source POJ Monthly, Guang Lin I wrote a heap, but it was tle. In fact, after understanding the principle, you don't need to heap. Using heap is not the minimum value. Just find the minimum value [cpp] # include <stdio. h> # include <string. h> # include <stdlib. h> int a [2100], B [2100], heap [1000000], data [110] [2100], Data [2100]; int n, m, pos [10000]; int cmp (const void * e, const void * f) {return (* (int *) e-* (int *) f );} int main () {void deal (int k); int I, j, s, t; scanf ("% d", & t); while (t --) {scanf ("% d", & m, & n); for (I = 1; I <= m; I ++) {for (j = 1; j <= n; j ++) {scanf ("% d", & Data [J-1]);} qsort (Data, n, sizeof (Data [0]), cmp); for (j = 1; j <= n; j ++) {data [I] [j] = Data [J-1] ;}} for (I = 1; I <= n; I ++) {a [I-1] = data [1] [I];} for (I = 2; I <= m; I ++) {deal (I) ;}for (I = 0; I <= n-1; I ++) {if (I = 0) {printf ("% d ", a [I]);} else {printf ("% d", a [I]);} printf ("\ n");} return 0 ;} void deal (int k) {int I, j, min, key; for (I = 1; I <= n; I ++) {pos [I] = 0 ;} for (I = 0; I <= n-1; I ++) {B [I] = data [k] [I + 1] ;}for (I = 1; I <= n; I ++) {heap [I] = a [I-1] + B [0];} for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) {if (j = 1) {min = heap [j]; key = j ;}else {if (heap [j] <min) {min = heap [j]; key = j ;}}} a [I-1] = heap [key]; heap [key] = heap [key]-B [pos [key] + B [pos [key] + 1]; pos [key] + = 1 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.