$CF\ 634\ (Div3)$

\(CF\ 634\ (Div3)\)

\(A.\)

Given \ (n \) , ask how many positive integers \ (a, \ b \) , such that \ (a + b = n \) and \ (a> b \)

If \ (n \) is even, then \ (ans = n / 2-1 \)

If \ (n \) is odd, then \ (ans = n / 2 \)

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
 
 
inline int read()
{
    char c = getchar();
    int ans = 0, f = 1;
    while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
    while(isdigit(c)) {ans = ans * 10 + c - '0'; c = getchar();}
    return ans * f;
}
 
int t, n;;
int main()
{
    t = read();
    while(t--) {
        n = read();
        int ans = 0;
        if(n % 2) ans = n / 2;
        else ans = n / 2 - 1;
        printf("%d\n", ans);
    }
    return 0;
}

\(B.\)

Construct a string of length \ (n \) so that each substring of length \ (a \) contains only \ (b \) different characters

Constructed as follows

According to the order before the first fill \ (b \) lowercase letters, and the rest of \ (a - b \) position to fill the first \ (b \) characters

Fill such a substring \ (s \) with the entire string of length \ (n \)

For example sample \ (7 \ 5 \ 3 \)

Construct \ (s = abccc \) and the answer is \ (abcccab \)

Perceptual understanding is that a greedy snake moves on the substring \ (s \) . The correct construction of \ (s \) guarantees the correctness of the entire string ...

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
 
 
inline int read()
{
    char c = getchar();
    int ans = 0, f = 1;
    while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
    while(isdigit(c)) {ans = ans * 10 + c - '0'; c = getchar();}
    return ans * f;
}
 
int t, a, b, c;
int main()
{
    t = read();
    while(t--) {
        a = read(), b = read(), c = read();
        string s = "";
        for(int i = 0; i < c; ++i)
            s += 'a' + i;
        for(int i = 0; i < b - c; ++i)
            s += 'a' + c - 1;
        for(int i = 0; i < a / b; ++i)
            cout<<s;
        for(int i = 0; i < a % b; ++i)
            cout<<s[i];
        cout<<endl;
    }
    return 0;
}

\(C.\)

Given a set of numbers \ (A \) , the set of requested divided into two different lengths \ (S, \ T \) , \ (S \) each have the same number, \ (T \) per The number is different, the number in \ (t \) can also appear in \ (s \) , find the largest possible length

The initial idea was a dichotomous answer, use \ (map \) to record the number of occurrences of each number, and then go to \ (check \)

Later \ (get \) got a better idea

With \ (X \) represents \ (A \) in \ (DISTINCT \) number of values of

Use \ (y \) to indicate the number of occurrences of the most frequent value in \ (a \)

\ (if \ x = y, \ ans = x - 1 \)
\ (else, \ ans = min (x, \ y) \)

/*二分答案*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
 
inline int read()
{
    char c = getchar();
    int ans = 0, f = 1;
    while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
    while(isdigit(c)) {ans = ans * 10 + c - '0'; c = getchar();}
    return ans * f;
}
 
int t, n, a[N];
map<int, int> mp;
 
bool check(int x)
{
    int f1 = 0, f2 = 0;
    set<int> st;
    for(int i = 1; i <= n; ++i)
        st.insert(a[i]);
    for(int i = 1; i <= n; ++i) {
        if(mp[a[i]] >= x) {
            if(mp[a[i]] == x) st.erase(a[i]);
            if(st.size() >= x) return 1;
            st.insert(a[i]);
        }
    }
    return 0;
}
int main()
{
    t = read();
    while(t--) {
        mp.clear();
        n = read();
        for(int i = 1; i <= n; ++i) a[i] = read(), mp[a[i]]++;
        int l = 0, r = n / 2;
        int ans = 0;
        while(l <= r) {
            int mid = l + r >> 1;
            //cout<<l<<' '<<r<<endl;
            if(check(mid)) ans = mid, l = mid + 1;
            else r = mid - 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}
/*
4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3
*/
/*good idea*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;


inline int read()
{
    char c = getchar();
    int ans = 0, f = 1;
    while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
    while(isdigit(c)) {ans = ans * 10 + c - '0'; c = getchar();}
    return ans * f;
}

int t, n;
map<int, int> mp;

int main()
{
    t = read();
    while(t--) {
        mp.clear();
        n = read();
        int x = 0;
        for(int i = 1; i <= n; ++i) {
            int y = read();
            mp[y]++;
            x = max(x, mp[y]);
        }
        int ans = 0;
        if(mp.size() == x) ans = x - 1;
        else ans = min((int)mp.size(), x);
        printf("%d\n", ans);
    }
    return 0;
}
/*
4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3
*/

\(D.\)

Given a correct Sudoku, at most \ (9 \) digits can be modified to make it \ (Anti-Sudoku \)

Sudoku satisfaction

  • Each line does not count
  • Each column does not repeat numbers
  • Each square of \ (3 \ times 3 \) does not overlap numbers

\ (Anti-Sudoku \)满足

  • At least two positions in each line are repeated
  • Each column repeats at least two positions
  • Every \ (3 \ times 3 \) square has at least two positions that repeat

Observation found that a position must be modified in each square, and the rows and columns of these positions cannot be repeated. You can use a search method similar to \ (N \) Queen to write

Later \ (get \) to a good \ (idea \) , since the correct Sudoku is given, then we only need to replace any number with another number, for example, all \ ( 3 \) replaced with \ (1 \)

/*dfs*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
int t, n, vis[20][20];
char g[20][20];
int f = 0, cnt;
const int dx[] = {0, 1, 1, 1, 2, 2, 2, 3, 3, 3};
const int dy[] = {0, 1, 2, 3, 1, 2, 3, 1, 2, 3};
int row[20], col[20];
 
inline int read()
{
    char c = getchar();
    int ans = 0, f = 1;
    while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
    while(isdigit(c)) {ans = ans * 10 + c - '0'; c = getchar();}
    return ans * f;
}
 
void out()
{
    //puts("XCY");
    for(int i = 1; i <= 9; ++i)
        puts(g[i] + 1);
}
 
bool check()
{
    map<int, int> mp;
    for(int i = 1; i <= 9; ++i) {
        int f = 0;
        for(int j = 1; j <= 9; ++j) {
            mp[g[i][j]]++;
            if(mp[g[i][j]] > 1) {f = 1; break;}
        }
        if(!f) return 0;
        mp.clear();
    }
    for(int i = 1; i <= 9; ++i) {
        int f = 0;
        for(int j = 1; j <= 9; ++j) {
            mp[g[j][i]]++;
            if(mp[g[j][i]] > 1) {f = 1; break;}
        }
        if(!f) return 0;
        mp.clear();
    }
    for(int k = 1; k <= 3; ++k) {
        for(int l = 1; l <= 3; ++l) {
            int f = 0;
            for(int i = (k - 1) * 3 + 1; i <= k * 3; ++i) {
                for(int j = (l - 1) * 3 + 1; j <= l * 3; ++j) {
                    mp[g[i][j]]++;
                    if(mp[g[i][j]] > 1) {f = 1; break;}
                }
                if(f) break;
            }
            mp.clear();
            if(!f) return 0;
        }
    }
    return 1;
}
 
void dfs(int step)
{
    //cout<<step<<endl;
    if(f) return;
    if(step > 9) {
        cnt++;
        if(check()) f = 1, out();
        else return;
    }
    for(int i = 3 * (dx[step] - 1) + 1; i <= 3 * dx[step]; ++i) {
        if(row[i]) continue;
        for(int j = 3 * (dy[step] - 1) + 1; j <= 3 * dy[step]; ++j) {
            if(col[j]) continue;
            for(int k = 1; k <= 9; ++k) {
                if(g[i][j] == k + '0') continue;
                if(vis[i][j]) continue;
                int temp = g[i][j];
                vis[i][j] = 1;
                g[i][j] = k + '0';
                row[i] = 1, col[j] = 1;
                dfs(step + 1);
                if(f) return;
                g[i][j] = temp;
                vis[i][j] = 0;
                row[i] = 0, col[j] = 0;
            }
        }
    }
}
int main()
{
    t = read();
    while(t--) {
        memset(vis, 0, sizeof(vis));
        memset(col, 0, sizeof(col));
        memset(row, 0, sizeof(row));
        f = 0;
        for(int i = 1; i <= 9; ++i)
            scanf("%s", g[i] + 1);
        dfs(1);
        //cout<<cnt<<endl;
    }
    return 0;
}
/*
154873296
386592714
729641835
863725149
975314628
412968357
631457982
598236471
247189563
*/
/*replace 3 with 1*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 4e2 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
int t, n;
char g[20][20];
 
inline int read()
{
    char c = getchar();
    int ans = 0, f = 1;
    while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();}
    while(isdigit(c)) {ans = ans * 10 + c - '0'; c = getchar();}
    return ans * f;
}
 
int main()
{
    t = read();
    while(t--) {
        for(int i = 1; i <= 9; ++i)
            scanf("%s", g[i] + 1);
        for(int i = 1; i <= 9; ++i)
            for(int j = 1; j <= 9; ++j)
                if(g[i][j] == '3') g[i][j] = '1';
        for(int i = 1; i <= 9; ++i)
            puts(g[i] + 1);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ChenyangXu/p/12702355.html