2019 GDUT Rating Contest #I 题解

                           2019 GDUT Rating Contest #I 题解
                                                   A题

题面:

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)(1≤N≤100), conveniently numbered 1…N1…N. The ithith cow needs to be milked from time sisi to time titi, and requires bibi 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′si′s milking cannot be used for any other cow's milking between time sisi and time titi. 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 sisi's and ti's are all distinct).

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

Please determine how many total buckets FJ would need to keep in his storage room in order to milk all the cows successfully.

Input

The first line of input contains NN. The next NN lines each describe one cow, containing the numbers sisi, titi, and bibi, separated by spaces. Both sisi and titi are integers in the range 1…10001…1000, and bibi is an integer in the range 1…101…10.

Output

Output a single integer telling how many total buckets FJ needs.

Example

input

3
4 10 1
8 13 3
2 6 2
output

4

Note

In this example, FJ needs 4 buckets: He uses buckets 1 and 2 for milking cow 3 (starting at time 2). He uses bucket 3 for milking cow 1 (starting at time 4). When cow 2 arrives at time 8, buckets 1 and 2 are now available, but not bucket 3, so he uses buckets 1, 2, and 4.

题面描述:

             题目讲述的是,有n头奶牛,每头奶牛都有对应的时间去被挤牛奶以及需要盛奶牛的木桶数,如果同一个时间段有多头奶牛被挤牛奶的话,那么一个木桶只能盛一头奶牛的牛奶,题目问我们最少用多少个木桶可以满足奶牛的需求。

题目分析:

             由于题目给的数据比较小,因此我们对于每头奶牛被挤牛奶的时间段均加上木桶数,然后求出每个时间段需要的最大木桶数即为最终答案。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int Maxn=105;
const int Maxst=1005;
int cnt[Maxst];
int main()
{
	int n;
	while (~scanf("%d",&n))
	{
		memset(cnt,0,sizeof(cnt));
		int ans=0;
		for (reg int i=1;i<=n;i++)
		{
			int s,t,b;
			scanf("%d%d%d",&s,&t,&b);
			for (reg int j=s;j<=t;j++)
			{
				cnt[j]+=b;
				ans=max(ans,cnt[j]);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

                                                   B题

题面:

For his favorite holiday, Farmer John wants to send presents to his friends. Since he isn't very good at wrapping presents, he wants to enlist the help of his cows. As you might expect, cows are not much better at wrapping presents themselves, a lesson Farmer John is about to learn the hard way.

Farmer John's NN cows (1≤N≤1041≤N≤104) are all standing in a row, conveniently numbered 1…N1…N in order. Cow ii has skill level sisi at wrapping presents. These skill levels might vary quite a bit, so FJ decides to combine his cows into teams. A team can consist of any consecutive set of up to KK cows (1≤K≤1031≤K≤103), and no cow can be part of more than one team. Since cows learn from each-other, the skill level of each cow on a team can be replaced by the skill level of the most-skilled cow on that team.

Please help FJ determine the highest possible sum of skill levels he can achieve by optimally forming teams.

Input

The first line of input contains NN and KK. The next NN lines contain the skill levels of the NN cows in the order in which they are standing. Each skill level is a positive integer at most 105105.

Output

Please print the highest possible sum of skill levels FJ can achieve by grouping appropriate consecutive sets of cows into teams.

Example

input

7 3
1
15
7
9
2
5
10
output

84

Note

In this example, the optimal solution is to group the first three cows and the last three cows, leaving the middle cow on a team by itself (remember that it is fine to have teams of size less than KK). This effectively boosts the skill levels of the 7 cows to 15, 15, 15, 9, 10, 10, 10, which sums to 84.

题面描述:

            这个题目讲述的是,输入n头奶牛的技巧值,然后可以将这些奶牛分成若干个组,每个组的奶牛数不超过k头,每组的奶牛经过互相学习,每头奶牛的技巧值均等于这头奶牛所在该组技巧值最高的奶牛的技巧值,而且奶牛的输入顺序不能够打乱,分为一组的奶牛必须是连续的奶牛,然后问我们怎么分组才能使得所有奶牛的技巧总和最大。

题目分析:

            这是一道线型DP的题目,我们设置一个DP数组,dp[i]表示前面的i头奶牛通过适当的分组所得的最大技巧值总和。

            首先我们从1到n枚举n头奶牛,然后我们需要枚举当前最后一组有多少头奶牛,假设当前最后一组有j头奶牛,那么dp[i]很明显就等于dp[i-j]再加上最后一组中技巧值最大的奶牛的技巧值乘以最后一组的奶牛个数。

            我们在求解最后一组奶牛最大技巧值的时候,如果我们再用一个循环去找出最大值的话,那么三重循环很明显会超时,其实我们可以枚举j从1到min(i,k),最后一组的奶牛数目从小到大枚举,这里注意的是,最后一组无论如何都要包括第i头奶牛,其实我们会发现这个过程已经从i枚举到i-j+1,刚好枚举完最后一组的所有奶牛,因此用一个变量记录枚举当中奶牛最大的技巧值即可,然后按上面的状态转移方程求出dp[i]即可,最后答案便为dp[n]。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int Maxn=1e4+5;
const int Maxk=1005;
int skill[Maxn],dp[Maxn];
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	for (reg int i=1;i<=n;i++) scanf("%d",&skill[i]);
	for (reg int i=1;i<=n;i++)
	{
		int maxs=-1;
		for (reg int j=1;j<=min(k,i);j++)
		{
			maxs=max(maxs,skill[i-j+1]);
			dp[i]=max(dp[i],dp[i-j]+maxs*j);
		}
	}
	printf("%d\n",dp[n]);
	return 0;
}

                                                   C题

题面:

With plenty of free time on their hands (or rather, hooves), the cows on Farmer John's farm often pass the time by playing video games. One of their favorites is based on a popular human video game called Puyo Puyo; the cow version is of course called Mooyo Mooyo. The game of Mooyo Mooyo is played on a tall narrow grid N cells tall (1≤N≤100)(1≤N≤100) and 1010 cells wide. Here is an example with N=6N=6:

00000000000000000000

00000003000000000300

00540003000054000300

10545022301054502230

22111222202211122220

11111112231111111223

Each cell is either empty (indicated by a 00), or a haybale in one of nine different colors (indicated by characters 1..91..9). Gravity causes haybales to fall downward, so there is never a 00 cell below a haybale.

Two cells belong to the same connected region if they are directly adjacent either horizontally or vertically, and they have the same nonzero color. Any time a connected region exists with at least KK cells, its haybales all disappear, turning into zeros. If multiple such connected regions exist at the same time, they all disappear simultaneously. Afterwards, gravity might cause haybales to fall downward to fill some of the resulting cells that became zeros. In the resulting configuration, there may again be connected regions of size at least KK cells. If so, they also disappear (simultaneously, if there are multiple such regions), then gravity pulls the remaining cells downward, and the process repeats until no connected regions of size at least KK exist.

Given the state of a Mooyo Mooyo board, please output a final picture of the board after these operations have occurred.

Input

The first line of input contains NN and K(1≤K≤10N)K(1≤K≤10N). The remaining NN lines specify the initial state of the board.

Output

Please output NN lines, describing a picture of the final board state.

Example

input

6 3
0000000000
0000000300
0054000300
1054502230
2211122220
1111111223
output

0000000000
0000000000
0000000000
0000000000
1054000000
2254500000

Note

In the example above, if K=3, then there is a connected region of size at least K with color 1 and also one with color 2. Once these are simultaneously removed, the board temporarily looks like this:

0000000000

0000000300

0054000300

1054500030

2200000000

0000000003

Then, gravity takes effect and the haybales drop to this configuration:

0000000000

0000000000

0000000000

0000000000

1054000300

2254500333

Again, there is a region of size at least K (with color 3). Removing it yields the final board configuration:

0000000000

0000000000

0000000000

0000000000

1054000000

2254500000

题面描述:

            题目讲述的是,输入n行10列的图以及k,然后图中除0之外的连续块中的数字个数大于等于k的话,那么这些数字将会全部变成0,值得注意的是,图中所有除0之外且连通块中的数字个数大于等于k的连通块同时变成0,然后每一列中的数字0必须在非0数字的上面,如果不满足的话,那么每一列的0必须要移上来,其他非0的数字全部保留固定顺序移下去,然后新得到的图重新按照上面的操作进行,直到图中不存在除0之外的连通块中的数字个数大于等于k。最后输出不能进行以上操作的图。

题目分析:

            这个题目由于数据范围很小,所以用模拟去做就行了,按照上面题目给的操作以及判断去做就行。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int Maxn=105;
const int m=10;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
struct node
{
	int x,y;
};
char mapp[Maxn][m+5];
int n,k,cnt;
bool sign[Maxn][m+5];
void dfs(int x,int y)
{
	if (x<0 || y<0 || x==n || y==m || sign[x][y]) return;
	sign[x][y]=1;
	cnt++;
	for (reg int i=0;i<4;i++)
	{
		int xx=x+dx[i],yy=y+dy[i];
		if (mapp[xx][yy]==mapp[x][y]) dfs(xx,yy);
	}
}
void clear()
{
	for (reg int i=0;i<n;i++)
	{
		for (reg int j=0;j<m;j++)
		if (sign[i][j]) mapp[i][j]='0';
	}
}
void solve()
{
	bool ff=0;
	for (reg int i=0;i<n;i++)
	{
		for (reg int j=0;j<m;j++)
		if (!sign[i][j] && mapp[i][j]!='0')
		{
			memset(sign,0,sizeof(sign));
			cnt=0;
			dfs(i,j);
			if (cnt<k) continue;
			ff=1;
			clear();
		}
	}
	for (reg int j=0;j<m;j++)
	{
		for (reg int i=n-1;i>=0;i--)
		{
			if (mapp[i][j]!='0') continue;
			int t=i;
			while (mapp[t][j]=='0' && t>=0) t--;
			if (t<0) break;
			char temp;
			temp=mapp[i][j];mapp[i][j]=mapp[t][j];mapp[t][j]=temp;
		}
	}
	if (ff) solve();
}
int main()
{
	scanf("%d%d",&n,&k);
	for (reg int i=0;i<n;i++) cin>>mapp[i];
	solve();
	for (reg int i=0;i<n;i++) cout<<mapp[i]<<endl;
	return 0;
}

                                                   D题

题面:

It turns out there is one factor that matters far more than any other when determining whether two cows are compatible as potential friends: whether they like similar flavors of ice cream!

Farmer John's NN cows (2≤N≤50,0002≤N≤50,000) have each listed their five favorite flavors of ice cream. To make this list concise, each possible flavor is represented by a positive integer ID at most 106106. Two cows are compatible if their lists contain at least one common flavor of ice cream.

Please determine the number of pairs of cows that are NOT compatible.

Input

The first line of input contains NN. Each of the following NN lines contain 5 integers (all different) representing the favorite ice cream flavors of one cow.

Output

Please output the number of pairs of cows that are not compatible.

Example

input

4
1 2 3 4 5
1 2 3 10 8
10 9 8 7 6
50 60 70 80 90

output

4

Note

Here, cow 4 is not compatible with any of cows 1, 2, or 3, and cows 1 and 3 are also not compatible.

题面描述:

            这个题目讲述的是,输入n头奶牛,然后每头奶牛有5种口味,如果任意两头奶牛至少有一种口味是一样的,那么则认为这两头奶牛是友好的,题目让我们求的是,n头奶牛当中有多少对奶牛是不友好的。

题目分析:

            这个题目巧妙地应用了STL中的map以及容斥原理,由于奶牛的口味种类比较多以及奶牛的头数也比较多,所以暴力的做法肯定超时。

            这个题目的正解如下:

            我们很明显会发现我们直接求有多少对奶牛不友好是难以求解的,因此我们可以换个角度去算,我们先算出有多少对奶牛,然后减去友好的奶牛对数。

            对于每头奶牛的五种口味,我们首先将这5种口味从小到大排序,然后将这五种口味可以组成的所有组合弄出来,然后用map记录当前含有这个组合字符串的奶牛数,根据容斥原理的性质,奇加偶减,那么我们就可以求出当前i头奶牛有多少对奶牛是不友好的,最后输出经过容斥原理后得到的答案即可。

           值得注意的是,这个题目如果用cin输入的话,需要取消同步,否则会超时。

代码:

#include <iostream>
#include <fstream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
#define IO cin.tie(0),ios::sync_with_stdio(false);
using namespace std;
map <string,int> mapp;
string cow[10];
int main()
{
	cin.tie(0),ios::sync_with_stdio(false);
	int n;
	cin>>n;
	ll ans=(ll)n*(n-1)/2;
	for (reg int i=1;i<=n;i++)
	{
		for (reg int j=0;j<5;j++) cin>>cow[j];
		sort(cow,cow+5);
		for (reg int j=1;j<(1<<5);j++)
		{
			int cnt=0;
			string st;
			for (reg int k=0;k<5;k++)
			if (j&(1<<k))
			{
				st+=cow[k]+' ';
				cnt++;
			}
			if (cnt&1) ans-=mapp[st];else ans+=mapp[st];
			mapp[st]++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

                                                   E题

题面:

Cows from all over the world are arriving at the local airport to attend the convention and eat grass. Specifically, there are NN cows arriving at the airport (1≤N≤1051≤N≤105) and cow ii arrives at time titi (0≤ti≤1090≤ti≤109). Farmer John has arranged MM (1≤M≤1051≤M≤105) buses to transport the cows from the airport. Each bus can hold up to CC cows in it (1≤C≤N1≤C≤N). Farmer John is waiting with the buses at the airport and would like to assign the arriving cows to the buses. A bus can leave at the time when the last cow on it arrives. Farmer John wants to be a good host and so does not want to keep the arriving cows waiting at the airport too long. What is the smallest possible value of the maximum waiting time of any one arriving cow if Farmer John coordinates his buses optimally? A cow's waiting time is the difference between her arrival time and the departure of her assigned bus.

It is guaranteed that MC≥NMC≥N.

Input

The first line contains three space separated integers NN, MM, and CC. The next line contains NN space separated integers representing the arrival time of each cow.

Output

Please write one line containing the optimal minimum maximum waiting time for any one arriving cow.

Example

input

6 3 2
1 1 10 14 4 3
output

4

题面描述:

            题目讲述的是,输入n头奶牛,m辆公交车以及公交车最多可以装载的奶牛数c,然后输入n头奶牛的到达时间,为了不让每头奶牛等太久,题目问我们每头奶牛最小的最大等待时间,所谓的等待时间指的是每辆公交车最早到的奶牛与最晚到的奶牛的时间差。

题目分析:

            这个题目是一道典型的二分答案的题目,我们首先将奶牛的到达时间从小到大排序,然后我们二分枚举一个答案出来,为了满足这个答案,每辆公交车的最早到的奶牛与最晚到的奶牛的时间差不能超过这个答案,然后我们判断满足这个答案最少需要多少辆公交车,如果需要公交车的辆数大于m的话,那么证明我们二分枚举的答案太小了,否则就太小了,需要公交车的辆数等于m的时候,我们二分的答案尽量取小一点,最终二分出来的即为答案。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int Maxn=1e5+5;
int n,m,c,maxt;
int cow[Maxn];
bool check(int k)
{
	int ans=1,pre=cow[1],tot=1;
	for (reg int i=2;i<=n;i++)
	if (cow[i]-pre>k || tot>=c)
	{
		tot=1;ans++;pre=cow[i];
	}
	else tot++;
	if (ans<=m) return 1;else return 0;
}
int main()
{
	maxt=0;
	scanf("%d%d%d",&n,&m,&c);
	for (reg int i=1;i<=n;i++)
	{
		scanf("%d",&cow[i]);
		maxt=max(maxt,cow[i]);
	}
	sort(cow+1,cow+n+1);
	int low=0,high=maxt;
	while (low<=high)
	{
		int mid=(low+high)>>1;
		if (check(mid)) high=mid-1;else low=mid+1;
	}
	printf("%d\n",low);
	return 0;
}

                                                   F题

题面:

The cows are heading back to the barn at the end of a long day, feeling both tired and hungry.

The farm consists of NN pastures (2≤N≤50,0002≤N≤50,000), conveniently numbered 1…N1…N. The cows all want to travel to the barn in pasture NN. Each of the other N−1N−1 pastures contains a cow. Cows can move from pasture to pasture via a set of MM undirected trails (1≤M≤100,0001≤M≤100,000). The iith trail connects a pair of pastures aiai and bibi, and requires time titi to traverse. Every cow can reach the barn through a sequence of trails.

Being hungry, the cows are interested in potentially stopping for food on their way home. Conveniently, KK of the pastures contain tasty haybales (1≤K≤N1≤K≤N), with the iith such haybale having a yumminess value of yiyi. Each cow is willing to stop at a single haybale along her trip to the barn, but only if the amount of time this adds to her path is at most the yumminess of the haybale she visits. Note that a cow only "officially" visits at most one haybale for dining purposes, although it is fine if her path takes her through other pastures containing haybales; she simply ignores these.

Input

The first line contains three space-separated integers NN, MM, and KK. Each of the next MM lines contains three integers aiai, bibi, and titi, describing a trail between pastures aiai and bibi which takes titi time to traverse (aiai and bibi are different from each-other, and titi is a positive integer at most 104104)

The next KK lines each describe a haybale in terms of two integers: the index of its pasture, and its yumminess value (a positive integer at most 109109). Multiple haybales can reside in the same pasture.

Output

The output should consist of N−1N−1 lines. Line ii contains the single integer 11 if the cow at pasture ii can visit and dine on a haybale on the way to the barn, and 00 otherwise.

Example

input

4 5 1
1 4 10
2 1 20
4 2 3
2 3 5
4 3 2
2 7
output

1
1
1

题面描述:

            这个题目讲述的是,输入n个牧场,m条双向路径,以及通过这条双向路径所要花费的时间,还有输入k个草所在的第几个牧场以及一个yumminess值,这个题目先让我们求出前n-1个牧场到达第n个牧场的最短路径,然后每个牧场的奶牛都必须至少要到一个有草的牧场去吃草,然后再到达第n个牧场,要求奶牛吃完草后到达第n个牧场的最短路径与不吃草到达第n个牧场的最短路径的差值不超过去吃草的那个牧场的yumminess值,如果在第i(1<=i<=n-1)个牧场的奶牛可以实现以上操作的话,输出1,否则输出0。

题目分析:

            首先我们先算出前i-1个牧场到达第n个牧场的最短路径,如果我们以第i个牧场为起点,然后用Dijkstra算法求出到达第n个牧场的最短路径,很显然是会超时的。因此我们需要反过来以第n个牧场为起点,然后求出第n个牧场到达前n-1个牧场的最短路径。

            这个题目的关键在于这里,如果保证每个牧场的奶牛至少经过一个有草的牧场后然后再到达第n个牧场,我们需要构造一个虚点,这个虚点连接着所有有草的牧场,权值为第n个牧场到达当前这个有草的牧场的最短路径减去这个牧场的yumminess值,然后我们再求出这个虚点到达前n-1个牧场的最短路径,然后与上面求到的第n个牧场到达前n-1个牧场的最短路径进行比较,如果前者小于等于后者的话,那么证明满足题目要求,则输出1,否则输出0。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int Maxn=5e4+5;
const int Maxm=1e5+5;
typedef pair<int,int> p;
struct Edge
{
	int t,dist,next;
};
Edge edge[(Maxm<<1)+Maxn];
int first[Maxn],dist[Maxn],distt[Maxn];
int n,tot;
void addedge(int a,int b,int t)
{
	edge[++tot].t=b;
	edge[tot].dist=t;
	edge[tot].next=first[a];
	first[a]=tot;
}
void Dijkstra(int x0)
{
	memset(dist,INF,sizeof(dist));
	priority_queue <p,vector<p>,greater<p> > q;
	q.push(p(0,x0));dist[x0]=0;
	while (!q.empty())
	{
		p temp=q.top();
		q.pop();
		if (dist[temp.second]<temp.first) continue;
		for (reg int i=first[temp.second];i!=-1;i=edge[i].next)
		if (dist[temp.second]+edge[i].dist<dist[edge[i].t])
		{
			dist[edge[i].t]=dist[temp.second]+edge[i].dist;
			q.push(p(dist[edge[i].t],edge[i].t));
		}
	}
}
int main()
{
	int m,k;
	scanf("%d%d%d",&n,&m,&k); 
	memset(first,-1,sizeof(first));
	tot=0;
	for (reg int i=1;i<=m;i++)
	{
		int a,b,t;
		scanf("%d%d%d",&a,&b,&t);
		addedge(a,b,t);
		addedge(b,a,t);
	}
	Dijkstra(n);
	memcpy(distt,dist,sizeof(distt));
	for (reg int i=1;i<=k;i++)
	{
		int pas,val;
		scanf("%d%d",&pas,&val);
		addedge(0,pas,dist[pas]-val);
	}
	Dijkstra(0);
	for (reg int i=1;i<n;i++) printf("%d\n",dist[i]<=distt[i]);
	return 0;
}

                                                   G题

题面:

Farmer John has two milking barns, each of which has a large milk tank as well as a storage closet containing 1010 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 10001000gallons of milk in the tank of the first barn, and exactly 10001000 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?

Input

The first line of input contains 1010 integers, giving the sizes of the buckets initially at the first barn. The second line of input contains 1010more integers, giving the sizes of the buckets initially at the second barn. All bucket sizes are in the range 1…1001…100.

Output

Please print the number of possible readings Farmer John could get from measuring the milk in the tank of the first barn after Friday.

Example

input

1 1 1 1 1 1 1 1 1 2
5 5 5 5 5 5 5 5 5 5

output

5

Note

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.

题面描述:

            题目讲述的是,有两个谷仓,每个谷仓一开始都有1000加仑的牛奶,然后每个谷仓均有10个大小的桶。

            FJ第一天可以在第一个谷仓任意选择一个桶去装满牛奶,然后把桶内的所有牛奶倒进第二个谷仓里,并且把桶放在第二个谷仓,然后第二天又重新从第二个谷仓任意选择一个桶去装满牛奶,然后把桶内的牛奶倒进第一个谷仓,以此类推,问我们直到第五天,第一个谷仓中的牛奶容量会出现多少种情况。

题目分析:

            这个题目可以用回溯去做,我们每一天枚举能够选择的桶,然后最后用一个数组去标记第五天的第一个谷仓的牛奶有多少加仑,避免重复的计算,最后统计出有多少种情况不一样即可。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int n=10;
int barn[2][n+5];
bool sign[2005];
int ans;
void solve(int k,int ftot,int state,int last)
{
	if (k==4)
	{
		if (sign[ftot]) return;
		sign[ftot]=1;
		ans++;
		return;
	}
	if (last!=-1) solve(k+1,state==0?ftot-last:ftot+last,state^1,last);
	for (reg int i=1;i<=10;i++)
	if (barn[state][i]!=-1)
	{
		int temp=barn[state][i];
		if (last==-1) barn[state][i]=-1;else barn[state][i]=last;
		solve(k+1,state==0?ftot-temp:ftot+temp,state^1,temp);
		barn[state][i]=temp;
	}
}
int main()
{
	for (reg int i=1;i<=10;i++) scanf("%d",&barn[0][i]);
	for (reg int i=1;i<=10;i++) scanf("%d",&barn[1][i]);
	ans=0;
	memset(sign,0,sizeof(sign));
	solve(0,1000,0,-1);
	printf("%d\n",ans);
	return 0;
}

                                                   H题

题面:

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 11 into bucket 22, then bucket 22 into bucket 33, then bucket 33 into bucket 11, then bucket 11 into bucket 22, and so on in a cyclic fashion, for a total of 100100 pour operations (so the 100100th pour would be from bucket 11 into bucket 22). 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 100100 pours.

Input

The first line of the input file contains two space-separated integers: the capacity c1c1 of the first bucket, and the amount of milk m1m1 in the first bucket. Both c1c1 and m1m1 are positive and at most 11 billion, with c1≤m1c1≤m1. The second and third lines are similar, containing capacities and milk amounts for the second and third buckets.

Output

Please print three lines of output, giving the final amount of milk in each bucket, after 100100 pour operations.

Example

input

10 3
11 4
12 5

output

0
10
2

Note

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 ...)

题面描述:

            题目讲述的是,题目输入三个桶的容量以及初始的牛奶容量,第一天将第一个桶内的牛奶倒进第二个桶,如果可以将第二个桶的牛奶倒满的话,那么就将第二个桶倒满,剩下的牛奶留在第一个桶,否则将第一个桶的牛奶全部倒进第二个桶,第二天按照上面的规则将第二个桶的牛奶倒进第三个桶,第三天依旧按照上面的规则将第三个桶的奶牛倒进第一个桶,以此类推,题目问我们经过100天后,三个桶分别有多少牛奶。

题目分析:

            按照上面题目给的规则模拟就好,模拟到100天后输出三个桶内的牛奶容量即可。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int n=3;
struct Buck
{
	int c,m;
};
Buck buck[n+5];
int main()
{
	for (reg int i=0;i<n;i++) scanf("%d%d",&buck[i].c,&buck[i].m);
	int T=100,i=0;
	while (T--)
	{
		if (buck[(i+1)%n].m+buck[i].m>buck[(i+1)%n].c)
		{
			buck[i].m=buck[(i+1)%n].m+buck[i].m-buck[(i+1)%n].c;
			buck[(i+1)%n].m=buck[(i+1)%n].c;
			i=(i+1)%n;
			continue;
		}
		buck[(i+1)%n].m=buck[(i+1)%n].m+buck[i].m;
		buck[i].m=0;
		i=(i+1)%n;
	}
	for (reg int i=0;i<n;i++) printf("%d\n",buck[i].m);
	return 0;
}

                                                   I题

题面:

Despite long delays in airport pickups, Farmer John's convention for cows interested in eating grass has been going well so far. It has attracted cows from all over the world.

The main event of the conference, however, is looking like it might cause Farmer John some further scheduling woes. A very small pasture on his farm features a rare form of grass that is supposed to be the tastiest in the world, according to discerning cows. As a result, all of the NN cows at the conference (1≤N≤1051≤N≤105) want to sample this grass. This will likely cause long lines to form, since the pasture is so small it can only accommodate one cow at a time.

Farmer John knows the time aiai that each cow ii plans to arrive at the special pasture, as well as the amount of time titi she plans to spend sampling the special grass, once it becomes her turn. Once cow ii starts eating the grass, she spends her full time of titi before leaving, during which other arriving cows need to wait. If multiple cows are waiting when the pasture becomes available again, the cow with the highest seniority is the next to be allowed to sample the grass. For this purpose, a cow who arrives right as another cow is finishing is considered "waiting". Similarly, if a number of cows all arrive at exactly the same time while no cow is currently eating, then the one with highest seniority is the next to eat.

Please help FJ compute the maximum amount of time any cow might possibly have to wait in line (between time aiai and the time the cow begins eating).

Input

The first line of input contains NN. Each of the next NN lines specify the details of the NN cows in order of seniority (the most senior cow being first). Each line contains aiai and titi for one cow. The titi's are positive integers each at most 104104, and the aiai's are positive integers at most 109109.

Output

Please print the longest potential waiting time over all the cows.

Example

input

5
25 3
105 30
20 50
10 17
100 10

output

10

Note

In this example, we have 5 cows (numbered 1..5 according to their order in the input). Cow 4 is the first to arrive (at time 10), and before she can finish eating (at time 27) cows 1 and 3 both arrive. Since cow 1 has higher seniority, she gets to eat next, having waited 2 units of time beyond her arrival time. She finishes at time 30, and then cow 3 starts eating, having waited for 10 units of time beyond her starting time. After a gap where no cow eats, cow 5 arrives and then while she is eating cow 2 arrives, eating 5 units of time later. The cow who is delayed the most relative to her arrival time is cow 3.

题面描述:

            这个题目讲述的是,题目输入n头奶牛,然后下面n行输入n头奶牛的到达时间以及持续时间,题目让我们求奶牛等待的时间最长是多少。如果第i头奶牛到达的时候已经有奶牛在吃草的话,那么这头奶牛需要等到前面的奶牛吃完草才能吃草,如果同一时间有多头奶牛到达吃草的话,那么级别最高的奶牛先吃草,所谓的等级指的是奶牛的输入顺序,输入顺序越前,级别越高。

题目分析:

            这个题目我们先将奶牛的到达时间按从小到大排序,然后计算第一头奶牛的离开时间,如果后面的奶牛到达时间早于第一头的离开时间的话,那么将这些奶牛插入到优先队列中,按照级别从高到低排序,直到第一头奶牛吃完草后,那么把优先队列中把权限最高的奶牛弹出优先队列,并且计算这头奶牛的等待时间以及离开时间,然后操作与上面第一头奶牛的操作即可,最后输出最大的等待时间即可。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int Maxn=1e5+5;
struct Cow
{
	int id;
	ll a,t;
};
Cow cow[Maxn];
bool cmp(Cow a,Cow b)
{
	if (a.a!=b.a) return a.a<b.a;
	return a.id<b.id;
}
bool operator < (Cow a,Cow b)
{
	return a.id>b.id;
}
int main()
{
	int n;
	scanf("%d",&n);
	for (reg int i=1;i<=n;i++)
	{
		cow[i].id=i;
		scanf("%d%d",&cow[i].a,&cow[i].t);
	}
	sort(cow+1,cow+n+1,cmp);
	priority_queue <Cow> q;
	ll ans=0,timee=0;
	for (reg int i=1;i<=n;i++)
	{
		if (cow[i].a<=timee)
		{
			Cow tt;
			tt.id=cow[i].id;tt.t=cow[i].t;tt.a=cow[i].a;
			q.push(tt);
			continue;
		}
		while (!q.empty() && cow[i].a>timee)
		{
			Cow temp=q.top();
			q.pop();
			ans=max(ans,timee-temp.a+1);
			timee=timee+temp.t;
		}
		if (!q.empty())
		{
			Cow tt;
			tt.id=cow[i].id;tt.t=cow[i].t;tt.a=cow[i].a;
			q.push(tt);
			continue;
		}
		timee=cow[i].a+cow[i].t-1;
	}
	printf("%lld\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36679229/article/details/88531222