UVa 10050 Hartals(优先队列)

10050 - Hartals

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=991


A social research organization has determined a simple set of parameters to simulate the behavior of the political parties of our country. One of the parameters is a positive integer h (called the hartal parameter) that denotes the average number of days between two successive hartals (strikes) called by the corresponding party. Though the parameter is far too simple to be flawless, it can still be used to forecast the damages caused by hartals. The following example will give you a clear idea:


Consider three political parties. Assume h1 = 3, h2 = 4 and h3 = 8 where hi is thehartal parameter for party i ( i = 1, 2, 3). Now, we will simulate the behavior of these three parties for N = 14 days. One must always start the simulation on a Sunday and assume that there will be no hartals on weekly holidays (on Fridays and Saturdays).


 1234567891011121314
Days              
 SuMoTuWeThFrSaSuMoTuWeThFrSa
Party 1  x  x  x  x  
Party 2   x   x   x  
Party 3       x      
Hartals  12   34  5  


The simulation above shows that there will be exactly 5 hartals (on days 3, 4, 8, 9 and 12) in 14 days. There will be no hartal on day 6 since it is a Friday. Hence we lose 5 working days in 2 weeks.

In this problem, given the hartal parameters for several political parties and the value of N, your job is to determine the number of working days we lose in those Ndays.

Input

The first line of the input consists of a single integer T giving the number of test cases to follow.

The first line of each test case contains an integer N ( $7 \le N \le 3650$) giving the number of days over which the simulation must be run. The next line contains another integer P ( $1 \le P \le 100$) representing the number of political parties in this case. Thei­th of the next P lines contains a positive integer hi (which will never be a multiple of 7) giving the hartal parameter for party i ( $1 \le i \leP$).

Output

For each test case in the input output the number of working days we lose. Each output must be on a separate line.

Sample Input 

2
14
3
3
4
8
100
4
12
15
25
40

Sample Output 

5
15

此题ZOJ 2212 Argus的简单版

思路:
可以用优先队列,复杂度为

也可以用数组,复杂度为O(N)

完整代码:

优先队列的:
/*0.015s*/

#include<queue>
#include<cstdio>
using namespace std;

struct Instrut
{
	int now;
	int h;
	Instrut(int p): now(p), h(p) {}
};

bool operator <(const Instrut& x, const Instrut& y)
{
	return x.now > y.now;
}

int main()
{
	int t, day, p, h, count, tempnow, tempnow2, temp7;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &day);
		priority_queue<Instrut> que;
		scanf("%d", &p);
		for (int i = 0; i < p; i++)
		{
			scanf("%d", &h);
			Instrut temp(h);
			que.push(temp);
		}
		count = 0;
		Instrut temp = que.top();
		tempnow = temp.now;
		while (tempnow <= day)
		{
			que.pop();
			tempnow2 = que.top().now;
			temp7 = temp.now % 7;
			temp.now += temp.h;
			que.push(temp);
			if (tempnow == tempnow2)
			{
				temp = que.top();
				tempnow = temp.now;
				continue;
			}
			if ((temp7 != 6) && (temp7 != 0))
				count++;
			temp = que.top();
			tempnow = temp.now;
		}
		printf("%d\n", count);
	}
	return 0;
}

数组的:
/*0.015s*/

#include <cstdio>
#include <cstring>
using namespace std;

int t;
int n;
int p;
int vis[3652];

int main()
{
	scanf("%d", &t);
	while (t --)
	{
		memset(vis, 0, sizeof(vis));
		int sum = 0;
		scanf("%d%d", &n, &p);
		for (int i = 0; i < p; i ++)
		{
			int temp;
			scanf("%d", &temp);
			int d = temp;
			while (d <= n)
			{
				if (vis[d] == 0 && d % 7 && (d + 1) % 7)
				{
					sum ++;
					vis[d] = 1;
				}
				d += temp;
			}
		}
		printf("%d\n", sum);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值