Sales Report
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 1658 | Accepted: 502 |
Description
The Unknown Trading Company have installed a new inventory-tracking system, which stores a complete database of goods and trading points worldwide. Each salespoint and each item was assigned an integer unique identifier (id). For every sale, the system logs id of the item, number of items sold, and id of the salespoint.
Your task is to output a summary report, tabulating total sales by items and salespoints. The report must be a two-dimensional table, with the first row containing item ids in increasing order, first column containing salespoint ids in increasing order, and values inside the table representing total sales of corresponding item from the corresponding salespoint. The value in first column of the first row must be −1. The values in cells without corresponding sales must be 0.
Your task is to output a summary report, tabulating total sales by items and salespoints. The report must be a two-dimensional table, with the first row containing item ids in increasing order, first column containing salespoint ids in increasing order, and values inside the table representing total sales of corresponding item from the corresponding salespoint. The value in first column of the first row must be −1. The values in cells without corresponding sales must be 0.
Input
Input contains number of records N, followed by N triplets of integers qi si vi, where qi -- item id, si -- salespoint id, vi -- number of items sold.
1 ≤ N ≤ 500000, 1 ≤ qi, si, vi ≤ 10 9, the summary table will have no more than 10 8 cells, the summary value in each cell will not exceed than 2 31−1.
1 ≤ N ≤ 500000, 1 ≤ qi, si, vi ≤ 10 9, the summary table will have no more than 10 8 cells, the summary value in each cell will not exceed than 2 31−1.
Output
Output must a table as described above, row-by-row.
Sample Input
4 10 1 3 20 2 5 10 2 2 20 2 1
Sample Output
-1 10 20 1 3 0 2 2 6
题意:一间全球具有很多分店的公司,每间分店有很多商品,乱序输入每间分店里的各种商品以及数量,以二维表的形式输出每间分店的各种商品的数量(如题目的样例输出)
思路:这道题卡了我很久...这是一道排序的题目...关键字有两个...经验少的我所以无从下手...想了很久....也重新过好多次...最后找到了解决方法...在排序中,的检验比较直接对两个关键字进行比较....其次,排好序后输出也是一个难题...因为不知道到底有多少种商品和多少间分店...这只好对排好序的数据再进行一次处理,最后根据处理后的数据进行输出。具体代码如下:
#include<iostream>
#define SIZE 500005
#define ID 0
#define SHOP 1
using namespace std;
int num[SIZE][3];
int id[SIZE],shop[SIZE];
void swap(int a, int b)
{
int tmp,i;
for(i = 0; i < 3; i++)
{
tmp = num[a][i];
num[a][i] = num[b][i];
num[b][i] = tmp;
}
}
int partition(int l, int r)
{
int i,j;
i = l - 1;
j = l;
while(j < r)
{
if(num[j][SHOP] < num[r][SHOP] || (num[j][SHOP] == num[r][SHOP] && num[j][ID] < num[r][ID]))
{
i++;
swap(i,j);
}
j++;
}
i++;
swap(i, r);
return i;
}
void qsort(int l, int r)
{
int mid;
if(l < r)
{
mid = partition(l,r);
qsort(l,mid -1);
qsort(mid + 1,r);
}
}
int main()
{
int n,i,j,z,tmp,x1,x2,k;
scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d%d%d",&num[i][0],&num[i][1],&num[i][2]);
//qsort
qsort(0, n - 1);
//deal
i = 0;
for(j = 1; j < n; j++)
{
if(num[i][0] == num[j][0] && num[i][1] == num[j][1])
num[i][2] += num[j][2];
else
{
i++;
swap(i,j);
}
}
x2 = 0;
x1 = 0;
shop[x2] = num[0][1];
id[x1] = num[0][0];
for(j = 1; j <= i; j++)
{
if(shop[x2] != num[j][1])
{
x2++;
shop[x2] = num[j][1];
}
x1++;
id[x1] = num[j][0];
}
for(j = 0; j <= i - 1; j++)
{
for(k = j + 1; k <= i; k++)
if(id[k] < id[j])
{
tmp = id[k];
id[k] = id[j];
id[j] = tmp;
}
}
x1 = -1;
tmp = 0;
while(tmp <= i)
{
while(tmp < i && id[tmp] == id[tmp + 1])
tmp++;
x1++;
id[x1] = id[tmp];
tmp++;
}
//output
printf("-1");
for(i = 0; i <= x1; i++)
{
cout << " " << id[i];
}
cout << endl;
z = 0;
for(i = 0; i <= x2; i++)
{
printf("%d",shop[i]);
for(j = 0; j <= x1; j++)
{
if(num[z][0] == id[j] && num[z][1] == shop[i])
{
printf(" %d",num[z][2]);
z++;
}
else
printf(" 0");
}
printf("\n");
}
return 0;
}