F. 丽娃河的狼人传说
Time limit per test: 1.0 seconds
Time limit all tests: 1.0 seconds
Memory limit: 256 megabytes
Accept / Submit: 224 / 1759
丽娃河是华师大著名的风景线。但由于学校财政紧缺,丽娃河边的路灯年久失修,一到晚上就会出现走在河边要打着手电的情况,不仅非常不方便,而且影响安全:已经发生了大大小小的事故多起。
方便起见,丽娃河可以看成是从 1 到 n 的一条数轴。为了美观,路灯只能安装在整数点上,每个整数点只能安装一盏路灯。经专业勘测,有 m 个区间特别容易发生事故,所以至少要安装一定数量的路灯,
请问至少还要安装多少路灯。
Input
第一行一个整数 T (1≤T≤300),表示测试数据组数。
对于每组数据:
-
第一行三个整数 n,m,k (1≤n≤103,1≤m≤103,1≤k≤n)。
-
第二行 k 个不同的整数用空格隔开,表示这些位置一开始就有路灯。
-
接下来 m 行表示约束条件。第 i 行三个整数 li,ri,ti 表示:第 i 个区间 [li,ri] 至少要安装 ti 盏路灯 (1≤li≤ri≤n,1≤ti≤n)。
Output
对于每组数据,输出 Case x: y
。其中 x 表示测试数据编号(从 1 开始),y 表示至少要安装的路灯数目。如果无解,y 为 −1。
Examples
3 5 1 3 1 3 5 2 3 2 5 2 3 1 3 5 2 3 2 3 5 3 5 2 3 1 3 5 2 3 2 4 5 1
Case 1: 1 Case 2: 2 Case 3: 1
Note
因为今天不是满月,所以狼人没有出现。
思路:将区间的最右端从小到大进行排序,将路灯尽可能的放在最右边,这样如果区间有重叠就能最大限度的利用路灯
AC代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int loc[1010];
struct line
{
int start;
int end;
int num;
}lines[1010];
bool cmp(line a,line b)
{
return a.end < b.end;
}
int main()
{
int t;
scanf("%d",&t);
for (int x=1; x<=t; x++)
{
memset(loc,0,sizeof(loc));
int n,m,k,temp;
scanf("%d%d%d",&n,&m,&k);
for (int i=0; i<k; i++)
{
scanf("%d",&temp);
loc[temp] = 1;
}
bool flag = false;
for (int i=0; i<m; i++)
{
scanf("%d%d%d",&lines[i].start,&lines[i].end,&lines[i].num);
//要放的树比空间还多,则输出-1
if (lines[i].num > lines[i].end - lines[i].start + 1)
flag = true;
}
if (flag)
printf("Case %d: -1\n",x);
else
{
int total = 0;
sort(lines,lines+m,cmp);
for (int i=0; i<m; i++)
{
int sum = 0;
for (int j=lines[i].start; j<=lines[i].end; j++)
{
if (loc[j])
sum++;
}
int temp = lines[i].num - sum;
if (temp <= 0)
continue;
for (int j=lines[i].end; j>=lines[i].start; j--)
{
if (loc[j] == 0)
{
loc[j] = 1;
total++;
temp--;
}
if (temp == 0)
break;
}
}
printf("Case %d: %d\n",x,total);
}
}
return 0;
}
E. 黑心啤酒厂
Time limit per test: 1.0 seconds
Time limit all tests: 1.0 seconds
Memory limit: 256 megabytes
Accept / Submit: 1184 / 4093
黑心啤酒厂为了让大家买啤酒,会把一瓶酒设计成恰好能倒七杯。由于聚会时经常会有大家一起干杯这样的事情,干杯之前又要给每个人都倒满,所以来两个人的时候,干完三轮,恰好多一杯;三个人的时候,干完两轮,恰好多一杯;四个人的时候会多三杯。在上述情况下,为了践行不浪费的原则,就会多买一瓶啤酒,再干一轮。当然多买的啤酒可能又有多了……然后循环往复,喝了好多好多。直到啤酒刚刚好喝完为止。
现在啤酒厂把酒瓶设计成刚好能倒 x 杯,请依次求出有 2 人、3 人,一直到 n 人参加聚会时,啤酒厂各能卖出多少瓶啤酒。
Input
输入只有一行,两个整数 x,n (1≤x≤109,2≤n≤105)。
Output
输出 n−1 行,分别表示 2,3,…,n 人参加聚会时,能卖出的啤酒瓶数。
Examples
7 5
2 3 4 5
思路:求出杯数x和人数n的最小公倍数,用最小公倍数除以杯数就能得到瓶数,即x*n/gcd(x,n)为最小公倍数 ,除以x为瓶数,即n/gcd(x,n)
AC代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int gcd(int a,int b)
{
return b == 0 ? a : gcd(b,a%b);
}
int main()
{
int x,n,sum;
cin >> x >> n;
for (int i=2; i<=n; i++)
{
sum = i / gcd(x,i);
printf("%d\n",sum);
}
return 0;
}