闲情偶记1

目录

Q1:

Q2:

Q3

Q4:

Q5:

 

Q1:

             You may have heard the classical story about Goldilocks and the 3 bears. Little known, however, is that Goldilocks ultimately took up farming as a profession. On her farm, she has a barn containing N cows (1 <= N <= 20,000). Unfortunately, her cows are rather sensitive to temperature.
           Each cow i specifies a range of temperatures A(i)..B(i) that are "just right" (0 <= A(i) <= B(i) <= 1,000,000,000). If Goldilocks sets the thermostat in the barn to a temperature T < A(i), the cow will be too cold, and will produce X units of milk . If she sets the thermostat to a temperature T within this range (A(i) <= T <= B(i)), then the cow will feel comfortable and produce Y units of milk. If she sets the thermostat to a temperature T > B(i), the cow will feel too hot,
and will produce Z units of milk. As one would expect, the value of Y is always larger than both X and Z.
          Given X, Y, and Z, as well as the preferred range of temperatures for each cow, please compute the maximum amount of milk Goldilocks can obtain if she sets the barn thermostat optimally. The values of X, Y, and Z are integers in the range 0..1000, and the thermostat can be set to any integer value.
 
Partial credit opportunities: Out of the 10 test cases for this problem,
cases 1..4 will have B(i) <= 100 for every cow, and in cases 1..6, N is at most 1000.
 
INPUT FORMAT:
* Line 1: Four space-separated integers: N X Y Z.
* Lines 2..1+N: Line 1+i contains two space-separated integers: A(i) and B(i).
 
SAMPLE INPUT:
4 7 9 6
5 8
3 4
13 20 2
7 10
 
INPUT DETAILS:
There are 4 cows in the barn, with temperature ranges 5..8, 3..4, 13..20, and 7..10. A cold cow produces 7 units of milk, a comfortable cow produces 9 units of milk, and a hot cow produces 6 units of milk.
 
OUTPUT FORMAT:
* Line 1: The maximum amount of milk Goldilocks can obtain by an optimal temperature setting in her barn.
 
SAMPLE OUTPUT:
31
 
OUTPUT DETAILS:
If Goldilocks sets the thermostat to either 7 or 8, then she will make cows #1 and #4 happy, with cow #2 being too hot and cow #3 being too cold. This yields 31 units of total milk.
 
首先这题我先想到的是二分,二分那个合适的温度,但其实这是一种错觉,因为你会发现二分完以后,不知道该怎么check,而且结果并没有明显的单调性。
后来又想到了差分,但差分做的话,区间加来加去,像我这种小菜鸟很容易就绕里面了。
所以正解其实是一种数学手法吧。
 
把每组数据的上下限都分别记在a数组和b数组中,并sort快排一下。
 
然后我们选取ai作为温度(我的下标从1开始)
a0 a1 a2......................ai   ai+1 ........ai+n   一共有n-i个比ai高的温度,也就是说我们选取的温度T比n-i个元素小,根据题目,那么就要增加x*(n-i)
 
 如果bj<ai  (b数组是上限值,我们找到第一个小于我们所选温度ai的位置)
 
b1 b2 b3......bj.......bj+n       那么从b1到bj这段都是比T要小的,根据题目要求,增加z*j
 
一共有n头奶牛,那么剩下的就是n-(n-i)-j=i-j 这些是要都增加y的
 
#include <bits/stdc++.h> 
using namespace std;
 
int main()
{
	int n,x,y,z;
	int a[20005],b[20005];
	int ans=0;
	
	//freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
	
	cin>>n>>x>>y>>z;
	
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	
	sort(a+1,a+n+1);
	sort(b+1,b+n+1);
	
	for(int i=n,j=n;i>=1;i--)
	{
		while(j>=1&&b[j]>=a[i]) j--;
		ans=max(ans,(n-i)*x+j*z+(i-j)*y);
	}
	
	cout<<ans<<endl;
	return 0;
}

 

Q2:

Farmer John has N cows (2 <= N <= 15) that are each one of three different breeds: Holsteins, Jerseys, or Guernseys.
 
Unfortunately, FJ cannot remember the exact breeds of his cows! He does, however, remember a list of K (1 <= K <= 50) relationships between pairs of cows; for example, he might remember that cows 1 and 2 have the same breed, or that cows 1 and 5 have different breeds.
 
Give FJ's list of relationships between pairs of cows, please help him compute the number of different possible assignments of breeds
to his cows (this number could be zero if his list contains contradictory information).
 
INPUT FORMAT:
* Line 1: Two space-separated integers: N and K.
* Lines 2..1+K: Each line describes the relationship between a pair of cows x and y (1 <= x,y <= N, x != y). It is either of the form "S x y", meaning that x and y have the same breed, or "D x y", meaning that x and y have different breeds. 
 
SAMPLE INPUT:
4 2
S 1 2
D 1 3
 
INPUT DETAILS:
There are 4 cows. Cows 1 and 2 have the same breed, and cows 1 and 3 have different breeds.
 
OUTPUT FORMAT:
* Line 1: The number of possible breed assignments.
 
SAMPLE OUTPUT:
18
 
OUTPUT DETAILS:
The following six breed assignments are possible for the first 3 cows: HHG, HHJ, GGH, GGJ, JJH, JJG. For each of these, we can have 3 possible breed assignments for the 4th cow, giving a total of 18 different assignments consistent with FJ's list.

 

这题其实就是一个简单的小搜索

有边相连就是1,否则就是-1 

建立好关系,搜索就是了

#include <bits/stdc++.h>
using namespace std;

int a[10000];
int ga[20][60];
int n,k,ans;

inline void solve(int cur)
{
	if(cur>n)
	{
		ans++;
		return;
	}
	for(int i=1;i<=3;i++)
	{
		int f=0;
		for(int j=1;j<cur;j++)
		{
			if(ga[cur][j]==1&&i!=a[j])
			{
				f=1;
				break;
			}
			if(ga[cur][j]==-1&&i==a[j])
			{
				f=1;
				break;
			}
		}
		if(!f)
		{
			a[cur]=i;
			solve(cur+1);
			a[cur]=0;
		}
	}
}
int main()
{
	//freopen("b.in","r",stdin);
	//freopen("b.out","w",stdout);
	cin>>n>>k;
	
	int x,y;
	while(k--)
	{
		char c;
		cin>>c>>x>>y;
		if(c=='S') ga[x][y]=ga[y][x]=1;
		 else ga[x][y]=ga[y][x]=-1;
	}
	
	solve(1);
	cout<<ans<<endl;
	return 0;
	
}

 

Q3

https://www.luogu.com.cn/problem/P3078

#include <bits/stdc++.h>
using namespace std;

int a[10000];
int ga[20][60];
int n,k,ans;

inline void solve(int cur)
{
	if(cur>n)
	{
		ans++;
		return;
	}
	for(int i=1;i<=3;i++)
	{
		int f=0;
		for(int j=1;j<cur;j++)
		{
			if(ga[cur][j]==1&&i!=a[j])
			{
				f=1;
				break;
			}
			if(ga[cur][j]==-1&&i==a[j])
			{
				f=1;
				break;
			}
		}
		if(!f)
		{
			a[cur]=i;
			solve(cur+1);
			a[cur]=0;
		}
	}
}
int main()
{
	//freopen("b.in","r",stdin);
	//freopen("b.out","w",stdout);
	cin>>n>>k;
	
	int x,y;
	while(k--)
	{
		char c;
		cin>>c>>x>>y;
		if(c=='S') ga[x][y]=ga[y][x]=1;
		 else ga[x][y]=ga[y][x]=-1;
	}
	
	solve(1);
	cout<<ans<<endl;
	return 0;
	
}

其实说真的,这道题有没有大佬给我详细解释一下!在下感激不尽!!

Q4:

数学作业 (math.cpp)
【问题描述】
小明的数学老师布置了一堆数学题作为作业,这些数学题有一个共同的特点,就是都是
要求计算出 C(N,M) 中不同质因子的个数。 Steven 请你帮他写一个程序,来帮助他尽快地
完成这些作业。
C(N,M) 即求在 N 个数中选 M 个数的组合数。
 
【输入格式】 (math.in)
输入文件中只有一行,其中有两个整数,表示 N M(1<=N, M<=50000)
 
【输出格式】 (math.out)
输出一个整数,表示 C(N,M) 中质因子的个数。
 
【输入样例】
  7 3 // C(7,3)=P(7,3)/3!=(7*6*5)/3!=7*5
【输出样例】
  2 // 两个不同的质因子: 7, 5
 
 
这题不是简单的模拟,它的数值很大,如果一个个乘的话,很容易爆掉。所以就要用到一个套路,去模拟约分的一个过程。
分母打钩,分子如果能约的话,分母在--就ok了。例如分母是25=5*5,那b[5]+=2, 然后再看分子,发现分子是5,b[5]--
 
#include <bits/stdc++.h>
using namespace std;

int a[50001],b[50001];
int n,m;
int tot;

int main()
{
	ios::sync_with_stdio(0);
	//freopen("math.in","r",stdin);
	//freopen("math.out","w",stdout);
	cin>>n>>m;
	tot=0;
	
	for(int i=2;i<=n;i++)//1-n中的素数 
	{
		int f=0;
		for(int j=2;j*j<=i;j++)
		 if(i%j==0)
		 {
		 	f=1;
		 	break;
		 }
		if(f==0)
		{
			tot++;
			a[tot]=i;
		}
	}
	
	memset(b,0,sizeof(b));
	for(int i=n;i>=n-m+1;i--)
	{
		int temp=i;
		int cnt=0;
		while(temp!=1)
		{
			cnt++;
			while(temp%a[cnt]==0)
			{
			   temp/=a[cnt];
			   b[cnt]++;	
			}
		}
	}
	
	for(int i=m;i>=1;i--)
	{
		int temp=i;
		int cnt=0;
		while(temp!=1)
		{
			cnt++;
			while(temp%a[cnt]==0)
			{
			   temp/=a[cnt];
			   b[cnt]--;	
			}
		}
	}
	
	tot=0;
	for(int i=1;i<=n;i++)
	 if(b[i]) tot++;
	
	cout<<tot<<endl;
	return 0;
} 

 

Q5:

奶牛晒衣服 (dry.cpp)
【问题描述】
在熊大妈的英明带领下,时针和它的同伴生下了许多牛宝宝。熊大妈决定给每个宝宝都
穿上可爱的婴儿装。于是,为牛宝宝洗晒衣服就成了很不爽的事情了。
圣人王担负起了这个重任。在洗完衣服后,你就要弄干衣服。衣服在自然条件下,用 1
的时间就可以晒干 A 点湿度。熊大妈买了一台烘干机,它可以让你用 1 的时间,使一件衣
服除了自然晒干的 A 点湿度外,还可烘干 B 点湿度,但在 1 的时间内,只能对一件衣服进
行烘干。
现有湿度不一样的 N 件衣服。现在,告诉你每件衣服的湿度,要求你求出弄干所有衣服
所需的最少时间(湿度 0 为干)。
 
【输入格式】(dry.in)
第一行有三个整数,即 N A B
接下来有 N 行,每行中有一个数,表示每件衣服的湿度 (1<= 湿度 , A, B<=500 000, 1<=N<=500 000)。
 
【输出格式】(dry.out)
输出所需的最少时间。
【输入样例】
3 2 1
1
2
3
【输出样例】
1
【样例说明】
1 个时间内,用机器处理第 3 件衣服。此外,所有衣服自然晒干 2 。花费 1 时间全
部弄干
 
 

Solution1:二分

题目要求最小时间,那么我们就二分时间

#include <bits/stdc++.h>
using namespace std;

int n,a,b,s,natu,hot;
int c[500005];

bool check(int t)
{
  s=0;
  for(int i=1;i<=n;i++)
  {
  	 natu=c[i]-a*t;//自然风干的湿度 
  	 if(natu<=0) continue;//湿度小于0就干了 
  	
	hot=natu/b;//余下衣服要烘干的时间 
	if(natu%b!=0) hot++;
	s+=hot;
	if(s>t) return false;
  }	
  if(s<=t) return true;
   else return false;
}
int main()
{
	//freopen("dry.in","r",stdin);
	//freopen("dry.out","w",stdout);
	cin>>n>>a>>b;
	
	for(int i=1;i<=n;i++) cin>>c[i];
	
	int l=0,r=c[n],mid;
	while(l<=r)
	{
		mid=(l+r)/2;
		if(check(mid))
		 r=mid-1;
		else l=mid+1;
	}
	
	cout<<l;
	return 0;
}

Solution 2:堆heap

#include <bits/stdc++.h>
using namespace std;

int main()
{
	//freopen("dry.in","r",stdin);
	//freopen("dry.out","w",stdout);
	priority_queue<int> heap;
	int n,a,b;
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++)
	{
		int u;
		cin>>u;
		heap.push(u);
	}
	
	int k=0;
	for(;heap.top()>k*a;k++)
	{
		int u=heap.top();
		heap.pop();
		u-=b;
		heap.push(u);
	}
	
	cout<<k<<endl;
	return 0;
}

 

Q6、Q7、Q8、Q9......................................Qn

[TO BE CONTINUED ~]

 


 

 
 
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值