头像

Value

Personal


访客:5145

离线:4小时前



Value
18小时前

https://pics.images.ac.cn/image/5f012d2a7462e.html

#include <iostream>
using namespace std;
const int N = 1010;
int a[N][N], s[N][N];
int main(){
    int n, m, q;    cin >> n >> m >> q;
    for(int i = 1; i <= n; i ++ ){
        for(int j = 1; j <= m; j ++ ){
            cin >> a[i][j];
        }
    }
    for(int i = 1; i <= n; i ++ ){
        for(int j = 1; j <= m; j ++ ){
            s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
        }
    }
    while(q -- ){
        int x1, y1; cin >> x1 >> y1;
        int x2, y2; cin >> x2 >> y2;
        cout << s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1] << endl;
    }
    return 0;
}



Value
18小时前
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    double n;   cin >> n;
    double l = -10000, r = 10000;
    while(r - l > 1e-7){
        double mid = (l + r) / 2;
        if(mid * mid * mid >= n)    r = mid;
        else    l = mid;
    }
    printf("%.6lf\n", l);
    return 0;
}



Value
18小时前
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int main(){
    int n, m;   cin >> n >> m;
    for(int i = 0; i < n; i ++ )    cin >> a[i];
    while(m -- ){
        int f;  cin >> f;
        int l = 0, r = n;
        /*
            找到左边界,在从左边界开始循环一次找到右边界
        */
        while(l < r){
            int mid = l + r >> 1;
            if(a[mid] >= f)  r = mid;
            else    l = mid + 1;
        }
        if(l == n || a[l] != f){
           cout << "-1 -1" << endl;
           continue;
        }
        r = l;
        while(a[r + 1] == f && r + 1 < n)    r ++ ;
        cout << l << " " << r << endl;
    }
    return 0;
}



Value
2天前
#include <iostream>
using namespace std;
string s1, s2;   
void change(int t){
    if(s1[t] == '*')    s1[t] = 'o';
    else   s1[t] = '*';
    if(t + 1 < s1.size()){
        if(s1[t + 1] == '*')    s1[t + 1] = 'o';
        else    s1[t + 1] = '*';
    }
}
int main(){
     cin >> s1 >> s2;
    int cnt = 0;
    for(int i = 0; i < s1.size(); i ++ ){
        if(s1[i] != s2[i]){
            change(i);
            cnt ++ ;
        }
    }
    cout << cnt << endl;
    return 0;
}



Value
2天前
/*
    n = a + b / c
    =>  n * c = a * c + b
    =>  n * c - a * c = b(用于验证)
*/
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10;
bool flag[N], backup[N]; 
typedef long long ll;
ll n, cnt;
bool check(ll a, ll c){
    ll b = (n - a) * c;
    if(!b)  return false;
    memcpy(backup, flag, sizeof flag);
    bool success = true;
    while(b){
        if(!(b % 10) || backup[b % 10]){
            success = false;
            break;
        }
        backup[b % 10] = true;
        b /= 10;
    }
    for(int i = 1; i < 10; i ++ ){
        if(!backup[i]){
            success = false;
            break;
        }
    }
    return success;
}
void dfs_c(ll a, ll c){
    if(c && check(a, c)){
        cnt ++ ;
        return ;
    }
    for(int i = 1; i < 10; i ++ ){
        if(!flag[i]){
            flag[i] = true;
            dfs_c(a, c * 10 + i);
            flag[i] = false;
        }
    }
}
void dfs_a(ll a){
    if(a >= n)  return ;
    if(a)   dfs_c(a, 0);
    for(int i = 1; i < 10; i ++ ){
        if(!flag[i]){
            flag[i] = true;
            dfs_a(a * 10 + i);
            flag[i] = false;
        }
    }
}
int main(){
    cin >> n;
    dfs_a(0);
    cout << cnt << endl;
    return 0;
}



Value
2天前
/*
    递归:vector保存状态
*/
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> res;
void dfs(int time, int pre){
    if(n - pre < m - time)  return ;
    if(time == m){
        for(int i = 0; i < m; i ++ ){
            cout << res[i];
            if(i != m - 1)  cout << " ";
            else    cout << endl;
        }
        return ;
    }
    res.push_back(pre + 1);
    dfs(time + 1, pre + 1);
    res.pop_back();
    dfs(time, pre + 1);
}
int main(){
    cin >> n >> m;
    dfs(0, 0);
    return 0;
}

/*
    递归:二进制状态表示
*/
#include <iostream>
using namespace std;
int n, m;
void dfs(int time, int pre, int state){
    if(n - pre < m - time)  return ;
    if(time == m){
        for(int i = 0; i < n; i ++ ){
            if(state >> i & 1)  cout << i + 1 << " ";
        }
        cout << endl;
        return ;
    }
    dfs(time + 1, pre + 1, state | (1 << pre));
    dfs(time, pre + 1, state);
}
int main(){
    cin >> n >> m;
    dfs(0, 0, 0);
    return 0;
}

/*
    非递归(栈模拟)
*/
#include <iostream>
#include <stack>
using namespace std;
struct node{
    int pos;
    int time;
    int pre;
    int state;
};
int main(){
    int n, m;   cin >> n >> m;
    stack<node> stk;
    stk.push({0, 0, 0, 0});
    while(!stk.empty()){
        node now = stk.top();

        stk.pop();
        if(now.pos == 0){
            if(n - now.pre < m - now.time)  continue;
            if(now.time == m){
                for(int i = 0; i < n; i ++ ){
                    if(now.state >> i & 1)  cout << i + 1 << " ";
                }
                cout << endl;
                continue;
            }
            now.pos = 1;
            stk.push(now);
            stk.push({0, now.time + 1, now.pre + 1, now.state | (1 << now.pre)});
        }else if(now.pos == 1){
            now.pos = 2;
            stk.push(now);
            stk.push({0, now.time, now.pre + 1, now.state});
        }else   continue;
    }
    return 0;
}




Value
2天前
#include <iostream>
using namespace std;
const int N = 50;
int res[N];
int main(){
    int n;
    cin >> n;
    res[0] = 0, res[1] = 1, res[2] = 1;
    for(int i = 3; i < N; i ++ )    res[i] = res[i - 1] + res[i - 2]; 
    for(int i = 0; i < n; i ++ ){
        cout << res[i];
        if(i != n - 1)  cout << " ";
        else    cout << endl;
    }
    return 0;
}



Value
2天前
#include <iostream>
#include <vector>
using namespace std;
int n;
vector<int> res;
void dfs(int t, int state){
    if(t == n){
        for(int i = 0; i < res.size(); i ++ )   cout << res[i] << " ";
        cout << endl;
        return ;
    }
    for(int i = 0; i < n; i ++ ){
        if(!(state >> i & 1)){
            res.push_back(i + 1);
            dfs(t + 1, state | (1 << i));
            res.pop_back();
        }
    }
}
int main(){
    cin >> n;
    dfs(0, 0);
    return 0;
}



Value
8天前

法医:

class Solution {
public:
    int moreThanHalfNum_Solution(vector<int>& nums) {
        unordered_map<int, int> mp;
        int cnt = 0, res;
        for(int i = 0; i < nums.size(); i ++ ){
            mp[nums[i]] ++ ;
            if(cnt < mp[nums[i]]){
                res = nums[i];
                cnt = mp[nums[i]];
            }
        }
        return res;
    }
};

法尔

class Solution {
public:
    int moreThanHalfNum_Solution(vector<int>& nums) {
        int cnt = 1, res = nums[0];
        for(int i = 1; i < nums.size(); i ++ ){
            if(res == nums[i])   cnt ++ ;
            else    cnt -- ;
            if(!cnt){
                cnt = 1;
                res = nums[i];
            }
        }
        return res;
    }
};



Value
8天前
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 310;
char graph[N][N];
bool vis[N][N];
int n, m;
typedef pair<int, int> pii;
pii st, ed;
struct node{
    pii loc;
    int step;
};
int dic[4][2] = {
    {0, -1}, {-1, 0}, {0, 1}, {1, 0}  
};
bool check(int x, int y){
    if(x >= 0 && x < n && y >= 0 && y < m)  return true;
    return false;
}
int bfs(){
    queue<node> qu;
    qu.push({st, 0});
    vis[st.first][st.second] = true;
    while(!qu.empty()){
        node now = qu.front();
        qu.pop();
        for(int i = 0; i < 4; i ++ ){
            int xx = now.loc.first + dic[i][0];
            int yy = now.loc.second + dic[i][1];
            if(xx == ed.first && yy == ed.second)   return now.step + 1;
            if(check(xx, yy) && !vis[xx][yy] && graph[xx][yy] != '#'){
                vis[xx][yy] = true;
                qu.push({{xx, yy}, now.step + 1});
            }
        }
    }
    return -1;
}
int main(){
    while(cin >> n >> m && n && m){
        memset(vis, false, sizeof vis);
        for(int i = 0; i < n; i ++ ){
           for(int j = 0; j < m; j ++ ){
               cin >> graph[i][j];
               if(graph[i][j] == '@')   st = {i, j};
               if(graph[i][j] == '*')   ed = {i, j};
           } 
        }
        cout << bfs() << endl;
    }
    return 0;
}