24h購物| | PChome| 登入
2012-03-16 22:08:49| 人氣1,909| 回應0 | 上一篇 | 下一篇

[UVA] 1260 - Sales

推薦 0 收藏 0 轉貼0 訂閱站台

Mr. Cooper, the CEO of CozyWalk Co., receives a report of daily sales every day since the company has been established. Starting from the second day since its establishment, on receiving the report, he compares it with each of the previous reports in order to calculate the number of previous days whose sales amounts are less than or equal to it. After obtaining the number of such days, he writes it in a list.

This problem can be stated more formally as follows. Let A = (a1, a2,..., an) denote the list of daily sales amounts. And let B = (b1, b2,..., bn-1) be another integer list maintained by Mr. Cooper, each value representing the number of such previous days. On the i-th day ( 2$ le$i$ le$n), he calculates bi-1, the number of ak's such that ak$ le$ai ( 1$ le$k < i).

For example, suppose that A = (20, 43, 57, 43, 20). For the fourth day's sales amount, a4 = 43, the number of previous days whose sales amounts are less than or equal to it is 2 since a1$ le$a4, a2$ le$a4, and a3 > a4. Therefore, b3 = 2. Similarly, b1, b2, and b4 can be obtained and it results in B = (1, 2, 2, 1).

Given an array of size n for the list of daily sales amounts, write a program that prints the sum of the n - 1 integers in the list B.

Input 

Your program is to read the input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing an integer n ( 2$ le$n$ le$1, 000), which represents the size of the list A . In the following line, n integers are given, each represents the daily sales amounts ai ( 1$ le$ai$ le$5, 000 and 1$ le$i$ le$n) for the test case.

Output 

Your program is to write to standard output. For each test case, print the sum of the n - 1 integers in the list B which is obtained from the list A.

The following shows sample input and output for two test cases.

Sample Input 

2
5
38 111 102 111 177
8
276 284 103 439 452 276 452 398

Sample Output 

9
20

模擬, 使用插入排序法

#include <stdio.h>
#include <stdlib.h>
int main() {
int T, n, A[1001];
int i, j;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
int ans = 0, tmp;
for(i = 0; i < n; i++) {
scanf("%d", &tmp);
for(j = i-1; j >= 0; j--)
if(tmp < A[j])
A[j+1] = A[j];
else
break;
A[j+1] = tmp;
ans += j+1;
}
printf("%d\n", ans);
}
return 0;
}

台長: Morris
人氣(1,909) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 846 - Steps
此分類上一篇:[UVA][D&C] 10006 - Carmichael Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文