Educational Codeforces Round 79 (Rated for Div. 2) A-D

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int te; cin >> te;
	while (te--) {
		int sum[5];
		cin >> sum[0] >> sum[1] >> sum[2];
		sort(sum, sum + 3);
		if (sum[1] + sum[0] + 1 >= sum[2]) {
			cout << "Yes\n";
		}
		else {
			cout << "No\n";
		}
		
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
long long sum[100010];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int te; cin >> te;
	while (te--) {
		long long n, m;
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			cin >> sum[i];
		}
		long long ans = 0, tmp = 0, maxn = -1, spot = 0, pos = -1;
		for (int i = 1; i <= n; i++) {
			tmp += sum[i];
			if (maxn < sum[i]) {
				maxn = sum[i];
				pos = i;
			}
			if (tmp - maxn <= m) {
				ans = pos;
				spot = 1;
			}
		}
		if (spot == 0) {
			cout << 0 << "\n";
		}
		else {
			if(tmp<=m) cout << 0 << "\n";
			else cout << ans << "\n";
		}
	}
	return 0;
}

C. Stack of Presents

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Santa has to send presents to the kids. He has a large stack of nn presents, numbered from 11 to nn; the topmost present has number a1a1, the next present is a2a2, and so on; the bottom present has number anan. All numbers are distinct.

Santa has a list of mm distinct presents he has to send: b1b1, b2b2, ..., bmbm. He will send them in the order they appear in the list.

To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are kk presents above the present Santa wants to send, it takes him 2k+12k+1 seconds to do it. Fortunately, Santa can speed the whole process up — when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).

What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.

Your program has to answer tt different test cases.

Input

The first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

Then the test cases follow, each represented by three lines.

The first line contains two integers nn and mm (1≤m≤n≤1051≤m≤n≤105) — the number of presents in the stack and the number of presents Santa wants to send, respectively.

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤n1≤ai≤n, all aiai are unique) — the order of presents in the stack.

The third line contains mm integers b1b1, b2b2, ..., bmbm (1≤bi≤n1≤bi≤n, all bibi are unique) — the ordered list of presents Santa has to send.

The sum of nn over all test cases does not exceed 105105.

Output

For each test case print one integer — the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.

Example

input

Copy

2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1

output

Copy

5
8
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int a[100010], b[100010], pos[100010];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int te; cin >> te;
	while (te--) {
		int n, m;
		cin >> n >> m;
		for (int i = 1; i <= n; i++)
			cin >> a[i], pos[a[i]] = i;
		for (int i = 1; i <= m; i++)
			cin >> b[i];
		int deep = 0;long long sum = 0;
		for (int i = 1; i <= m; i++) {
			if (pos[b[i]] > deep) {
				deep = pos[b[i]];
				sum += 2 * (deep - i) + 1;
			}
			else {
				sum++;
			}
		}
		cout << sum << "\n";
	}
	return 0;
}

D. Santa's Bot

time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Santa Claus has received letters from nn different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the ii-th kid asked Santa to give them one of kiki different items as a present. Some items could have been asked by multiple kids.

Santa is really busy, so he wants the New Year Bot to choose the presents for all children. Unfortunately, the Bot's algorithm of choosing presents is bugged. To choose a present for some kid, the Bot does the following:

  • choose one kid xx equiprobably among all nn kids;
  • choose some item yy equiprobably among all kxkx items kid xx wants;
  • choose a kid zz who will receive the present equipropably among all nn kids (this choice is independent of choosing xx and yy); the resulting triple (x,y,z)(x,y,z) is called the decision of the Bot.

If kid zz listed item yy as an item they want to receive, then the decision valid. Otherwise, the Bot's choice is invalid.

Santa is aware of the bug, but he can't estimate if this bug is really severe. To do so, he wants to know the probability that one decision generated according to the aforementioned algorithm is valid. Can you help him?

Input

The first line contains one integer nn (1≤n≤1061≤n≤106) — the number of kids who wrote their letters to Santa.

Then nn lines follow, the ii-th of them contains a list of items wanted by the ii-th kid in the following format: kiki ai,1ai,1 ai,2ai,2 ... ai,kiai,ki (1≤ki,ai,j≤1061≤ki,ai,j≤106), where kiki is the number of items wanted by the ii-th kid, and ai,jai,j are the items themselves. No item is contained in the same list more than once.

It is guaranteed that ∑i=1nki≤106∑i=1nki≤106.

Output

Print the probatility that the Bot produces a valid decision as follows:

Let this probability be represented as an irreducible fraction xyxy. You have to print x⋅y−1mod998244353x⋅y−1mod998244353, where y−1y−1 is the inverse element of yy modulo 998244353998244353 (such integer that y⋅y−1y⋅y−1 has remainder 11 modulo 998244353998244353).

Examples

input

Copy

2
2 2 1
1 1

output

Copy

124780545

input

Copy

5
2 1 2
2 3 1
3 2 4 3
2 1 4
3 4 3 2

output

Copy

798595483

          概率dp。

#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define PB push_back
#define LL long long
#define pii pair<int,int>
#define MEM(x,y) memset(x,y,sizeof(x))
#define bug(x) cout<<"debug "#x" is "<<x<<endl;
#define FIO ios::sync_with_stdio(false);
#define ALL(x) x.begin(),x.end()
#define LOG 20
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
const int maxn =1e6+7;
const int inf = 1e9+7;
const LL mod=998244353;
LL qpow(LL a,LL b){
    LL ret=1;
    while(b){
        if(b&1) ret=(ret*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ret;
}
LL add(LL a,LL b){return (a+b)%mod;}
LL mul(LL a,LL b){return (a*b)%mod;}
LL di(LL a,LL b){return mul(a,qpow(b,mod-2));}
int main(){
    FIO;
    int t,n,x,m;
    cin>>n;
    map<LL,LL> M;
    vector<vector<LL>> a(n);
    for(int i=0;i<n;i++){
        cin>>m;
        for(int j=0;j<m;j++){
            cin>>x;
            a[i].PB(x);
            M[x]=add(M[x],mul(di(1LL,n),di(1LL,m)));
        }
    }
    LL ans=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<a[i].size();j++){
            ans=add(ans,mul(di(1,n),M[a[i][j]]));
        }
    }
    cout<<ans<<endl;
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/chenshibo17/article/details/103743830