山东第九届省赛

 A       Anagram

链接:https://www.nowcoder.com/acm/contest/123/A
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

    Orz has two strings of the same length: A and B. Now she wants to transform A into an anagram of B (which means, a rearrangement of B) by changing some of its letters. The only operation the girl can make is to “increase” some (possibly none or all) characters in A. E.g., she can change an ‘ A’ to a ‘ B’, or a ‘ K’ to an ‘ L’. She can increase any character any times. E.g., she can increment an ‘ A’ three times to get a ‘ D’. The increment is cyclic: if she increases a ‘ Z’, she gets an ‘ A’ again.

    For example, she can transform “ELLY” to “KRIS” character by character by shifting ‘E’ to ‘K’ (6 operations), ‘L’ to ‘R’ (again 6 operations), the second ‘L’ to ‘I’ (23 operations, going from ‘Z’ to ‘A’ on the 15-th operation), and finally ‘Y’ to ‘S’ (20 operations, again cyclically going from ‘Z’ to ‘A’ on the 2-nd operation). The total number of operations would be 6 + 6 + 23 + 20 = 55. However, to make “ELLY” an anagram of “KRIS” it would be better to change it to “IRSK” with only 29 operations. You are given the strings A and B. Find the minimal number of operations needed to transform A into some other string X, such that X is an anagram of B.

输入描述:

There will be multiple test cases. For each testcase:

There is two strings A and B in one line.∣A∣=∣B∣≤50. A and B will contain only uppercase letters
from the English alphabet (‘A’-‘Z’).

输出描述:

For each test case, output the minimal number of
operations.
示例1

输入

ABCA BACA
ELLY KRIS
AAAA ZZZZ

输出

0
29
100

当时想了好久,想明白就好了,就是贪心。

代码:

#include<bits/stdc++.h>
using namespace std;
int main() {
  char a[55], b[55];
  while(~scanf("%s%s", a, b)) {
    int len = strlen(a), vis[55];
    long long sum = 0;
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < len; i++) {
      int minn = 30, num;
      for(int j = 0; j < len; j++) {
        if(vis[j]) continue;
        int temp;
        if(b[j] >= a[i]) temp = b[j] - a[i];
        else
          temp = (26 - (a[i] - b[j]));
        if(temp < minn) {
          minn = temp;
          num = j;
        }
      }
      sum += minn;
      vis[num] = 1;
    }
    printf("%lld\n", sum);
  }
  return 0;
}

E       Sequence

链接:https://www.nowcoder.com/acm/contest/123/E
来源:牛客网

时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

We define an element  in a sequence "good", if and only if there exists  (1≤j<i) such that .

Given a permutation p of integers from 1 to n. Remove an element from the permutation such that the number of "good" elements is maximized.

输入描述:

The input consists of several test cases. The first
line of the input gives the number of test cases,.

For each test case, the first line contains an
integer ,
representing the length of the given permutation.

The second line contains n integersp1,p2,…,pn ,
representing the given permutation p.

It's guaranteed that  .

输出描述:

For each test case, output one integer in a single
line, representing the element that should be deleted. If there are several
answers, output the minimal one.
示例1

输入

2
1
1
5
5 1 2 3 4

输出

1
5

思路:首先要明白,一个不好数,它总是它之前(包括它)出现的所有数中最小的;
考虑删除一个好数还是不好数:
删除一个好数,则总的好数数量将减少一个(因为删除它后能影响到的好数仅有它自己);

删除一个不好数,考虑删除这个不好数后能影响到的好数有几个,那么总的好数数量就减少几个;(被它所影响到的好数(假设目前考虑的好数是a[i])是那些满足 最小{a[0~1-i]}<a[i]<=次小{a[0~n-1]} 的数)(转自:点击打开链接

代码:

#include <iostream>
#include<cstdio>
#include<algorithm>
typedef long long ll;
#define inf 0x3f3f3f3f
using namespace std;
const int M =1000010;
ll a[M],first[M],second[M];
bool flag[M];
ll cnt[M];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        ll n;
        scanf("%lld",&n);
        ll now_min=inf;
        ll pre_min=inf;
        for(ll i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            first[i]=now_min;
            second[i]=pre_min;
            if(a[i]<=now_min)
            {
                pre_min=now_min; now_min=a[i];
            }
            else
            {
                if(a[i]<pre_min) pre_min=a[i];
            }
        }
        for(ll i=1;i<=n;i++)
        {
            if(a[i]<=first[i]) flag[i]=1;
            else flag[i]=0;
        }
        ll k;
        for(ll i=1;i<=n;i++)
        {
            if(flag[i]==1)
            {
                k=i;
                cnt[k]=0;
            }
            else
            {
                if(a[i]<=second[i]) cnt[k]++;
            }
        }
        ll ans=inf;
        for(ll i=1;i<=n;i++)
        {
            if(flag[i]==1&&cnt[i]==0) ans=min(ans,a[i]);
        }
        if(ans==inf) 
        {
            for(ll i=1;i<=n;i++)
            {
              if(flag[i]==0) ans=min(ans,a[i]);
              else if(cnt[i]==1) ans=min(ans,a[i]);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 F   Four-tuples

链接:https://www.nowcoder.com/acm/contest/123/F
来源:牛客网

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

Given    ,please count the number of four-tuples  such that and   
The answer should modulo 10 9 +7 before output.

输入描述:

The input consists of several test cases. The first
line gives the number of test cases,T(1≤T≤106).

For each test case, the input contains one line
with 8 integers,.

输出描述:

For each test case, output one line containing one
integer, representing the answer.
示例1

输入

1
1 1 2 2 3 3 4 4

输出

1

思路:

容次原理,一辈子不会忘记的题....

代码:

#include <bits/stdc++.h>
  
using namespace std;
  
const int mod=1e9+7;
typedef  long long ll;
ll Query(ll l1,ll r1,ll l2,ll r2)
{
    ll l=max(l1,l2),r=min(r1,r2);
    return r-l+1>0?r-l+1:0;
}
ll Query2(ll l1,ll r1,ll l2,ll r2,ll l3,ll r3)
{
    ll l=max(l1,max(l2,l3)),r=min(r1,min(r2,r3));
    return r-l+1>0?r-l+1:0;
}
ll Query3(ll l1,ll r1,ll l2,ll r2,ll l3,ll r3,ll l4,ll r4)
{
    ll l=max(max(l1,l2),max(l3,l4)),r=min(min(r1,r2),min(r3,r4));
    return r-l+1>0?r-l+1:0;
}
int main()
{
    int t;
    ll l1,r1,l2,r2,l3,r3,l4,r4;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%lld%lld%lld%lld%lld%lld",&l1,&r1,&l2,&r2,&l3,&r3,&l4,&r4);
        ll sum=1;
        sum=sum*(r1-l1+1)%mod;
        sum=sum*(r2-l2+1)%mod;
        sum=sum*(r3-l3+1)%mod;
        sum=sum*(r4-l4+1)%mod;
        ll ant=0;
        ant=(ant+Query(l1,r1,l2,r2)%mod*(r3-l3+1)%mod*(r4-l4+1)%mod)%mod;
        ant=(ant+Query(l2,r2,l3,r3)%mod*(r1-l1+1)%mod*(r4-l4+1)%mod)%mod;
        ant=(ant+Query(l3,r3,l4,r4)%mod*(r1-l1+1)%mod*(r2-l2+1)%mod)%mod;
        ant=(ant+Query(l4,r4,l1,r1)%mod*(r3-l3+1)%mod*(r2-l2+1)%mod)%mod;
  
        ant=(ant-Query2(l1,r1,l2,r2,l3,r3)*(r4-l4+1)%mod+mod)%mod;
        ant=(ant-Query(l1,r1,l2,r2)*Query(l3,r3,l4,r4)%mod+mod)%mod;
        ant=(ant-Query2(l1,r1,l2,r2,l4,r4)*(r3-l3+1)%mod+mod)%mod;
        ant=(ant-Query2(l2,r2,l3,r3,l4,r4)*(r1-l1+1)%mod+mod)%mod;
        ant=(ant-Query(l2,r2,l3,r3)*Query(l1,r1,l4,r4)%mod+mod)%mod;
        ant=(ant-Query2(l1,r1,l3,r3,l4,r4)*(r2-l2+1)%mod+mod)%mod;
  
        ant=(ant+3*Query3(l1,r1,l2,r2,l3,r3,l4,r4)%mod+mod)%mod;
        printf("%lld\n",(sum-ant+mod)%mod);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_37668729/article/details/80377100