Codeforces Round 905 (Div. 3)

目录

A. Morning

B. Chemistry

C. Raspberries

D. In Love

E. Look Back

F. You Are So Beautiful

G1. Dances (Easy version)

G2. Dances (Hard Version)


A. Morning

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a four-digit pin code consisting of digits from 00 to 99 that needs to be entered. Initially, the cursor points to the digit 11. In one second, you can perform exactly one of the following two actions:

  • Press the cursor to display the current digit,
  • Move the cursor to any adjacent digit.

The image above shows the device you are using to enter the pin code. For example, for the digit 55, the adjacent digits are 44 and 66, and for the digit 00, there is only one adjacent digit, 99.

Determine the minimum number of seconds required to enter the given four-digit pin code.

Input

Each test consists of multiple test cases. The first line contains a single integer t� (1≤t≤1e4) - the number of the test cases. This is followed by their description.

The single line of each test case describes the pin code as a string of length 44, consisting of digits from 00 to 99.

Output

For each test case, output the minimum number of seconds required to enter the given pin code.

Example 

input

Copy

 

10

1111

1236

1010

1920

9273

0000

7492

8543

0294

8361

output

Copy

4
9
31
27
28
13
25
16
33
24

Note

In the first test case, the cursor needs to be pressed 44 times.

In the second test case, it can be done in 99 seconds as follows:

  • Press the cursor.
  • Move the cursor to the digit 22.
  • Press the cursor.
  • Move the cursor to the digit 33.
  • Press the cursor.
  • Move the cursor to the digit 44.
  • Move the cursor to the digit 55.
  • Move the cursor to the digit 66.
  • Press the cursor.

直接用map就行

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
// const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }
map<char,int>mp;
void init()
{
  ll c=1;
  rep(i,'1','9')
  {
    mp[i]=c++;
  }
  mp['0']=10;
}
void solve()
{
  string s;
  cin >> s;
  ll num=0;
  char x='1';
  rep(i,0,3)
  {
    num+=abs(mp[s[i]]-mp[x]);
    num++;
    x=s[i];
  }
  cout << num<<endl;
}


int main()
{
   IOS;
   ll _;
    _=1;
    init();
    //scanf("%lld",&_);
    cin>>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}



 

B. Chemistry

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string s� of length n�, consisting of lowercase Latin letters, and an integer k�.

You need to check if it is possible to remove exactly k� characters from the string s� in such a way that the remaining characters can be rearranged to form a palindrome. Note that you can reorder the remaining characters in any way.

A palindrome is a string that reads the same forwards and backwards. For example, the strings "z", "aaa", "aba", "abccba" are palindromes, while the strings "codeforces", "reality", "ab" are not.

Input

Each test consists of multiple test cases. The first line contains a single integer t� (1≤t≤1041≤�≤104) — the number of the test cases. This is followed by their description.

The first line of each test case contains two integers n� and k� (0≤k<n≤1050≤�<�≤105) — the length of the string s� and the number of characters to be deleted.

The second line of each test case contains a string s� of length n�, consisting of lowercase Latin letters.

It is guaranteed that the sum of n� over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output "YES" if it is possible to remove exactly k� characters from the string s� in such a way that the remaining characters can be rearranged to form a palindrome, and "NO" otherwise.

You can output the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.

Example

input

Copy

 

14

1 0

a

2 0

ab

2 1

ba

3 1

abb

3 2

abc

6 2

bacacd

6 2

fagbza

6 2

zwaafa

7 2

taagaak

14 3

ttrraakkttoorr

5 3

debdb

5 4

ecadc

5 3

debca

5 3

abaac

output

Copy

YES
NO
YES
YES
YES
YES
NO
NO
YES
YES
YES
YES
NO
YES

Note

In the first test case, nothing can be removed, and the string "a" is a palindrome.

In the second test case, nothing can be removed, but the strings "ab" and "ba" are not palindromes.

In the third test case, any character can be removed, and the resulting string will be a palindrome.

In the fourth test case, one occurrence of the character "a" can be removed, resulting in the string "bb", which is a palindrome.

In the sixth test case, one occurrence of the characters "b" and "d" can be removed, resulting in the string "acac", which can be rearranged to the string "acca".

In the ninth test case, one occurrence of the characters "t" and "k" can be removed, resulting in the string "aagaa", which is a palindrome.

思路:

        因为要正好删除k个,那么就先得着单数的字母去删,可以留下一个或者零个单数的字母

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
// const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }

void solve()
{
  cin >> n >> m;
  string s;
  cin >> s;
  map<char,ll>mp;
  rep(i,0,s.size()-1)
  {
    mp[s[i]]++;
  }
  ll ant=0;
  for(auto it:mp)
  {
    if(it.second & 1)
    {
      ant++;
    }
  }
  if(m==0 && ant>1)
  {
    no;
    return;
  }
  if(ant==m || ant-1==m)
  {
    yes;
    return;
  }
  if(ant > m)
  {
    if(ant-m==1)
    {
      yes;
    }else{
      no;
    }
    return;
  }else 
  {
    if((m-ant)%2==0 || (m-ant-1)%2==0)
    {
      yes;
    }else{
      no;
    }
    return;
  }

}


int main()
{
   IOS;
   ll _;
    _=1;
    //scanf("%lld",&_);
    cin>>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}



 

C. Raspberries

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array of integers 𝑎1,𝑎2,…,𝑎𝑛

and a number 𝑘 (2≤𝑘≤5

). In one operation, you can do the following:

  • Choose an index 1≤𝑖≤𝑛
  • ,
  • Set 𝑎𝑖=𝑎𝑖+1
  • .

Find the minimum number of operations needed to make the product of all the numbers in the array 𝑎1⋅𝑎2⋅…⋅𝑎𝑛

divisible by 𝑘

.

Input

Each test consists of multiple test cases. The first line contains a single integer 𝑡

(1≤𝑡≤104

) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers 𝑛

and 𝑘 (2≤𝑛≤105, 2≤𝑘≤5) — the size of the array 𝑎 and the number 𝑘

.

The second line of each test case contains 𝑛

integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤10

).

It is guaranteed that the sum of 𝑛

over all test cases does not exceed 2⋅105

.

Output

For each test case, output the minimum number of operations needed to make the product of all the numbers in the array divisible by 𝑘

.

Example

Input

Copy

 

15

2 5

7 3

3 3

7 4 1

5 2

9 7 7 3 9

5 5

5 4 1 2 3

7 4

9 5 1 5 9 5 1

3 4

6 3 6

3 4

6 1 5

3 4

1 5 9

4 4

1 4 1 1

3 4

3 5 3

4 5

8 9 9 3

2 5

1 6

2 5

10 10

4 5

1 6 1 1

2 5

7 7

Output

Copy

2
2
1
0
2
0
1
2
0
1
1
4
0
4
3

Note

In the first test case, we need to choose the index 𝑖=2

twice. After that, the array will be 𝑎=[7,5]. The product of all the numbers in the array is 35

.

In the fourth test case, the product of the numbers in the array is 120

, which is already divisible by 5

, so no operations are needed.

In the eighth test case, we can perform two operations by choosing 𝑖=2

and 𝑖=3 in any order. After that, the array will be 𝑎=[1,6,10]. The product of the numbers in the array is 60.

思路:

        其实不难发现除了k等于4的时候要单独讨论一下,其他情况就直接处理,不满足就直接加上去就行了,为什么要特殊处理4,因为2可以由俩个二组成

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
// const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }

void solve()
{
  cin >> n >> m;
  ll a=0,b=0,c=0,d=0;//2345
  ll ant=0,cnt=0;
  ll x=INF;
  rep(i,1,n)
  {
    cin >> arr[i];
    if(arr[i]&1)
    {
      ant++;
    }else{
      cnt++;
    }
    ll res=arr[i]/m*m;
    if(arr[i]%m!=0)
    {
      res+=m;
    }
    x=min(x,res-arr[i]);
  }
  ll ans=x;;
  if(m==4)
  {
    if(cnt>1)
    {
      ans=0;
    }else if(cnt>0 && ant>0)
    {
      ans=min(x,ll(1));
    }else if(ant>1)
    {
      ans=min(x,ll(2));
    }
  }
  cout << ans<<endl;
}


int main()
{
   IOS;
   ll _;
    _=1;
    //scanf("%lld",&_);
    cin>>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}



 

 

D. In Love

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Initially, you have an empty multiset of segments. You need to process 𝑞

operations of two types:

  • +

𝑙 𝑟 — Add the segment (𝑙,𝑟)

  • to the multiset,
  • 𝑙
  • 𝑟 — Remove exactly one segment (𝑙,𝑟)
    • from the multiset. It is guaranteed that this segment exists in the multiset.

    After each operation, you need to determine if there exists a pair of segments in the multiset that do not intersect. A pair of segments (𝑙,𝑟)

    and (𝑎,𝑏) do not intersect if there does not exist a point 𝑥 such that 𝑙≤𝑥≤𝑟 and 𝑎≤𝑥≤𝑏

    .

    Input

    The first line of each test case contains an integer 𝑞

    (1≤𝑞≤105

    ) — the number of operations.

    The next 𝑞

    lines describe two types of operations. If it is an addition operation, it is given in the format + 𝑙 𝑟. If it is a deletion operation, it is given in the format − 𝑙 𝑟 (1≤𝑙≤𝑟≤109

    ).

    Output

    After each operation, print "YES" if there exists a pair of segments in the multiset that do not intersect, and "NO" otherwise.

    You can print the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.

    Example

    Input

    Copy

     

    12

    + 1 2

    + 3 4

    + 2 3

    + 2 2

    + 3 4

    - 3 4

    - 3 4

    - 1 2

    + 3 4

    - 2 2

    - 2 3

    - 3 4

    Output

    Copy

    NO
    YES
    YES
    YES
    YES
    YES
    NO
    NO
    YES
    NO
    NO
    NO
    

    Note

    In the example, after the second, third, fourth, and fifth operations, there exists a pair of segments (1,2)and (3,4)that do not intersect.

    Then we remove exactly one segment (3,4), and by that time we had two segments. Therefore, the answer after this operation also exists.

思路:

        一开始想的是线段树,但是想想没那么复杂,又想着用树状数字,但是发现还是做不出来,所以思路不对

         应该是用数据结构multiset之类的数据结构将左端点和右端点分开来存,然后看左端点的最大值是否大于右端点的最小值,如果大于,那么必然就存在交点。

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"*****hear*****"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
// const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }
multiset<ll>l,r;
ll cnt=0;
void solve()
{
  char op;
  ll a,b;
  cin >> op >> a >> b;
  if(op=='+')
  {
    cnt++;
    l.insert(a);
    r.insert(b);    
  }else{
    cnt--;
    l.erase(l.lower_bound(a));
    r.erase(r.lower_bound(b));
  }
  //cout << x <<"    "<<y<<endl;
  if(cnt<2)
  {
    no;
  }
  else if(*l.rbegin()<=*r.begin())
  {
    no;
  }else{
    yes;
  }
}


int main()
{
   IOS;
   ll _;
    _=1;
    //scanf("%lld",&_);
    cin>>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}



 

E. Look Back

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array of integers 𝑎1,𝑎2,…,𝑎𝑛

. You need to make it non-decreasing with the minimum number of operations. In one operation, you do the following:

  • Choose an index 1≤𝑖≤𝑛
  • ,
  • Set 𝑎𝑖=𝑎𝑖⋅2
  • .

An array 𝑏1,𝑏2,…,𝑏𝑛

is non-decreasing if 𝑏𝑖≤𝑏𝑖+1 for all 1≤𝑖<𝑛

.

Input

Each test consists of multiple test cases. The first line contains a single integer 𝑡

(1≤𝑡≤104

) — the number of test cases. This is followed by their description.

The first line of each test case contains an integer 𝑛

(1≤𝑛≤105) — the size of the array 𝑎

.

The second line of each test case contains 𝑛

integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109

).

It is guaranteed that the sum of 𝑛

over all test cases does not exceed 2⋅105

.

Output

For each test case, output the minimum number of operations needed to make the array non-decreasing.

Example

Input

Copy

 

9

1

1

2

2 1

3

3 2 1

4

7 1 5 3

5

11 2 15 7 10

6

1 8 2 16 8 16

2

624323799 708290323

12

2 1 1 3 3 11 12 22 45 777 777 1500

12

12 11 10 9 8 7 6 5 4 3 2 1

Output

Copy

0
1
3
6
10
3
0
2
66

Note

No operations are needed in the first test case.

In the second test case, we need to choose 𝑖=2

, after which the array will be [2,2]

.

In the third test case, we can apply the following operations:

  • Choose 𝑖=3

, after which the array will be [3,2,2]

  • ,
  • Choose 𝑖=3
  • , after which the array will be [3,2,4]
  • ,
  • Choose 𝑖=2
  • , after which the array will be [3,4,4].

思路:
        暴力直接跑会超时

        我们设a[i-1]进行了cnt次操做,n和m的初始值为0对于当前a[i],如果a[i]<a[i-1],我们考虑a[i]>=a[i-1]的最少操作次数为m,我们需要的操作次数为m+cnt(cnt=max(0,m+cnt);)

如果a[i]>a[i-1],我们考虑a[i-1]>=a[i]的最少操作次数为n

此时如果a[i-1]>a[i],然后我们考虑让a[i]>a[i-1]的操作m;

如果cnt<=n-m,需要操作次数为0

否则操作次数为cnt-(n-m)

综上cnt=max(0,cnt-n+m);

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"*****hear*****"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
// const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }

void solve()
{
  cin >> n;
  ll ans=0;
  rep(i,1,n)
  {
    cin >> arr[i];
  }
  ll f=0;
  rep(i,2,n)
  {
    ll x=arr[i-1];
    ll y=arr[i];
    ll ant=0;
    while(x<y)
    {
      x<<=1;
      ant--;
    }
    while(x>y)
    {
      y<<=1;
      ant++;
    } 
    
    f=max(ll(0),f+ant);
    ans+=f;
  }
  cout << ans << endl;;
}


int main()
{
   IOS;
   ll _;
    _=1;
    //scanf("%lld",&_);
    cin>>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}



 

F. You Are So Beautiful

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array of integers 𝑎1,𝑎2,…,𝑎𝑛

. Calculate the number of subarrays of this array 1≤𝑙≤𝑟≤𝑛

, such that:

  • The array 𝑏=[𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟]

occurs in the array 𝑎 as a subsequence exactly once. In other words, there is exactly one way to select a set of indices 1≤𝑖1<𝑖2<…<𝑖𝑟−𝑙+1≤𝑛, such that 𝑏𝑗=𝑎𝑖𝑗 for all 1≤𝑗≤𝑟−𝑙+1

  • .

Input

Each test consists of multiple test cases. The first line contains a single integer 𝑡

(1≤𝑡≤104

) — the number of test cases. This is followed by their description.

The first line of each test case contains an integer 𝑛

(1≤𝑛≤105) — the size of the array 𝑎

.

The second line of each test case contains 𝑛

integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109

).

It is guaranteed that the sum of 𝑛

over all test cases does not exceed 2⋅105

.

Output

For each test case, output the number of suitable subarrays.

Example

Input

Copy

 

6

1

1

2

1 1

3

1 2 1

4

2 3 2 1

5

4 5 4 5 4

10

1 7 7 2 3 4 3 2 1 100

Output

Copy

1
1
4
7
4
28

Note

In the first test case, there is exactly one subarray (1,1)

that suits us.

In the second test case, there is exactly one subarray (1,2)

that suits us. Subarrays (1,1) and (2,2) do not suit us, as the subsequence [1]

occurs twice in the array.

In the third test case, all subarrays except (1,1)

and (3,3) are suitable.

思路:

        一个序列al .... ar,只需要满足它的左端点al没在之前出现过,ar没在之后出现过即可

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"*****hear*****"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
// const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }
multiset<ll>l,r;
ll cnt=0;
void solve()
{
  cin >> n;
  rep(i,1,n)
  {
    cin >> arr[i];
  }
  map<ll,ll>mp;
  ll ant=0;
  ll ans=0;
  per(i,n,1)
  {
    mp[arr[i]]++;
    if(mp[arr[i]]==1)ant++;
    brr[i]=ant;
  }
  map<ll,ll>mmp;
  mp.clear();
  rep(i,1,n)
  {
    if(!mp.count(arr[i]))ans+=brr[i];
    mp[arr[i]]=1;
  }
  cout << ans<<endl;
}


int main()
{
   IOS;
   ll _;
    _=1;
    //scanf("%lld",&_);
    cin>>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}



 

G1. Dances (Easy version)

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This is the easy version of the problem. The only difference is that in this version 𝑚=1

.

You are given two arrays of integers 𝑎1,𝑎2,…,𝑎𝑛

and 𝑏1,𝑏2,…,𝑏𝑛

. Before applying any operations, you can reorder the elements of each array as you wish. Then, in one operation, you will perform both of the following actions, if the arrays are not empty:

  • Choose any element from array 𝑎

and remove it (all remaining elements are shifted to a new array 𝑎

  • ),
  • Choose any element from array 𝑏
  • and remove it (all remaining elements are shifted to a new array 𝑏
    • ).

    Let 𝑘

    be the final size of both arrays. You need to find the minimum number of operations required to satisfy 𝑎𝑖<𝑏𝑖 for all 1≤𝑖≤𝑘

    .

    This problem was too easy, so the problem author decided to make it more challenging. You are also given a positive integer 𝑚

    . Now, you need to find the sum of answers to the problem for 𝑚 pairs of arrays (𝑐[𝑖],𝑏), where 1≤𝑖≤𝑚. Array 𝑐[𝑖] is obtained from 𝑎

    as follows:

    • 𝑐[𝑖]1=𝑖
  • ,
  • 𝑐[𝑖]𝑗=𝑎𝑗
  • , for 2≤𝑗≤𝑛
    • .

    Input

    Each test consists of multiple test cases. The first line contains a single integer 𝑡

    (1≤𝑡≤104

    ) - the number of sets of input data. This is followed by their description.

    The first line of each test case contains two integers 𝑛

    and 𝑚 (2≤𝑛≤105, 𝑚=1) - the size of arrays 𝑎 and 𝑏 and the constraints on the value of element 𝑎1

    .

    The second line of each test case contains 𝑛−1

    integers 𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109

    ).

    The third line of each test case contains 𝑛

    integers 𝑏1,𝑏2,…,𝑏𝑛 (1≤𝑏𝑖≤109

    ).

    It is guaranteed that the sum of 𝑛

    over all test cases does not exceed 105

    .

    Output

    For each test case, output the total number of minimum operations for all pairs of arrays (𝑐𝑖,𝑏)

    .

    Example

    Input

    Copy

     

    4

    2 1

    1

    3 2

    4 1

    5 1 5

    3 8 3 3

    8 1

    4 3 3 2 2 1 1

    1 1 1 1 3 3 3 3

    9 1

    9 2 8 3 7 4 6 5

    1 2 3 2 1 4 5 6 5

    Output

    Copy

    0
    1
    4
    4
    

    Note

    In the first test case for the pair of arrays ([1,1],[3,2])

    , the answer is 0. No operations or reordering of elements are needed.

思路:
        因为m==1, 所以直接将a数组的1号为负值为m直接硬跑就能过

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"*****hear*****"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
// const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }

void add(ll x,ll k)
{
  for(int i=x;i<=1e5+10;i+=lowbit(i))
  {
    arr[i]+=k;
  }
}

ll query()
{
  ll sum=0;
  for(int i=1;i<=1e5+10;i+=lowbit(i))
  {
    sum+=arr[i];
  }
  return sum;
}

void solve()
{
  cin >> n >> m;
  rep(i,2,n)
  {
    cin >> arr[i];
  }
  rep(i,1,n)
  {
    cin >> brr[i];
  }

  sort(arr+2,arr+2+n-1);
  sort(brr+1,brr+1+n);
  ll ans=0;
  arr[1]=1;
  ll a=1,b=1;
  // if(brr[1]>m)b++;
  // else {
  //   ans++;
  //   b++;
  // }
  rep(i,1,n)
  {
    if(arr[a]<brr[b])
    {
      a++;
      b++;
    }else{
      ans++;
      b++;
    }
  }
  cout << ans<<endl;
}


int main()
{
   IOS;
   ll _;
    _=1;
    //scanf("%lld",&_);
    cin>>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}



 

G2. Dances (Hard Version)

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This is the hard version of the problem. The only difference is that in this version 𝑚≤109

.

You are given two arrays of integers 𝑎1,𝑎2,…,𝑎𝑛

and 𝑏1,𝑏2,…,𝑏𝑛

. Before applying any operations, you can reorder the elements of each array as you wish. Then, in one operation, you will perform both of the following actions, if the arrays are not empty:

  • Choose any element from array 𝑎

and remove it (all remaining elements are shifted to a new array 𝑎

  • ),
  • Choose any element from array 𝑏
  • and remove it (all remaining elements are shifted to a new array 𝑏
    • ).

    Let 𝑘

    be the final size of both arrays. You need to find the minimum number of operations required to satisfy 𝑎𝑖<𝑏𝑖 for all 1≤𝑖≤𝑘

    .

    This problem was too easy, so the problem author decided to make it more challenging. You are also given a positive integer 𝑚

    . Now, you need to find the sum of answers to the problem for 𝑚 pairs of arrays (𝑐[𝑖],𝑏), where 1≤𝑖≤𝑚. Array 𝑐[𝑖] is obtained from 𝑎

    as follows:

    • 𝑐[𝑖]1=𝑖
  • ,
  • 𝑐[𝑖]𝑗=𝑎𝑗
  • , for 2≤𝑗≤𝑛
    • .

    Input

    Each test consists of multiple test cases. The first line contains a single integer 𝑡

    (1≤𝑡≤104

    ) - the number of sets of input data. This is followed by their description.

    The first line of each test case contains two integers 𝑛

    and 𝑚 (2≤𝑛≤105, 1≤𝑚≤109) - the size of arrays 𝑎 and 𝑏 and the constraints on the value of element 𝑎1

    .

    The second line of each test case contains 𝑛−1

    integers 𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109

    ).

    The third line of each test case contains 𝑛

    integers 𝑏1,𝑏2,…,𝑏𝑛 (1≤𝑏𝑖≤109

    ).

    It is guaranteed that the sum of 𝑛

    over all test cases does not exceed 105

    .

    Output

    For each test case, output the total number of minimum operations for all pairs of arrays (𝑐𝑖,𝑏)

    .

    Example

    Input

    Copy

     

    4

    2 4

    1

    3 2

    4 7

    5 1 5

    3 8 3 3

    8 4

    4 3 3 2 2 1 1

    1 1 1 1 3 3 3 3

    9 1

    9 2 8 3 7 4 6 5

    1 2 3 2 1 4 5 6 5

    Output

    Copy

    2
    12
    16
    4
    

    Note

    In the first test case:

    • For the pair of arrays ([1,1],[3,2])
    , the answer is 0
  • . No operations or reordering of elements are needed.
  • For the pair of arrays ([2,1],[3,2])
  • , the answer is 0. The elements of the first array can be rearranged to obtain [1,2)
  • . No operations are needed.
  • For the pair of arrays ([3,1],[3,2])
  • , the answer is 1. The element 3 can be removed from the first array and the element 2
  • can be removed from the second array.
  • For the pair of arrays ([4,1],[3,2])
  • , the answer is 1. The element 4 can be removed from the first array and the element 3
    • can be removed from the second array.

    思路:

        与easy问题相比就是m不一样,这个时候再暴力可能就会t了

        很容易想到用a数组的较小数抵消b数组的较小数,然后看剩下的是没办法抵消的,无论a1取多少都需要计算代价。然后贪心考虑b数组的剩下的最大数,当m大于等于这个最大数时,a1也需要删除,代价+1。

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"*****hear*****"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
// const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }
multiset<ll>l,r;
ll cnt=0;
void solve()
{
  int n, m;
    cin >> n >> m;
    vector<int> a(n - 1), b(n);
    vector<bool> st(n);
    for(int i = 0; i < n - 1; i ++ ) cin >> a[i];
    for(int i = 0; i < n; i ++ ) cin >> b[i];
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int l = 0, r = 0;
    while(l < n - 1 && r < n) {
        while(r < n && a[l] >= b[r]) r ++;
        if(r >= n) break;
        st[r] = 1;
        l ++, r ++;
    }
    int c1, c2 = 0, cnt = n - l - 1;
    for(int i = n - 1; i >= 0; i -- )
        if(!st[i]) {
            c1 = min(b[i] - 1, m);
            c2 = max(0, m - b[i] + 1);
            break;
        }
    cout << (ll)c1 * cnt + (ll)c2 * (cnt + 1) << endl;; 
}


int main()
{
   IOS;
   ll _;
    _=1;
    //scanf("%lld",&_);
    cin>>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}



 

vp下来,D题让我其实有点手足无措,当时想着树状数组,死活想不出来,因为之前做过一次贴海报的题目,所以就感觉这题也很像,直接思维定式了。

F题没做出来也很可惜,把G1写完之后就静不下心来看F,有点浮躁了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值