AIM Tech Round 4 (Div. 2)ABCD

A. Diversity
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters, or print that it is impossible.

String s consists only of lowercase Latin letters, and it is allowed to change characters only to lowercase Latin letters too.

Input

First line of input contains string s, consisting only of lowercase Latin letters (1 ≤ |s| ≤ 1000, |s| denotes the length of s).

Second line of input contains integer k (1 ≤ k ≤ 26).

Output

Print single line with a minimum number of necessary changes, or the word «impossible» (without quotes) if it is impossible.

Examples
input
yandex
6
output
0
input
yahoo
5
output
1
input
google
7
output
impossible
Note

In the first test case string contains 6 different letters, so we don't need to change anything.

In the second test case string contains 4 different letters: {'a', 'h', 'o', 'y'}. To get 5 different letters it is necessary to change one occurrence of 'o' to some letter, which doesn't occur in the string, for example, {'b'}.

In the third test case, it is impossible to make 7 different letters because the length of the string is 6.

 题意:给你一个字符串,问你至少要改变多少个字符才能有k个不同字符?

题解:先看字符串长度是否有k个,然后用一个vis数组标记有多少个不用字符出现(tmp),然后如果k>tmp的话ans=k-tmp,否则为0;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int maxn=1e3+5;
const ll inf=0x4f4f4f4f4f;
int n,k;
char b[maxn];
int tmp;
bool vis[26]; 
int main()
{
    scanf("%s %d",b,&k);
    int len=strlen(b);
    if(len<k)
    {
        puts("impossible");return 0;
    }
    for(int i=0;i<len;i++)
    {
        int t=b[i]-'a';
        if(!vis[t])vis[t]=true,tmp++;
    }
    if(k>tmp)
    printf("%d
",k-tmp);
    else
    printf("0
");
    return 0;    
}
View Code
B. Rectangles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n × m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that:

  1. All cells in a set have the same color.
  2. Every two cells in a set share row or column.
Input

The first line of input contains integers n and m (1 ≤ n, m ≤ 50) — the number of rows and the number of columns correspondingly.

The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black.

Output

Output single integer  — the number of non-empty sets from the problem description.

Examples
input
1 1
0
output
1
input
2 3
1 0 1
0 1 0
output
8
Note

In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets.

题解:先从n行上算,后再同理从m列上算,每行算出白的个数tmp,黑的个数就是n-tmp,从中tmp挑出1~tmp个的和是2^tmp-1,但这样挑出一个的会算两次,最后把结果再减去n*m就可以了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int maxn=1e3+5;
const ll inf=0x4f4f4f4f4f;
int n,m,k;
int a[55][55];
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0); 
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
        } 
    }
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        int tmp=0;
        for(int j=0;j<m;j++)
        {
            if(a[i][j])tmp++;
        }
        ans=ans+pow(2.,tmp)-1+pow(2.,m-tmp)-1; 
    }
        for(int i=0;i<m;i++)
    {
        int tmp=0;
        for(int j=0;j<n;j++)
        {
            if(a[j][i])tmp++;
        }
        ans=ans+pow(2.,tmp)-1+pow(2.,n-tmp)-1; 
    }
    cout<<ans-n*m<<endl;
    return 0;    
}
View Code
C. Sorting by Subsequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.

Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.

Every element of the sequence must appear in exactly one subsequence.

Input

The first line of input data contains integer n (1 ≤ n ≤ 105) — the length of the sequence.

The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.

Output

In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.

In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci ≤ n), then ci integers l1, l2, ..., lci (1 ≤ lj ≤ n) — indices of these elements in the original sequence.

Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.

If there are several possible answers, print any of them.

Examples
input
6
3 2 1 6 5 4
output
4
2 1 3
1 2
2 4 6
1 5
input
6
83 -75 -49 11 37 62
output
1
6 1 2 3 4 5 6
Note

In the first sample output:

After sorting the first subsequence we will get sequence 1 2 3 6 5 4.

Sorting the second subsequence changes nothing.

After sorting the third subsequence we will get sequence 1 2 3 4 5 6.

Sorting the last subsequence changes nothing.

 题意:就是把一个序列分成尽可能多的子序列,使得每子序列排好序之后,整个序列也是拍好序的

题解:把数组储存再a[]和b[]中,后再把b[]排好序找到每个数应该在的位置,再从a[]开始dfs()找调换的位置,调换好的标记,用vector<int>储存答案就好了,具体dfs()过程看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
#include<vector>
#define pb push_back
#define ll long long
using namespace std;
const int maxn=1e5+5;
int n,m,tmp;
int a[maxn];
int b[maxn];
map<int,int>mp;
map<int,bool>mp2;
vector<int> ans[maxn];
void dfs(int x,int p)
{
    mp2[x]=true;
    if(mp[x]==p)
    {
        ans[tmp++].pb(p); 
        return;
    }
    ans[tmp].pb(p); 
     if(a[mp[x]]==b[p])
    {
        ans[tmp++].pb(mp[x]);
        mp2[b[p]]=true;
        return ;
    }
    if(!mp2[a[mp[x]]])
    dfs(a[mp[x]],mp[x]);
    else
    {
        tmp++;
        return ;
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0); 
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++)
    {
        mp[b[i]]=i;
        mp2[b[i]]=false;
    }
    for(int i=1;i<=n;i++)
    {
        if(!mp2[a[i]])
        {
            dfs(a[i],i);
        }
    }
    cout<<tmp<<endl;
    for(int i=0;i<tmp;i++)
    {
        cout<<ans[i].size()<<' ';
        for(int j=0;j<ans[i].size();j++)
        {
            cout<<ans[i][j]<<' ';
        }
        cout<<'
';
    }
    return 0;    
}
D. Interactive LowerBound
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.

More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠  - 1, then valuenexti > valuei.

You are given the number of elements in the list n, the index of the first element start, and the integer x.

You can make up to 2000 queries of the following two types:

  • ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
  • ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.

Write a program that solves this problem.

Input

The first line contains three integers nstartx (1 ≤ n ≤ 50000, 1 ≤ start ≤ n0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.

Output

To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.

Interaction

To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.

After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109,  - 1 ≤ nexti ≤ nnexti ≠ 0).

It is guaranteed that if nexti ≠  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.

Note that you can't ask more than 1999 queries of the type ?.

If nexti =  - 1 and valuei =  - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.

Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.

To flush you can use (just after printing a query and line end):

  • fflush(stdout) in C++;
  • System.out.flush() in Java;
  • stdout.flush() in Python;
  • flush(output) in Pascal;
  • For other languages see documentation.

Hacks format

For hacks, use the following format:

In the first line print three integers nstartx (1 ≤ n ≤ 50000, 1 ≤ start ≤ n0 ≤ x ≤ 109).

In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109,  - 1 ≤ nexti ≤ nnexti ≠ 0).

The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.

Example
input
5 3 80
97 -1
58 5
16 2
81 1
79 4
output
? 1
? 2
? 3
? 4
? 5
! 81
Note

You can read more about singly linked list by the following link: https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list

The illustration for the first sample case. Start and finish elements are marked dark.

 题意:交互题,给你一个单向列表这(单调递增)的长度和起始下标,以及一个数x,然后你可以询问不超过2000次某个下标的的值和他下个数的下标,让你找到这这个列表中>=x的最小值

题解:生成一个随机数组,询问前一千个找到其中小于x的最大值和下标,然后再去一个一个询问找到>=x的值,找不到就为-1

srand(time(NULL));random_shuffle(s.begin(),s.end());这两句可以把有序的数组变成随机数组
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
const int maxn=1e5+5;
int n,m,st;
vector<int>s;
int main()
{    
    srand(time(NULL));
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0); 
    cin>>n>>st>>m; 
    int ans=0,p=st;
    for(int i=1;i<=n;i++)
    {
        s.pb(i);
    }
    random_shuffle(s.begin(),s.end());
    for(int i=0;i<min(n,1000);i++)
    {
        int v,next;
        cout<<"? "<<s[i]<<endl;
        cout.flush();
        cin>>v>>next;
        if(ans<v&&v<=m)
        {
            ans=v;p=next;
         }     
    }
    while(p!=-1&&ans<m)
    {
        cout<<"? "<<p<<endl;
        cout.flush();
        cin>>ans>>p;
    }
    if(ans<m)
    {
        cout<<"! "<<-1<<endl;
    }
    else
    {
        cout<<"! "<<ans<<endl;
    }
    cout.flush();
    return 0;
}
View Code
 
原文地址:https://www.cnblogs.com/lhclqslove/p/7443483.html