UVa1260 - Sales

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 <cstdio>

using namespace std;

const int N = 1010;

int a[N], b[N];
int n;

void input();
void solve();

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("d:\\OJ\\uva_in.txt", "r", stdin);
	#endif
	
	int 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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值