UVa1260 - Sales

来源:互联网 发布:java enum valueof 编辑:程序博客网 时间:2024/05/17 07:02

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 letB = (b1, b2,...,bn-1) be another integer list maintained by Mr. Cooper, each value representing the number of such previous days. On thei-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 sincea1$ \le$a4,a2$ \le$a4, anda3 > a4. Therefore,b3 = 2. Similarly, b1, b2, andb4 can be obtained and it results inB = (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 then - 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 integern (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 amountsai (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 then - 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 

2538 111 102 111 1778276 284 103 439 452 276 452 398

Sample Output 

920
#include <cstdio>using namespace std;const int N = 1010;int a[N], b[N];int n;void input();void solve();int main(){#ifndef ONLINE_JUDGEfreopen("d:\\OJ\\uva_in.txt", "r", stdin);#endifint t;scanf("%d", &t);while (t--) {input();solve();}return 0;}void input(){scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}}void solve(){for (int i = 1; i < n; i++) {int cnt = 0;for (int j = 0; j < i; j++) {if (a[j] <= a[i]) cnt++;}b[i - 1] = cnt;}int ans = 0;for (int i = 0; i < n - 1; i++) {ans += b[i];}printf("%d\n", ans);}



0 0