Codeforces Round #673 (Div. 2) AC (thinking, thinking, thinking + greed + holistic view)

Codeforces Round #673 (Div. 2)

A. Copy-paste

Title:

You have n piles of candy, each pile has a certain number of candies, you can magically copy any pile of candies to another pile (1. can not be copied to yourself 2. the number of candies in each pile cannot be More than k) Find the maximum number of times that can be copied

problem analysis:

It must be to find the smallest pile, and then copy this pile to other candy piles until it does not exceed the maximum value of k.
I have sorted it again.
Otherwise, I need to record the position of the minimum value, because the minimum value Conan is not unique.

AC code:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;
#define ll long long
#define mem(a,b) memset((a),(b),sizeof(a));
#define lowbit(a) ((a)&-(a))
const ll inf=0x3f3f3f3f;//1061109567,2*未超int,allinf=mem(a,0x3f,sizeof(a));
const int N=3e5+10;
int  a[1005];
int main(){
    
    
       //#define io
#ifdef io
        freopen("in.txt","r",stdin);
#endif
        cin.tie(0);
        int t;
        cin>>t;
        while(t--){
    
    
                int n,k;
                cin>>n>>k;
                for(int i=0;i<n;i++){
    
    
                        cin>>a[i];
                }
                sort(a,a+n);
                int sum=0;
                for(int i=n-1;i>=1;i--){
    
    
                        if(a[i]<k)
                        sum+=(k-a[i])/a[0];                    
                }
                cout<<sum<<endl;
        }
        return 0;
}

B. Two Arrays

Title:

There is an array of n numbers, given the number T,
now you are required to divide this array into 2 arrays
such that the sum of any two numbers in each array equals the smallest number of groups of T
insert image description here

problem analysis:

At first, I thought of using map to create a map.
Then I thought about it. In fact, according to T, there is only one group before
T/2. When T is an even number, T/2 crosses into the first group and the second group
. There is a kind of overall situation. view. . . (Bullshit? . . The next question also feels the same way)

AC code:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;
#define ll long long
#define mem(a,b) memset((a),(b),sizeof(a));
#define lowbit(a) ((a)&-(a))
const ll inf=0x3f3f3f3f;//1061109567,2*未超int,allinf=mem(a,0x3f,sizeof(a));
const int N=1e6+10;
long long  a[N];
int main(){
    
    
       //#define io
#ifdef io
        freopen("in.txt","r",stdin);
#endif
        cin.tie(0);
        int t;
        cin>>t;
        while(t--){
    
    
                long long  n,m;
                cin>>n>>m;
                int flag=0;
                for(int i=0;i<n;i++){
    
    
                        cin>>a[i];
                }
                for(int i=0;i<n;i++){
    
    
                        if(a[i]<(m+1)/2){
    
    
                                cout<<1<<" ";
                        }else if(a[i]>m/2){
    
    
                                cout<<0<<" ";
                        }else{
    
    
                                flag++;//交叉为1/2组————T为偶数时
                                if(flag%2)cout<<1<<" ";
                                else cout<<0<<" ";
                        }              
                }
                cout<<endl;
        }
        return 0;
}

C. k-Amazing Numbers

Title:

Give you an array of n numbers, where the number in the array does not exceed n and
let you find 1, 2, respectively. . . n is the common minimum value of the sub-array of length,
if not, output -1
insert image description here

for example

4  4  2
1长度: 4  4  2 没有公共
2长度:44  42 公共最小为4
3长度:442 公共最小为2 

problem analysis:

It's full of violence. . . . .
The actual idea is to count the maximum interval of the same number (first temporarily understand
nv[i]_back() in the following code to explain)
and then take the smallest number in the same interval.
See the code for details.

AC code:

#include<iostream>
#include<cstdio>

#include<cstring>
#include<bitset>
#include<sstream>
#include<string.h>
#include<iomanip>

#include<cmath>
#include<algorithm>
#include<cstdlib>

#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long 
#define lowbit(x) (x)&(-x)
#define mem(a,b) memset((a),(b),sizeof(a));
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
const ll inf=0x3f3f3f3f;//1061109567,2*未超int,allinf=mem(a,0x3f,sizeof(a));
typedef pair<int ,int > PII;
const int  nn=3e5+10;
vector<int> v[nn];//记录相同数字的在数组中的下标
int ans[nn];//统计间隔最小数字数组
int main()
{
    
    
    //#define io
    #ifdef io
        freopen("in.txt","r",stdin);
    #endif
    int t,n;
    cin>>t;
    while(t--){
    
    
        cin>>n;
        forr(i,0,n+1){
    
    
            ans[i]=inf;//最大值因为有 无公共数字即 -1情况
            v[i].clear();
        }
        forr(i,1,n){
    
    
            int x;cin>>x;
            v[x].push_back(i);记录相同数字的在数组中的下标
        }
        forr(i,1,n){
    
    
            if(!v[i].empty()){
    
    
                int md=v[i].front();//相当于到1的间隔
                forr(j,1,v[i].size()-1){
    
    
                //v[i]是从v[i][0]开始的哈这里1是第二项
                    md=max(md,v[i][j]-v[i][j-1]);
                    //统计出现相同数字的最大间隔
                }
                md=max(md,n-v[i].back()+1);
                //统计出现相同数字的最大间隔
                //这里是最后一个数字到 这个数字的间隔 
                //这里为什么要+1呢 
  /*
我们来看一下实际前面求的间隔是一段中有某个数字的长度 所以 不需要
两个数子都在 同一段 样例: 1 0 0 0 1 0 0 0 
但是这个 n-v[i]_back()+1确是必须要的 因为 n不是这同一个数字 所以必须要把他的长度算上去  样例:1 0 0 0 1 0 0 0 0 

*/                                                                                                                                          
                ans[md]=min(ans[md],i);
            }
        }
        forr(i,2,n)ans[i]=min(ans[i],ans[i-1]);
        forr(i,1,n){
    
    
            if(ans[i]==inf)cout<<-1<<" ";
            else cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325788760&siteId=291194637