2021 UPC 第21场

2021 UPC 第21场


前言

a题得那个排序好像得用归并?还是太次了qaq


B 数字反转

给你一个数字,把他反过来输出,注意前导0
代码:`

#include<iostream>
#include <algorithm>
using namespace std;
#define ll long long
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
int main()
{
    int n,sum=0;
    cin >> n;
    while(n)
    {
        sum=sum*10+n%10;
        n/=10;
    }
    cout << sum << endl;
    return 0;
}

C 统计单词数

一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。
现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章中的某一独立单词在不区分大小写的情况下完全相同,如果给定单词仅是文章中某一单词的一部分则不算匹配。
输入
To
to be or not to be is a question
输出
2 0
思路:这个最好空格处理一下,防止find函数查的时候出错,比如:
to
to tototo to
之后,小写化处理一下,剩下的注意使用find函数就行了

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

const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
int main()
{
    string a,b;
    int n=0,ans=0;
    getline(cin,a);
	getline(cin,b);
    transform(a.begin(),a.end(),a.begin(),::tolower); //小写转换
    transform(b.begin(),b.end(),b.begin(),::tolower);
	b.insert(0," "); 
	a=a+' ';
	a.insert(a.begin(),' ');
	if(b.find(a)==-1)  //处理找不到得情况
    {
        cout << -1;
    }
    else
    {
        while(b.find(a,n)!=-1)  
        {
            ans++;
            n=n=b.find(a,n)+1;
        }
        cout << ans << " ";
        cout << b.find(a); //返回第一次的位置
    }



    return 0;
}

E,F水题

G Factors of Factorial

题意:让你算出n的阶乘的因子数,显然暴力是不可能的,100的阶乘就很大了,1000早炸了,所以这是个数论题,因字数等于分解质因数后每个质因数的对应的次数加一求和

#include<iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
long int f[10001];
void fun(int n)
{
    memset(f, 0, sizeof(f));
    for(int i=2;i<=n;i++)
    {
        int k=i;
        for(int j=2;j<=n;j++) 
        {
            while(k%j==0)   //分解质因数
            {
                f[j]++;    //记载对应次数
                k/=j;
            }
        }
        if(k) f[k]++;   //注意
    }

}
int main()
{
    long long ans=1;
    int n;
    
    cin >> n;
	fun(n);
    for(int i=2;i<=n;i++)
    {
        if(f[i])
        {
            ans=ans*(f[i]+1)%mod; 
        }
    }
    cout << ans;
    return 0;
}

L Walk and Teleport

There are N towns on a line running east-west. The towns are numbered 1 through N, in order from west to east. Each point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value. The coordinate of town i is Xi.
You are now at town 1, and you want to visit all the other towns. You have two ways to travel:
Walk on the line. Your fatigue level increases by A each time you travel a distance of 1, regardless of direction.
Teleport to any location of your choice. Your fatigue level increases by B, regardless of the distance covered.
Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.
简单dp,其实说贪心也行

#include<iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
long long int f[100005],a[100005] ;

int main()
{
   int n,t,b;
   f[1]=0;
   cin >> n >> t >> b;
   for(int i=1;i<=n;i++)
   {
   	  cin >> a[i];
   }
    for(int i=2;i<=n;i++)
    {
    	f[i]=min(f[i-1]+b,f[i-1]+(a[i]-a[i-1])*t);  //对于每个城镇选最小的
	}
   cout << f[n];
    return 0;
}

K: Mixing Milk II

时间限制: 1 Sec 内存限制: 128 MB

题目描述
Farming is competitive business – particularly milk production. Farmer John figures that if he doesn’t innovate in his milk production methods, his dairy business could get creamed!
Fortunately, Farmer John has a good idea. His three prize dairy cows Bessie, Elsie, and Mildred each produce milk with a slightly different taste, and he plans to mix these together to get the perfect blend of flavors.

To mix the three different milks, he takes three buckets containing milk from the three cows. The buckets may have different sizes, and may not be completely full. He then pours bucket 1 into bucket 2, then bucket 2 into bucket 3, then bucket 3 into bucket 1, then bucket 1 into bucket 2, and so on in a cyclic fashion, for a total of 100 pour operations (so the 100th pour would be from bucket 1 into bucket 2). When Farmer John pours from bucket a into bucket b, he pours as much milk as possible until either bucket a becomes empty or bucket b becomes full.

Please tell Farmer John how much milk will be in each bucket after he finishes all 100 pours.
输入
The first line of the input file contains two space-separated integers: the capacity c1 of the first bucket, and the amount of milk m1 in the first bucket. Both c1 and m1 are positive and at most 1 billion, with c1≥m1. The second and third lines are similar, containing capacities and milk amounts for the second and third buckets.
输出
Please print three lines of output, giving the final amount of milk in each bucket, after 100 pour operations.
样例输入 Copy
10 3
11 4
12 5
样例输出 Copy
0
10
2
提示
In this example, the milk in each bucket is as follows during the sequence of pours:
Initial State: 3 4 5

  1. Pour 1->2: 0 7 5
  2. Pour 2->3: 0 0 12
  3. Pour 3->1: 10 0 2
  4. Pour 1->2: 0 10 2
  5. Pour 2->3: 0 0 12
    (The last three states then repeat in a cycle …)
    这题真的很无聊,真就是模拟,倒来倒去的
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
long long a[3], b[3];
int main()
{
	for(int i=0;i<3;i++)
	{
		cin >> a[i] >> b[i];
	}
	for(int i=0;i<100;i++)
	{
		if(i%3==0)//0->1
		{
			int t=a[1]-b[1]; 
			if(b[0]<t)  //倒不满
			{
				b[1]+=b[0];
				b[0]=0;
			}
			else //倒满了
			{
				b[1]=a[1];
				b[0]-=t;
			}
		}
		else if(i%3==1) //1->2
		{
			int t=a[2]-b[2]; 
			if(b[1]<t)
			{
				b[2]=b[1]+b[2];
				b[1]=0;
			}
			else
			{
				b[2]=a[2];
				b[1]-=t;
			}
		}
		else //2->0
		{
			int t=a[0]-b[0];
			if(b[2]<t)
			{
				b[0]=b[2]+b[0];
				b[2]=0;
			}
			else
			{
				b[0]=a[0];
				b[2]-=t;
			}
		}
	}
	cout << b[0] << endl;
	cout << b[1] << endl;
	cout <<  b[2] << endl;
	return 0;
}

M The Bucket List

Farmer John is considering a change in how he allocates buckets for milking his cows. He thinks this will ultimately allow him to use a small number of total buckets, but he is not sure how many exactly. Please help him out!
Farmer John has N cows (1≤N≤100), conveniently numbered 1…N. The ith cow needs to be milked from time si to time ti, and requires bi buckets to be used during the milking process. Several cows might end up being milked at the same time; if so, they cannot use the same buckets. That is, a bucket assigned to cow i’s milking cannot be used for any other cow’s milking between time si and time ti. The bucket can be used for other cows outside this window of time, of course. To simplify his job, FJ has made sure that at any given moment in time, there is at most one cow whose milking is starting or ending (that is, the si’s and ti’s are all distinct).

FJ has a storage room containing buckets that are sequentially numbered with labels 1, 2, 3, and so on. In his current milking strategy, whenever some cow (say, cow i) starts milking (at time si), FJ runs to the storage room and collects the bi buckets with the smallest available labels and allocates these for milking cow i.

Please determine how many total buckets FJ would need to keep in his storage room in order to milk all the cows successfully.
输入
The first line of input contains N. The next N lines each describe one cow, containing the numbers si, ti, and bi, separated by spaces. Both si and ti are integers in the range 1…1000, and bi is an integer in the range 1…10.
输出
Output a single integer telling how many total buckets FJ needs.

样例输入 Copy
3
4 10 1
8 13 3
2 6 2
样例输出 Copy
4
思路: 要是真去模拟肯定也行,但显然不需要,这题就是想求出每时段用桶数最大值,其实是个差分,但我没那么写

#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
int lk[10000];
int main()
{
    int s,t,a,n,manx=0;
    cin >> n;
    while(n--)
    {
    	cin >> s >> t >> a;
    	for(int i=s;i<=t;i++)
    	{
    		lk[i]+=a;
		}
	}
	for(int i=1;i<=1000;i++)
	{
	  manx=max(manx,lk[i]) ;
	}
	cout <<manx;
	return 0;
}

M: Back and Forth

时间限制: 1 Sec 内存限制: 128 MB

题目描述
Farmer John has two milking barns, each of which has a large milk tank as well as a storage closet containing 10 buckets of various sizes. He likes to carry milk back and forth between the two barns as a means of exercise.
On Monday, Farmer John measures exactly 1000 gallons of milk in the tank of the first barn, and exactly 1000 gallons of milk in the tank of the second barn.

On Tuesday, he takes a bucket from the first barn, fills it, and carries the milk to the second barn, where he pours it into the storage tank. He leaves the bucket at the second barn.

On Wednesday, he takes a bucket from the second barn (possibly the one he left on Tuesday), fills it, and carries the milk to the first barn, where he pours it into the storage tank. He leaves the bucket at the first barn.

On Thursday, he takes a bucket from the first barn (possibly the one he left on Wednesday), fills it, and carries the milk to the second barn, where he pours it into the tank. He leaves the bucket at the second barn.

On Friday, he takes a bucket from the second barn (possibly the one he left on Tuesday or Thursday), fills it, and carries the milk to the first barn, where he pours it into the tank. He leaves the bucket at the first barn.

Farmer John then measures the milk in the tank of the first barn. How many possible different readings could he see?
输入
The first line of input contains 10 integers, giving the sizes of the buckets initially at the first barn. The second line of input contains 10 more integers, giving the sizes of the buckets initially at the second barn. All bucket sizes are in the range 1…100.
输出
Please print the number of possible readings Farmer John could get from measuring the milk in the tank of the first barn after Friday.
样例输入 Copy
1 1 1 1 1 1 1 1 1 2
5 5 5 5 5 5 5 5 5 5
样例输出 Copy
5
提示
In this example, there are 5 possible results for the final amount of milk in the first barn’s tank:

1000: FJ could carry the same bucket back and forth in each trip, leaving the total amount in the first barn’s tank unchanged.
1003: FJ could carry 2 units on Tuesday, then 5 units on Wednesday, then 1 unit on Thursday, and 1 unit on Friday.
1004: FJ could carry 1 unit on Tuesday, then 5 units on Wednesday, then 1 unit on Thursday, and 1 unit on Friday.
1007: FJ could carry 1 unit on Tuesday, then 5 units on Wednesday, then 2 units on Thursday, and 5 units on Friday.
1008: FJ could carry 1 unit on Tuesday, then 5 units on Wednesday, then 1 unit on Thursday, and 5 units on Friday.
思路:dfs应该可以搞,但我枚举的,因为数据小,我们可以分三种情况来想一个是一直就是一个桶,所以最后那个值没变,还是1000,第二种是前两次是一个桶后面两个不一样,第三个是四次通都不是一个,把结果存在数组中最后扫一遍就行了。

#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
int jk[10000];
int a[10], b[10];
int main()
{
    for(int i=0;i<10;++i)
    {
    	cin >> a[i];
	}
	for(int i=0;i<10;++i)
	{
		cin >> b[i];
	}
	jk[1000]=1;
	int ans=0;
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<10;j++)
		 {
			jk[1000-a[i]+b[j]]++;
		 } 
	}
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<10;j++)
		{
			for(int k=0;k<10;k++)
			{
				for(int l=0;l<10;l++)
				{
					if(i!=k&&j!=l) //注意不能一样
					{
						jk[1000-a[i]+b[j]-a[k]+b[l]]++;  
					}
				}
			}
		}
	}
	for(int i=0;i<=3000;i++)
	{
		if(jk[i])
		{
		ans++;
     	}
	}
	cout << ans ;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

\ 安 /

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

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

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

打赏作者

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

抵扣说明:

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

余额充值