Codeforces Round 905 (Div. 3)

news2024/9/26 1:23:58

目录

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,有点浮躁了

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1151623.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

重磅新闻-国内首家八类网线认证分析仪上市了

伴随USA对国内某些敏感企业的非常不友好&#xff0c;设置层层障碍&#xff0c;技术堡垒。使得一些网线基础制造研发、线缆线束厂、汽车生产生产厂、军工用途的线缆品质的认证、以及相关高校的研发受到了不同的程度的阻碍。重磅消息&#xff0c;国内首家八类网线认证测仪-维信仪…

Vue前后端分离的低代码开发框架

目录 项目简介 平台特性 1.构架特性 2.功能特性 技术栈 1.后端技术栈 2.前端技术栈 2.1 Vue2技术栈 2.2 Vue3技术栈 3.数据库支持 部署方式 项目简介 JNPF开发平台是一个基于SpringBootVue3的全栈开发平台&#xff0c;采用微服务、前后端分离架构。前后端封装了上千…

【多线程相关其一】Python并发编程

1.为什么要引入并发编程 场景1&#xff1a;一个网络爬虫&#xff0c;按顺序爬取花了1个小时&#xff0c;采用并发下载减少到20分钟。 场景2&#xff1a;一个APP应用&#xff0c;优化前每次打开页面需要3秒&#xff0c;采用异步并发提升到每次200毫秒 引入并发&#xff0c;就是…

Centos系统安装阿里云盘+简单使用

GitHub地址&#xff1a; 1、安装阿里云盘 wget https://github.com/tickstep/aliyunpan/releases/download/v0.2.7/aliyunpan-v0.2.7-linux-amd64.zip unzip aliyunpan-v0.2.7-linux-amd64.zip mv aliyunpan-v0.2.7-linux-amd64 /usr/local/aliyunpan ln -s /usr/local/aliyu…

安装Jdk 报错 ,Java SE Development Kit 8 Update 202(64-bit)安装完毕之前,向导被中断

具体原因没有找到&#xff0c;估计是由于jdk 没有删干净导致的&#xff0c;我的处理方法是&#xff0c;将 Java的注册表全然后手动安装 Jdk和导入注册表&#xff08;在同事那里获取jdk文件 压缩包&#xff0c;并将 java的注册表导出&#xff0c;放在自己电脑上使用。&#xff0…

动手学深度学习——第七次学

LeNet&#xff08;LeNet-5&#xff09;由两个部分组成&#xff1a; 卷积编码器和全连接层密集块 卷积把高宽不断变小&#xff0c;把通道数逐渐增多&#xff0c;&#xff08;最后高宽会变成&#xff0c;通道会变得很大&#xff0c;然后做全连接进行输出&#xff09;通道信息可以…

Leetcode—1488.避免洪水泛滥【中等】

2023每日刷题&#xff08;十四&#xff09; Leetcode—1488.避免洪水泛滥 算法思想 将晴天的日期全部记录在set<int> sun中使用unordered_map<int, int> lakeRainy来记录每个湖泊上一次下雨的日期遇到晴天时先不用管抽哪个湖当下雨时&#xff0c;湖泊已经装满水时…

QT5 通过 webview2 加载网页

官方文档参考&#xff1a;https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/get-started/win32 Webview2依赖的头文件和库 头文件主要为&#xff1a;WebView2和WixLibrary&#xff0c;存储在include/external 库主要为&#xff1a;WebView2LoaderStatic.lib和W…

C++——类和对象(构造函数与析构函数)

构造函数与析构函数 本章思维导图&#xff1a; 注&#xff1a;本章思维导图对应的Xmind文件和.png文件都已导入到”资料“中 1. 构造函数 以前&#xff0c;我们写一个Date类一般是这么写的&#xff1a; class Date { public :void Init(int year, int month, int day){_year…

Unity Animator cpu性能测试

测试案例&#xff1a; 场景中共有4000个物体&#xff0c;挂在40个animtor 上&#xff0c;每个Animator控制100个物体的动画。 使用工具&#xff1a; Unity Profiler. Unity 版本&#xff1a; unity 2019.4.40f1 测试环境&#xff1a; 手机 测试过程&#xff1a; 没有挂…

解读电力系统中的GPS北斗卫星同步时钟系统

随着电力系统的快速发展,变电站中的各类系统 &#xff1a;计算机监控系统、水情测报系统、视频监控系统 状态监测系统 生产信息管理系统等&#xff0c;各类装置&#xff1a;继电保护装置、故障录波装置、PMU装置、事件顺序记录SOE功能越来越强大&#xff0c;需要采集、记录的数…

CSS3背景样式

在CSS 2.1中&#xff0c;background属性的功能还无法满足设计的需求&#xff0c;为了方便设计师更灵活地设计需要的网页效果&#xff0c;CSS3在原有background基础上新增了一些功能属性&#xff0c;可以在同一个对象内叠加多个背景图像&#xff0c;可以改变背景图像的大小尺寸&…

LeetCode热题100 48.旋转图像

题目描述 给定一个 n n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。 你必须在 原地 旋转图像&#xff0c;这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。 示例 1&#xff1a; 输入&#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9…

2022年09月 Python(二级)真题解析#中国电子学会#全国青少年软件编程等级考试

Python等级考试&#xff08;1~6级&#xff09;全部真题・点这里 一、单选题&#xff08;共25题&#xff0c;每题2分&#xff0c;共50分&#xff09; 第1题 运行以下代码&#xff0c;结果输出的是&#xff1f;&#xff08; &#xff09; means[Thank,You] print(len(means))A…

Android开发知识学习——TCP / IP 协议族

文章目录 学习资源来自&#xff1a;扔物线TCP / IP 协议族TCP连接TCP 连接的建立与关闭TCP 连接的建立为什么要三次握手&#xff1f; TCP 连接的关闭为什么要四次挥手&#xff1f; 为什么要⻓连接&#xff1f; 常见面试题课后题 学习资源来自&#xff1a;扔物线 TCP / IP 协议…

潜力无限!深眸科技以工业视觉软硬件一体化解决方案深入应用场景

工业视觉作为智能制造的眼睛&#xff0c;在制造业各场景中具有广泛的应用前景&#xff0c;尤其是在检测、引导、定位、测量等方面应用需求不断提高。深眸科技为进一步巩固和加强技术领先优势&#xff0c;持续拓宽机器视觉技术的应用边界&#xff0c;通过先进的硬件设备和自研的…

86 最小栈

最小栈 题解1 STL大法好题解2 辅助最小栈&#xff08;直观&#xff0c;空间换时间&#xff09;题解3 不需要额外空间(!!!差值!!!) 设计一个支持 push &#xff0c;pop &#xff0c;top 操作&#xff0c;并能在常数时间内检索到最小元素的栈。 实现 MinStack 类: MinStack() 初…

支持「导入/导出」,应用迁移瞬间完成_三叠云

应用导入/导出 路径 应用管理 >> 创建新应用 功能简介 1. 应用程序支持「导出」功能&#xff0c;即将应用独立封装导出&#xff0c;用于三叠云系统内应用导入。 2. 应用程序支持「导入」功能&#xff0c;可以帮助用户将数据或文件导出到其他设备或应用程序中&#x…

思维导图在学习中的应用

思维导图在做学习中发挥着非常高效的作用&#xff0c;因为因为思维导图只有一页&#xff0c;所以需要准备和组织内容的时间会大大减少。并且思维导图的可视化的结构&#xff0c;可以让你瞬间将所有信息一览无余。而传统的笔记方法&#xff0c;会记录好几页。不仅信息冗杂&#…

【经典面试】87 字符串解码

字符串解码 题解1 递归(程序栈)——形式语言自动机(LL(1)) : O(S)另一种递归(直观) 题解2 2个栈(逆波兰式)1个栈(参考官方&#xff0c;但是不喜欢) 给定一个经过编码的字符串&#xff0c;返回它解码后的字符串。 编码规则为: k[encoded_string]&#xff0c;表示其中方括号内部的…