$ CF \ 634 \(Div3)$

\(CF \ 634 \(Div3)\)

\(A. \)

\(n \)が与えられたら\(a + b = n \)\(a> b \)のような正の整数\(a、\ b \)の数を尋ねます

場合\(N- \)もあり、その後、\(ANS = N / 2 - 1 \)

場合(N- \)は\奇数である場合、\(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. \)

長さ\(n \)の文字列を作成し、長さ\(a \)の各部分文字列に\(b \)の異なる文字のみが含まれるようにする

次のように構成されています

最初の塗りつぶしの前に順番によると、\(B \)小文字、および残りの- (B \)\最初に記入する立場\(Bを\)文字

そのような部分文字列\(s \)を長さ\(n \)の文字列全体で埋めます

たとえば、サンプル\(7 \ 5 \ 3 \)

建設\(S = abccc \) そして答えは(abcccab \)\

感情理解ストリングにおける巨大なメモリこと\(S \)移動、\(S \)は、文字列全体の正しさの正確さを確保するように構成されています...

#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. \)

数字のセット所与\(A \) 二つの異なる長さに分割された要求の集合\(S、\ T \)、\ (S \)はそれぞれ同数有し\(T \)をあたりない同じ数、\(T \)もで発生することが数値\(S \) および最大可能な長さを求めます

最初のアイデアは二分法の答えでした。\(マップ\)を使用して各数値の出現回数記録し、次に\(チェック\)に進みます

後で\(get \)より良いアイデアを得た

\(X \)を表し\(A \)\(DISTINCT \)の値の数

\(y \)を使用して\(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. \)

正しい数独の場合、最大で\(9 \)桁を変更し\(反数独\)することができます

数独満足

  • 各行はカウントされません
  • 各列は数字を繰り返さない
  • \(3 \×3 \)の正方形は数値と重なりません

\(Anti-Sudoku\) 满足

  • 各行で少なくとも2つの位置が繰り返されている
  • 各列は少なくとも2つの位置を繰り返します
  • すべての\(3 \×3 \)正方形には、少なくとも2つの位置が繰り返されます

観察により、各正方形で位置を変更する必要があり、これらの位置の行と列を繰り返すことはできません。\(N \) Queenと同様の検索方法を使用して

後で\(get \)を適切な\(idea \)に変更します。正しい数独が指定されているため、任意の数値を別の数値たとえば、すべての\( 3 \)\(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;
}

おすすめ

転載: www.cnblogs.com/ChenyangXu/p/12702355.html