Sales Report
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 1803 | Accepted: 539 |
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.
未知的贸易公司安装了一个新的库存跟踪系统,它存储了一个关于商品,世界买卖点的数据库。 每个销售点和每个商品使用一个整数作为唯一标识符(ID)。 每次销售 ,系统记录会商品 id , 销售商品的数量 , 和销售点的id 。
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.
未知的贸易公司安装了一个新的库存跟踪系统,它存储了一个关于商品,世界买卖点的数据库。 每个销售点和每个商品使用一个整数作为唯一标识符(ID)。 每次销售 ,系统记录会商品 id , 销售商品的数量 , 和销售点的id 。
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.
你的任务是输出一个总结报告,商品和销售点的总销量制成表格。
报告必须是一个二维表,第一行包含商品ID,以上升的顺序,第一列包括销售点的id,也是升序。
在第一行的第一列中的值必须−1。在格子中没有对应销量的填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.
输入包括记录数量N,接着N行,每行三个数据,qi si vi,qi是
商品d,si是销售点id,vi是商品卖出去的数量。
1 ≤ N ≤ 500000, 1 ≤ qi, si, vi ≤ 10
9
,表格的总数不会超过108 格子,每个格子的价值总和不会超过231−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
Hint
This problem has huge input and output data,use scanf() and printf() instead of cin and cout to read data to avoid time limit exceed.
Source
Northeastern Europe 2004, Far-Eastern Subregion
这道题已经写疯了,wa到没脾气。。。
我是首先对s排序辅排q,然后对q单独排序,之后就按照s的排序顺序输出,每次输出在那个单独排序的q里面找对应的输出点。。。
一个是我用的是堆排,然后经常从1开始跑,结果有特殊情况,N=1的时候要特殊判断,死了一天。。。
然后要用c++交,用c交会超时,死了一天。。。
我这里面q单独排序也是用的堆排,排序过程中没有处理,所以排序后还要把排好顺序的队列给处理成没有重复数字的队列,这里可以优化。。。
再说吧,已经死了。。。。。。
#include <stdio.h>
#define PARENT(i) (i >> 1)
#define LEFT(i) (i << 1)
#define RIGHT(i) (i << 1) + 1
struct Sale{
int q;
int s;
int v;
};
int N, heap_size;
void MAX_HEAPIFY(struct Sale *value_point, int i);
void BUILD_MAX_HEAP(struct Sale *value_point);
void HEAPSORT(struct Sale *value_point);
void MAX_HEAPIFY_q(int *q, int i);
void BUILD_MAX_HEAP_q(int *q);
void HEAPSORT_q(int *q);
int main(void){
struct Sale value[500111];
int q[500111];
int *p;
int r_boundary, pos, k, i, sum;
// freopen("in", "r", stdin);
scanf("%d", &N);
for (i = 1;i <= N;i ++){
scanf("%d%d%d", &value[i].q, &value[i].s, &value[i].v);
q[i] = value[i].q;
}
HEAPSORT(value);
HEAPSORT_q(q);
p = q+1;
// for (i = 1;i <= N;i ++){
// printf("value_point[%d], q = %d, s = %d, v = %d\n", i, value[i].q, value[i].s, value[i].v);
// }
for (i = 2;i <= N;i ++){
if(q[i]> q[i-1]){
p++;
*p = q[i];
}
}
// for (i =1;i <= N;i ++){
// printf("%d ", q[i]);
// }
printf("-1");
for (i = 1;i <= N;i ++){
if (q[i-1] >= q[i]){
printf("\n");
r_boundary = i - 1;
break;
}else{
printf(" %d", q[i]);
}
if (i == N){
r_boundary = N;
printf("\n");
}
}
// printf("r_boundary= %d\n", r_boundary);
printf("%d", value[1].s);
k = 1;sum = 0;
while(value[1].q != q[k]){printf(" 0");k++;}k++;
sum += value[1].v;
if (N == 1){
printf(" %d\n", sum);
}
for (i = 2;i <= N;i ++){
if (value[i-1].s != value[i].s){
printf(" %d", sum);
while(k <= r_boundary){printf(" 0");k++;}
printf("\n");
// if (i == N) break;
sum = 0;k = 1;
printf("%d", value[i].s);
while(value[i].q !=q[k] && k <= r_boundary){printf(" 0");k++;}k++;
}else{
if(value[i-1].q != value[i].q){
printf(" %d", sum);
sum = 0;
while(value[i].q !=q[k] && k <= r_boundary){printf(" 0");k++;}k++;
}
}
sum += value[i].v;
if (i == N){
printf(" %d", sum);
while(k < r_boundary + 1){printf(" 0");k++;}
printf("\n");
}
}
return 0;
}
void MAX_HEAPIFY(struct Sale *value_point, int i){
int l = LEFT(i);
int r = RIGHT(i);
int largest;
struct Sale temp;
if (l <= heap_size && (value_point+l)->s > (value_point+i)->s){
largest = l;
}else{
largest = i;
}
if (l <= heap_size && (value_point+l)->s == (value_point+i)->s){
if ((value_point+l)->q > (value_point+i)->q){
largest = l;
}else{
largest = i;
}
}
if (r <= heap_size && (value_point+r)->s > (value_point+largest)->s){
largest = r;
}
if (r <= heap_size && (value_point+r)->s == (value_point+largest)->s){
if ((value_point+r)->q > (value_point+largest)->q){
largest = r;
}
}
if (largest != i){
temp = value_point[i]; value_point[i] = value_point[largest];value_point[largest] = temp;
MAX_HEAPIFY(value_point, largest);
}
}
void BUILD_MAX_HEAP(struct Sale *value_point){
int i;
heap_size = N;
for (i = (heap_size >> 1); i >= 1;i --){
MAX_HEAPIFY(value_point, i);
}
}
void HEAPSORT(struct Sale *value_point){
struct Sale temp;
int i;
BUILD_MAX_HEAP(value_point);
for (i = heap_size;i >= 2;i --){
heap_size --;
temp = value_point[i];value_point[i] = value_point[1];value_point[1] = temp;
MAX_HEAPIFY(value_point, 1);
}
}
void MAX_HEAPIFY_q(int *q, int i){
int l = LEFT(i);
int r = RIGHT(i);
int largest, temp;
if (l <= heap_size && q[l] > q[i]){
largest = l;
}else{
largest = i;
}
if (r <= heap_size && q[r] > q[largest]){
largest = r;
}
if (largest != i){
temp = q[i];q[i] = q[largest];q[largest] = temp;
MAX_HEAPIFY_q(q, largest);
}
}
void BUILD_MAX_HEAP_q(int *q){
int i;
heap_size = N;
for (i = (heap_size >> 1); i >= 1;i --){
MAX_HEAPIFY_q(q, i);
}
}
void HEAPSORT_q(int *q){
int temp;
int i;
BUILD_MAX_HEAP_q(q);
for (i = heap_size;i >= 2;i --){
heap_size --;
temp = q[i];q[i] = q[1];q[1] = temp;
MAX_HEAPIFY_q(q, 1);
}
}

145

被折叠的 条评论
为什么被折叠?



