Codeforces Round #697 (Div. 3)——C~G

C. Ball in Berland

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.
Each class must present two couples to the ball. In Vasya’s class, 𝑎 boys and 𝑏 girls wish to participate. But not all boys and not all girls are ready to dance in pairs.
Formally, you know 𝑘 possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.
For example, if 𝑎=3, 𝑏=4, 𝑘=4 and the couples (1,2), (1,3), (2,2), (3,4) are ready to dance together (in each pair, the boy’s number comes first, then the girl’s number), then the following combinations of two pairs are possible (not all possible options are listed below):
(1,3) and (2,2); (3,4) and (1,3);
But the following combinations are not possible:
(1,3) and (1,2) — the first boy enters two pairs; (1,2) and (2,2) — the second girl enters two pairs;
Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs.

Input
The first line contains one integer 𝑡 (1≤𝑡≤104) — the number of test cases. Then 𝑡 test cases follow.
The first line of each test case contains three integers 𝑎, 𝑏 and 𝑘 (1≤𝑎,𝑏,𝑘≤2⋅105) — the number of boys and girls in the class and the number of couples ready to dance together.
The second line of each test case contains 𝑘 integers 𝑎1,𝑎2,…𝑎𝑘. (1≤𝑎𝑖≤𝑎), where 𝑎𝑖 is the number of the boy in the pair with the number 𝑖.
The third line of each test case contains 𝑘 integers 𝑏1,𝑏2,…𝑏𝑘. (1≤𝑏𝑖≤𝑏), where 𝑏𝑖 is the number of the girl in the pair with the number 𝑖.
It is guaranteed that the sums of 𝑎, 𝑏, and 𝑘 over all test cases do not exceed 2⋅105.
It is guaranteed that each pair is specified at most once in one test case.
Output
For each test case, on a separate line print one integer — the number of ways to choose two pairs that match the condition above.
Example
input
3
3 4 4
1 1 2 3
2 3 2 4
1 1 1
1
1
2 2 4
1 1 2 2
1 2 1 2
output
4
0
2
Note
In the first test case, the following combinations of pairs fit:
(1,2) and (3,4);
(1,3) and (2,2);
(1,3) and (3,4);
(2,2) and (3,4).
There is only one pair in the second test case.
In the third test case, the following combinations of pairs fit:
(1,1) and (2,2);
(1,2) and (2,1).

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int binBoy[200010];
int binGirl[200010];
int pairBoy[200010];
int pairGirl[200010];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    while (N--)
    {
        mem(binBoy);
        mem(binGirl);
        ll ans = 0;
        int a, b, k;
        cin >> a >> b >> k;
        for (int i = 0; i < k; i++)
        {
            cin >> pairBoy[i];
            binBoy[pairBoy[i]]++;
        }
        for (int i = 0; i < k; i++)
        {
            cin >> pairGirl[i];
            binGirl[pairGirl[i]]++;
        }
        for (int i = 0; i < k; i++)
        {
            int shengyuBoy = k - binBoy[pairBoy[i]];
            int shengyuGirl = k - binGirl[pairGirl[i]];
            ans += (shengyuBoy + shengyuGirl) - (k - 1);
        }
        ans /= 2;
        cout << ans << endl;
    }
    return 0;
}

D. Cleaning the Phone

Polycarp often uses his smartphone. He has already installed 𝑛 applications on it. Application with number 𝑖 takes up 𝑎𝑖 units of memory.
Polycarp wants to free at least 𝑚 units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer 𝑏𝑖 to each application:
𝑏𝑖=1 — regular application;
𝑏𝑖=2 — important application.
According to this rating system, his phone has 𝑏1+𝑏2+…+𝑏𝑛 convenience points.
Polycarp believes that if he removes applications with numbers 𝑖1,𝑖2,…,𝑖𝑘, then he will free 𝑎𝑖1+𝑎𝑖2+…+𝑎𝑖𝑘 units of memory and lose 𝑏𝑖1+𝑏𝑖2+…+𝑏𝑖𝑘 convenience points.
For example, if 𝑛=5, 𝑚=7, 𝑎=[5,3,2,1,4], 𝑏=[2,1,1,2,1], then Polycarp can uninstall the following application sets (not all options are listed below):
applications with numbers 1,4 and 5. In this case, it will free 𝑎1+𝑎4+𝑎5=10 units of memory and lose 𝑏1+𝑏4+𝑏5=5 convenience points;
applications with numbers 1 and 3. In this case, it will free 𝑎1+𝑎3=7 units of memory and lose 𝑏1+𝑏3=3 convenience points.
applications with numbers 2 and 5. In this case, it will free 𝑎2+𝑎5=7 memory units and lose 𝑏2+𝑏5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least 𝑚 units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer 𝑡 (1≤𝑡≤104) — the number of test cases. Then 𝑡 test cases follow.
The first line of each test case contains two integers 𝑛 and 𝑚 (1≤𝑛≤2⋅105, 1≤𝑚≤109) — the number of applications on Polycarp’s phone and the number of memory units to be freed.
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) — the number of memory units used by applications.
The third line of each test case contains 𝑛 integers 𝑏1,𝑏2,…,𝑏𝑛 (1≤𝑏𝑖≤2) — the convenience points of each application.
It is guaranteed that the sum of 𝑛 over all test cases does not exceed 2⋅105.
Output
For each test case, output on a separate line:
-1, if there is no set of applications, removing which will free at least 𝑚 units of memory;
the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
output
2
-1
6
4
3

题解:
有点像01背包问题,但由于n的范围太大只好换枚举法(贪心)。
还用到二分和前缀和。
当时没有想到用前缀和。

AC代码:

#include <string.h>
#include <math.h>
#include<algorithm>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef long long ll;

int main()
{
    cin.tie(0);std::ios::sync_with_stdio(false);
    ll t;
    cin>>t;
    for(; t>0; t--){
       ll n, tot1=0, tot2=0, m, sum=0;
       cin>>n>>m;
       ll tag[n+10], a[n+10], b1[n+10], b2[n+10];
       //一定要清空初始化为0,不然最后的运算可能会出错。找了好久的一个bug。
       memset(tag, 0, sizeof(tag));
       memset(a, 0, sizeof(a));
       memset(b1, 0, sizeof(b1));
       memset(b2, 0, sizeof(b2));
       for(ll i=1; i<=n; i++) { cin>>a[i]; sum+=a[i]; }
       for(ll i=1; i<=n; i++) { 
           cin>>tag[i]; 
           if( tag[i]==1 ) { b1[++tot1]=a[i]; }
           else { b2[++tot2]=a[i]; }
        }
       if(sum<m) { cout<<"-1"<<endl; continue; }
       //这里的tot1、tot2以及b1[0]=0,b2[0]=0是借鉴了别人写法,真的好简便
       sort(b1+1, b1+tot1+1, greater<ll>() );
       sort(b2+1, b2+tot2+1, greater<ll>() );
       for(ll i=1; i<=tot1; i++) b1[i]+=b1[i-1];
       for(ll i=1; i<=tot2; i++) b2[i]+=b2[i-1];
       ll ans=1e18;
       for(ll i=0; i<=tot1; i++){
           ll tem=m-b1[i];
           ll p=lower_bound(b2+1, b2+tot2+1, tem)-b2;
           if( b1[i]+b2[p]>=m ) ans=min( ans, i+p*2 );
           if( b1[i]+b2[--p]>=m ) ans=min( ans, i+p*2 );
        }
        cout<<ans<<endl;
    }
    return 0;
}

E. Advertising Agency

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of 𝑛 different bloggers. Blogger numbered 𝑖 has 𝑎𝑖 followers.
Since Masha has a limited budget, she can only sign a contract with 𝑘 different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select 𝑘 bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if 𝑛=4, 𝑘=3, 𝑎=[1,3,1,2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to 𝑎1+𝑎2+𝑎4=6.
conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to 𝑎2+𝑎3+𝑎4=6.
Since the answer can be quite large, output it modulo 109+7.
Input
The first line contains one integer 𝑡 (1≤𝑡≤1000) — the number of test cases. Then 𝑡 test cases follow.
The first line of each test case contains two integers 𝑛 and 𝑘 (1≤𝑘≤𝑛≤1000) — the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…𝑎𝑛 (1≤𝑎𝑖≤𝑛) — the number of followers of each blogger.
It is guaranteed that the sum of 𝑛 over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer — the number of ways to select 𝑘 bloggers so that the total number of their followers is maximum possible.
Example
input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to 𝑎1+𝑎2=2;
conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to 𝑎1+𝑎3=2;
conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to 𝑎1+𝑎4=2;
conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to 𝑎2+𝑎3=2;
conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to 𝑎2+𝑎4=2;
conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to 𝑎3+𝑎4=2.
In the third test case, the following ways are valid:
concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to 𝑎2=2.

分析:
将数组从小到大排列后,从后往前取k位即为答案的一种情况。数组第n-k位可能与前后存在并列情况,可用排列组合。
对于我难点——排列组合代码…

代码:

#include <string.h>
#include <string>
#include <math.h>
#include<algorithm>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll cd[1010][1010] = {0};
ll C(int a, int b)
{

    if (a == b)
        return 1;
    //C(a,b)=c(a-1,b-1)+c(a-1,b)
    if (a < b)
        return 0;
    if (a == 0)
        return 0;
    if (b == 0)
        return 1;
    b = min(b, a - b);

    if (!cd[a - 1][b - 1])
        cd[a - 1][b - 1] = C(a - 1, b - 1);
    if (!cd[a - 1][b])
        cd[a - 1][b] = C(a - 1, b);
    return (cd[a - 1][b - 1] + cd[a - 1][b]) % mod;
}
int main(){
	int t; 
    cin>>t;
    while(t--){
        int n,k,num=1,num1=1;
        cin>>n>>k;
        int a[n+5];
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        sort(a,a+n);
        for(int i=n-k;a[i]==a[i-1] && i>0;i--) num++;
        for(int i=n-k;a[i]==a[i+1] && i<n-1;i++) { num1++; num++; }
        printf("%lld\n",C(num,num1));
    }
	return 0;
} 

F. Unusual Matrix

You are given two binary square matrices 𝑎 and 𝑏 of size 𝑛×𝑛. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix 𝑎 arbitrary number of times (0 or more):
vertical xor. You choose the number 𝑗 (1≤𝑗≤𝑛) and for all 𝑖 (1≤𝑖≤𝑛) do the following: 𝑎𝑖,𝑗:=𝑎𝑖,𝑗⊕1 (⊕ — is the operation xor (exclusive or)).
horizontal xor. You choose the number 𝑖 (1≤𝑖≤𝑛) and for all 𝑗 (1≤𝑗≤𝑛) do the following: 𝑎𝑖,𝑗:=𝑎𝑖,𝑗⊕1.
Note that the elements of the 𝑎 matrix change after each operation.
在这里插入图片描述Check if there is a sequence of operations such that the matrix 𝑎 becomes equal to the matrix 𝑏.
Input
The first line contains one integer 𝑡 (1≤𝑡≤1000) — the number of test cases. Then 𝑡 test cases follow.
The first line of each test case contains one integer 𝑛 (1≤𝑛≤1000) — the size of the matrices.
The following 𝑛 lines contain strings of length 𝑛, consisting of the characters ‘0’ and ‘1’ — the description of the matrix 𝑎.
An empty line follows.
The following 𝑛 lines contain strings of length 𝑛, consisting of the characters ‘0’ and ‘1’ — the description of the matrix 𝑏.
It is guaranteed that the sum of 𝑛 over all test cases does not exceed 1000.
Output
For each test case, output on a separate line:
“YES”, there is such a sequence of operations that the matrix 𝑎 becomes equal to the matrix 𝑏;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
input
3
3
110
001
110

000
000
000
3
101
010
101

010
101
010
2
01
11

10
10
output
YES
YES
NO

题解:
与题七夕祭类似。
分析可得:
1、最终的结果和我们异或的顺序是没有关系的
2、异或两次相当于没有进行异或操作
注意,字符型与整形的转化,其实不需要转化,有个技巧。

AC代码:

#include <string.h>
#include <string>
#include <math.h>
#include<algorithm>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1010;
int n;      
char a[maxn][maxn], b[maxn][maxn];
void turn(int x, int y){
    //列变换
    if(y==0){
        for(int i=0; i<n; i++)  a[i][x]=(a[i][x]^'1')+'0';
    }
    //注意由于优先级,^运算一定要加括号!
    //行变换
    else {
        for(int i=0; i<n; i++)  a[x][i]=(a[x][i]^'1')+'0';
    }
}

void work(){
    if(n==1) { cout<<"YES"<<endl; return; }
    for(int i=0; i<n; i++){
        if( a[1][i]!=b[1][i] )  turn(i, 0);
    }
    for(int i=0; i<n; i++){
        if( a[i][1]!=b[i][1] )  turn(i, 1);
    }
    bool ok=1;
    for(int i=0; i<n && ok==1; i++)
        for(int j=0; j<n && ok==1; j++)
            if( a[i][j]!=b[i][j] ) { ok=0; break; }
    if(ok) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}

int main()
{
    cin.tie(0);std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        memset(a, '\0', sizeof(a));
        memset(b, '\0', sizeof(b));
        for(int i=0; i<n; i++) cin>>a[i];
        string tem;
        getline(cin, tem);
        for(int i=0; i<n; i++) cin>>b[i];
        work();
    }
    return 0;
}

G. Strange Beauty

Polycarp invented his criterion for the beauty of an array. He calls an array 𝑎 beautiful if at least one of the following conditions must be met for each different pair of indices 𝑖≠𝑗:
𝑎𝑖 is divisible by 𝑎𝑗;
or 𝑎𝑗 is divisible by 𝑎𝑖.
For example, if:
𝑛=5 and 𝑎=[7,9,3,14,63], then the 𝑎 array is not beautiful (for 𝑖=4 and 𝑗=2, none of the conditions above is met);
𝑛=3 and 𝑎=[2,14,42], then the 𝑎 array is beautiful;
𝑛=4 and 𝑎=[45,9,3,18], then the 𝑎 array is not beautiful (for 𝑖=1 and 𝑗=4 none of the conditions above is met);
Ugly arrays upset Polycarp, so he wants to remove some elements from the array 𝑎 so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array 𝑎 beautiful.
Input
The first line contains one integer 𝑡 (1≤𝑡≤10) — the number of test cases. Then 𝑡 test cases follow.
The first line of each test case contains one integer 𝑛 (1≤𝑛≤2⋅105) — the length of the array 𝑎.
The second line of each test case contains 𝑛 numbers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤2⋅105) — elements of the array 𝑎.
Output
For each test case output one integer — the minimum number of elements that must be removed to make the array 𝑎 beautiful.
Example
input
4
5
7 9 3 14 63
3
2 14 42
4
45 9 3 18
3
2 2 8
output
2
0
1
0

题解:
思路dp,代码应该很好理解

AC代码:

#include <string.h>
#include <math.h>
#include<algorithm>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=2*1e5+10;
int main()
{
    cin.tie(0);std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        int n, ans=0;
        cin>>n;
        int a[maxn], di[maxn], f[maxn];
        memset(f, 0, sizeof(f));
        memset(di, 0, sizeof(di));
        for(int i=1; i<=n; i++) cin>>a[i];
        sort( a+1, a+1+n );
        for(int i=1; i<=n; i++) {
            f[i]=1;
            for(int j=1; j*j<=a[i]; j++){
                if( a[i]%j ) continue;
                int tem=a[i]/j;
                f[i]=max( f[i], f[di[j]]+1 );
                f[i]=max( f[i], f[di[tem]]+1 );
            }
            di[a[i]]=i;
            ans=max( ans, f[i] );
        }
        cout<<n-ans<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值