20240526训练题目

A题

题目

A factory receives n n n orders at the beginning of day 1 1 1. The i i i-th order can be described as two integers a i a_i ai and b i b_i bi, indicating that at the end of day a i a_i ai, the factory needs to deliver b i b_i bi products to the customer.
Given that the factory can produce k k k products each day, and at the beginning of day 1 1 1 the factory has no product in stock, can the factory complete all orders?

Input

There are multiple test cases. The first line of the input contains an integer T T T ( 1 ≤ T ≤ 100 1 \le T \le 100 1T100) indicating the number of test cases. For each test case:

The first line contains two integers n n n and k k k ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100, 1 ≤ k ≤ 1 0 9 1 \le k \le 10^9 1k109) indicating the number of orders and the number of products the factory can produce each day.

For the following n n n lines, the i i i-th line contains two integers a i a_i ai and b i b_i bi ( 1 ≤ a i , b i ≤ 1 0 9 1 \le a_i, b_i \le 10^9 1ai,bi109) indicating that the i i i-th order require the factory to deliver b i b_i bi products at the end of day a i a_i ai.

Output

For each test case output one line. If the factory can complete all orders output Yes, otherwise output No.

解题思路

先排序,按照时间前后,依次交货

AC代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct op{
	int a,b;
};
bool cmp(op x,op y)
{
	return x.a<y.a;
}
void solve()
{
	int t=0,n,k,ff=0;
	op q[110];
	q[0].a=0;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>q[i].a>>q[i].b;
	}
	sort(q+1,q+n+1,cmp);//自定排序
	for(int i=1;i<=n;i++)
	{
		t+=(q[i].a-q[i-1].a)*k;
		t-=q[i].b;
		if(t<0)
		{
			ff=1;
			break;
		}
	}
	if(ff==0)	cout<<"Yes"<<'\n';
	else	cout<<"No"<<'\n';
}
signed main()
{
	int t;
	cin>>t;
	while(t--)	solve();
	return 0;
 } 

B题

题目描述

You’re the boss of a building company. At the beginning, there are g g g types of employees in the company, and different types of employees have different occupations. For the i i i-th type of employees, their occupation can be numbered as t i t_i ti and there are u i u_i ui employees in total.

There are n n n building projects in the market waiting to be undertaken. To undertake the i i i-th project, your company must meet m i m_i mi requirements. The j j j-th requirement requires that your company has at least b i , j b_{i, j} bi,j employees whose occupation is a i , j a_{i, j} ai,j. After undertaking the project, your company will become more famous and will attract k i k_i ki types of employees to join your company. The occupation of the j j j-th type of employees is c i , j c_{i, j} ci,j and there are d i , j d_{i, j} di,j employees in total.

You can undertake any number of projects in any order. Each project can be undertaken at most once. Calculate the maximum number of projects you can undertake.

Note that employees are not consumables. After undertaking a project the number of employees in your company won’t decrease.

Input

There is only one test case in each test file.

The first line of the input first contains an integer g g g ( 1 ≤ g ≤ 1 0 5 1 \le g \le 10^5 1g105) indicating the number of types of employees in the company at the beginning. Then g g g pairs of integers t 1 , u 1 , t 2 , u 2 , ⋯ t g , u g t_1, u_1, t_2, u_2, \cdots t_g, u_g t1,u1,t2,u2,tg,ug follow ( 1 ≤ t i , u i ≤ 1 0 9 1 \le t_i, u_i \le 10^9 1ti,ui109), where t i t_i ti and u i u_i ui indicate that there are u i u_i ui employees whose occupation is t i t_i ti. It’s guaranteed that for all KaTeX parse error: Expected 'EOF', got '&' at position 9: 1 \le i &̲lt; j \le g we have t i ≠ t j t_i \ne t_j ti=tj.

The second line contains an integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105) indicating the number of projects waiting to be undertaken.

For the following 2 n 2n 2n lines, each two lines describe a project.

The ( 2 i − 1 ) (2i - 1) (2i1)-th line first contains an integer m i m_i mi ( 0 ≤ m i ≤ 1 0 5 0 \le m_i \le 10^5 0mi105) indicating the number of requirements to undertake the i i i-th project. Then m i m_i mi pairs of integers a i , 1 , b i , 1 , a i , 2 , b i , 2 , ⋯   , a i , m i , b i , m i a_{i, 1}, b_{i, 1}, a_{i, 2}, b_{i, 2}, \cdots, a_{i, m_i}, b_{i, m_i} ai,1,bi,1,ai,2,bi,2,,ai,mi,bi,mi follow ( 1 ≤ a i , j , b i , j ≤ 1 0 9 1 \le a_{i, j}, b_{i, j} \le 10^9 1ai,j,bi,j109) where a i , j a_{i, j} ai,j and b i , j b_{i, j} bi,j indicate that the company is required to have at least b i , j b_{i, j} bi,j employees whose occupation is a i , j a_{i, j} ai,j. It’s guaranteed that for all KaTeX parse error: Expected 'EOF', got '&' at position 9: 1 \le x &̲lt; y \le m_i we have a i , x ≠ a i , y a_{i, x} \ne a_{i, y} ai,x=ai,y.

The 2 i 2i 2i-th line first contains an integer k i k_i ki ( 0 ≤ k i ≤ 1 0 5 0 \le k_i \le 10^5 0ki105) indicating the number of types of employees to join the company after undertaking the i i i-th project. Then k i k_i ki pairs of integers c i , 1 , d i , 1 , c i , 2 , d i , 2 , ⋯   , c i , k i , d i , k i c_{i, 1}, d_{i, 1}, c_{i, 2}, d_{i, 2}, \cdots, c_{i, k_i}, d_{i, k_i} ci,1,di,1,ci,2,di,2,,ci,ki,di,ki follow ( 1 ≤ c i , j , d i , j ≤ 1 0 9 1 \le c_{i, j}, d_{i, j} \le 10^9 1ci,j,di,j109) where c i , j c_{i, j} ci,j and d i , j d_{i, j} di,j indicate that there are d i , j d_{i, j} di,j employees whose occupation is c i , j c_{i, j} ci,j joining the company. It’s guaranteed that for all KaTeX parse error: Expected 'EOF', got '&' at position 9: 1 \le x &̲lt; y \le k_i we have c i , x ≠ c i , y c_{i, x} \ne c_{i, y} ci,x=ci,y.

It’s guaranteed that neither the sum of m i m_i mi nor the sum of k i k_i ki will exceed 1 0 5 10^5 105.

Output

Output one line containing one integer indicating the maximum number of projects you can undertake.

AC代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
#define ll long long
map<ll,set<pair<ll,ll>>>mp;
map<ll,ll>now;
ll goal[N],res[N];
vector<pair<ll,ll>> add[N];
void Solve()
{
	ll n,m;
	cin >> n;
	for(int i = 1; i <= n; i ++ )
	{
		ll x,y;
		cin >> x >> y;
		now[x] += y;
	}
	cin >> m;
	ll ans = 0;
	ll s;
	for(int i = 1; i <= m; i ++ )
	{
		cin >> s;
		goal[i] = s;
		for(int j = 1; j <= s; j ++ )
		{
			ll x,y;
			cin >> x >> y;
			mp[x].insert({y,i});
		}
		cin >> s;
		if(!goal[i]) ans ++;
		for(int j = 1; j <= s; j ++ )
		{
			ll x,y;
			cin >> x >> y;
			if(!goal[i]) now[x] += y;
			else add[i].push_back({x,y});
		}
	}
	vector<ll>check;
	for(auto &it : mp)
	{
		vector<pair<ll,ll>>re;
		for(auto [x,y] : it.second)
		{
			if(x <= now[it.first])
			{
				re.push_back({x,y});
				res[y] ++;
				if(res[y] == goal[y])
				{
					ans ++;
					for(auto [xx,yy] : add[y])
					{
						if(xx < it.first) check.push_back(xx);
						now[xx] += yy;
					}
				}
			}else break;
		}
		for(auto itt : re)
		{
			it.second.erase(itt);
		}
	}
	while(check.size())
	{
		vector<ll>tmp;
		for(auto it : check)
		{
			vector<pair<ll,ll>>re;
			for(auto [x,y] : mp[it])
			{
				if(x<=now[it])
				{
					re.push_back({x,y});
					res[y] ++;
					if(res[y]==goal[y])
					{
						ans++;
						for(auto [xx,yy]:add[y])
						{
							tmp.push_back(xx);
							now[xx]+=yy;
						}
					}
				}else break;
			}
			for(auto itt:re){
				mp[it].erase(itt);
			}
		}
		check = tmp;
	}
	cout << ans << '\n';
}
 
int main()
{
	int t = 1;
	while(t--) Solve();
	return 0;
}

D题

题目描述

You’re participating in a team competition of trail running. There are n n n members in your team where v i v_i vi is the speed of the i i i-th member and w i w_i wi is his/her weight.

The competition allows each team member to move alone or carry another team member on their back. When member i i i carries member j j j, if member i i i’s weight is greater than or equal to member j j j’s weight, member i i i’s speed remains unchanged at v i v_i vi. However, if member i i i’s weight is less than member j j j’s weight, member i i i’s speed will decrease by the difference of their weight and becomes v i − ( w j − w i ) v_i - (w_j - w_i) vi(wjwi). If member i i i’s speed will become negative, then member i i i is not able to carry member j j j. Each member can only carry at most one other member. If a member is being carried, he/she cannot carry another member at the same time.

For all members not being carried, the speed of the slowest member is the speed of the whole team. Find the maximum possible speed of the whole team.

Input

There are multiple test cases. The first line of the input contains an integer T T T indicating the number of test cases. For each test case:

The first line contains an integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105) indicating the number of team members.

For the following n n n lines, the i i i-th line contains two integers v i v_i vi and w i w_i wi ( 1 ≤ v i , w i ≤ 1 0 9 1 \le v_i, w_i \le 10^9 1vi,wi109) indicating the speed and weight of the i i i-th member.

It’s guaranteed that the sum of n n n of all test cases will not exceed 1 0 5 10^5 105.

Output

For each test case output one line containing one integer indicating the maximum speed of the whole team.

AC代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
const int INF = 1e9;
int T,n;
struct Node
{
    int v,w;
}A[N],B[N];
bool check(int x)
{
    vector <int> p,q;
    for(int i=1;i<=n;i++) if(A[i].v >= x) p.push_back(A[i].v+A[i].w-x);
    for(int i=1;i<=n;i++) if(B[i].v < x) q.push_back(B[i].w);
    if(p.size() < q.size()) return false;
    for(int i=0;i<q.size();i++)
    {
        if(p[i] < q[i]) return false;
    }
    return true;
}
bool cmp(Node a,Node b) 
{
    return a.w > b.w;
}
bool cmp1(Node a,Node b)
{
    return (a.v+a.w) > (b.v+b.w);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>T;
    while(T--)
    {   
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>A[i].v>>A[i].w;
            B[i].v = A[i].v;
            B[i].w = A[i].w;
        }
        sort(A+1,A+n+1,cmp1);
        sort(B+1,B+n+1,cmp);
        int l = 0,r = INF;
        while(l < r)
        {
            int mid = (l+r+1) / 2;
            if(check(mid)) l = mid;
            else r = mid-1;
        }
        cout<<l<<endl;
    }
    return 0;
}

G题

题目描述

Given an integer sequence a 1 , a 2 , ⋯   , a n a_1, a_2, \cdots, a_n a1,a2,,an of length n n n, we construct an undirected graph G G G from the sequence. More precisely, for all KaTeX parse error: Expected 'EOF', got '&' at position 9: 1 \le i &̲lt; j \le n, if i − j = a i − a j i - j = a_i - a_j ij=aiaj, there will be an undirected edge in G G G connecting vertices i i i and j j j. The weight of the edge is ( a i + a j ) (a_i + a_j) (ai+aj).

Find a matching of G G G so that the sum of weight of all edges in the matching is maximized, and output this maximized sum.

Recall that a matching of an undirected graph means that we choose some edges from the graph such that any two edges have no common vertices. Specifically, not choosing any edge is also a matching.

Input

There are multiple test cases. The first line of the input contains an integer T T T indicating the number of test cases. For each test case:

The first line contains an integer n n n ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2n105) indicating the length of the sequence.

The second line contains n n n integers a 1 , a 2 , ⋯   , a n a_1, a_2, \cdots, a_n a1,a2,,an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 109ai109) indicating the sequence.

It’s guaranteed that the sum of n n n of all test cases will not exceed 5 × 1 0 5 5 \times 10^5 5×105.

Output

For each test case output one line containing one integer indicating the maximum sum of weight of all edges in a matching.

解题思路

先排序,再依次转化成答案

AC代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
struct op
{
	int x,xx;
}a[N];
int cmp(op p,op q)
{
	if(p.xx==q.xx) return p.x>q.x;
	return p.xx>q.xx;
}
void solve()
{
	int sum=0;
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].x;
		a[i].xx=a[i].x-i;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n-1;){
		if(a[i].xx==a[i+1].xx){
			if(a[i].x+a[i+1].x>0){
				sum=sum+a[i].x+a[i+1].x;	
			}
			i+=2;
		}
		else if(a[i].xx!=a[i+1].xx){
			i++;
		}
	}
	cout<<sum<<endl;
}
signed main()
{
	int t;cin>>t;
	while(t--){
		solve();
	}
	return 0;
} 

I题

题目描述

Dice are small, throwable objects with marked sides capable of landing in multiple positions. They are
typically used to generate random values, especially in the context of tabletop games.
The most common dice are small cubes, with faces numbered from 1 to 6. Number n (1 ≤ n ≤ 6) is usually represented by a pattern of n round dots, known as pips. Moreover, the pips on the 1 and 4 faces are colored red ( , ), while those on the 2, 3, 5 and 6 faces are black ( , , , ).
Little Cyan Fish has three dice. One day, he threw them onto a table, and then observed the uppermost faces. He claimed that the total number of the red pips facing up was exactly A, and the total number of the black pips facing up was exactly B.
However, you find his claim doubtful. You want to verify whether it is possible to throw three dice such that the total number of red pips facing up is A, and the total number of black pips facing up is B.

Input

There is only one test case in each test file.
The first line of the input contains two integers A and B (0 ≤ A, B ≤ 100), indicating the total number of red pips facing up and the number of black pips facing up.

Output

Output one line. If it is possible for Little Cyan Fish to throw three dice such that the total number of red pips facing up is A, and the total number of black pips facing up is B output Yes. Otherwise ## AC代码

//签到题,枚举即可
#include<bits/stdc++.h>
using namespace std;
int h[4]={2,3,5,6};
void solve()
{
	int a,b;
	cin>>a>>b;
	if(a==3||a==6||a==9||a==12)
	{
		if(b==0)	cout<<"Yes";
		else cout<<"No";
	}
	else if(a==4||a==1)
	{
		int gg=0;
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				if(h[i]+h[j]==b)
				{
					gg=1;
					cout<<"Yes";
					break;
				}
			}
			if(gg==1)	break;
		}
		if(gg==0)	cout<<"No";
	}
	else if(a==5||a==8||a==2)
	{
		if(b==2||b==3||b==5||b==6)
		{
			cout<<"Yes";
		}
		else
		{
			cout<<"No";
		}
	}
	else if(a==0)
	{
		int ff=0;
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				for(int z=0;z<4;z++)
				{
					if(h[i]+h[j]+h[z]==b)
					{
						cout<<"Yes";
						ff=1;
						break;
					}
				}
				if(ff==1)	break;
			}
			if(ff==1)	break;
		}
		if(ff==0)	 cout<<"No";
	}
	else cout<<"No";
}
int main()
{
	int t;t=1;
	while(t--)	solve();
	return 0;
}

L题

题目描述

Given a grid with n n n rows and n n n columns, there is exactly one black cell in the grid and all other cells are white. Let ( i , j ) (i, j) (i,j) be the cell on the i i i-th row and the j j j-th column, this black cell is located at ( b i , b j ) (b_i, b_j) (bi,bj).

You need to cover all white cells with some L-shapes, so that each white cell is covered by exactly one L-shape and the only black cell is not covered by any L-shape. L-shapes must not exceed the boundary of the grid.

More formally, an L-shape in the grid is uniquely determined by four integers ( r , c , h , w ) (r, c, h, w) (r,c,h,w), where ( r , c ) (r, c) (r,c) determines the turning point of the L-shape, and h h h and w w w determine the direction and lengths of the two arms of the L-shape. The four integers must satisfy 1 ≤ r , c ≤ n 1 \le r, c \le n 1r,cn, 1 ≤ r + h ≤ n 1 \le r + h \le n 1r+hn, 1 ≤ c + w ≤ n 1 \le c + w \le n 1c+wn, h ≠ 0 h \ne 0 h=0, w ≠ 0 w \ne 0 w=0.

  • If KaTeX parse error: Expected 'EOF', got '&' at position 3: h &̲lt; 0, then all cells ( i , c ) (i, c) (i,c) satisfying r + h ≤ i ≤ r r + h \le i \le r r+hir belong to this L-shape; Otherwise if KaTeX parse error: Expected 'EOF', got '&' at position 3: h &̲gt; 0, all cells ( i , c ) (i, c) (i,c) satisfying r ≤ i ≤ r + h r \le i \le r + h rir+h belong to this L-shape.
  • If KaTeX parse error: Expected 'EOF', got '&' at position 3: w &̲lt; 0, then all cells ( r , j ) (r, j) (r,j) satisfying c + w ≤ j ≤ c c + w \le j \le c c+wjc belong to this L-shape; Otherwise if KaTeX parse error: Expected 'EOF', got '&' at position 3: w &̲gt; 0, all cells ( r , j ) (r, j) (r,j) satisfying c ≤ j ≤ c + w c \le j \le c + w cjc+w belong to this L-shape.

The following image illustrates some L-shapes.

Input

There is only one test case in each test file.

The first line contains three integers n n n, b i b_i bi and b j b_j bj ( 1 ≤ n ≤ 1 0 3 1 \le n \le 10^3 1n103, 1 ≤ b i , b j ≤ n 1 \le b_i, b_j \le n 1bi,bjn) indicating the size of the grid and the position of the black cell.

Output

If a valid answer exists first output Yes in the first line, then in the second line output an integer k k k ( 0 ≤ k ≤ n 2 − 1 3 0 \leq k \leq \frac{n^2-1}{3} 0k3n21) indicating the number of L-shapes to cover white cells. Then output k k k lines where the i i i-th line contains four integers r i r_i ri, c i c_i ci, h i h_i hi, w i w_i wi separated by a space indicating that the i i i-th L-shape is uniquely determined by ( r i , c i , h i , w i ) (r_i, c_i, h_i, w_i) (ri,ci,hi,wi). If there are multiple valid answers you can print any of them.

If there is no valid answer, just output No in one line.

AC代码

#include<bits/stdc++.h>
using namespace std;
int n,x,y,sum,a,b,c,d,len;
int main()
{
	scanf("%d%d%d",&n,&x,&y);
	sum=min(min(x-1,y-1),min(n-x,n-y));
	a=x-1-sum,b=y-1-sum,c=n-x-sum,d=n-y-sum;
	printf("Yes\n%d\n",2*sum+(n-sum*2-1));
	for(int i=1;i<=sum;++i) printf("%d %d %d %d\n",x+i,y+i,-i-1,-i-1),printf("%d %d %d %d\n",x-i,y-i,i,i);
	if(!a&&!b) for(int i=x+sum+1;i<=n;++i) printf("%d %d %d %d\n",i,i,-i,-i);
	else if(!c&&!b) for(int i=x+sum+1;i<=n;++i) printf("%d %d %d %d\n",i,n-i+1,-i,i);
	else if(!c&&!d) for(int i=x+sum+1;i<=n;++i) printf("%d %d %d %d\n",n-i+1,n-i+1,i,i);
	else if(!a&&!d) for(int i=x+sum+1;i<=n;++i) printf("%d %d %d %d\n",n-i+1,i,i,-i);
	else if(!a)
	{
		x=x+sum+1,len=sum*2+1;
		for(int i=y-sum-1;i>=1;--i) printf("%d %d %d %d\n",x++,i,-len++,len);
		for(int i=y+sum+1;i<=n;++i) printf("%d %d %d %d\n",x++,i,-len++,-len);
	}
	else if(!b)
	{
		y=y+sum+1,len=sum*2+1;
		for(int i=x-sum-1;i>=1;--i) printf("%d %d %d %d\n",i,y++,len++,-len);
		for(int i=x+sum+1;i<=n;++i) printf("%d %d %d %d\n",i,y++,-len++,-len);
	}
	else if(!c)
	{
		x=x-sum-1,len=sum*2+1;
		for(int i=y-sum-1;i>=1;--i) printf("%d %d %d %d\n",x--,i,len++,len);
		for(int i=y+sum+1;i<=n;++i) printf("%d %d %d %d\n",x--,i,len++,-len);
	}
	else
	{
		y=y-sum-1,len=sum*2+1;
		for(int i=x-sum-1;i>=1;--i) printf("%d %d %d %d\n",i,y--,len++,len);
		for(int i=x+sum+1;i<=n;++i) printf("%d %d %d %d\n",i,y--,-len++,len);
	}
	return 0;
}
  • 27
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值