头像

NeonSean




离线:23天前


活动打卡代码 AcWing 840. 模拟散列表

NeonSean
1个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

include [HTML_REMOVED]

using namespace std;

const int N =100003;
int h[N] ,e[N] , ne[N] ,idx;

void insert(int x)
{
int k= (x %N +N)%N;
e[idx] =x;
ne[idx]= h[k];
h[k] =idx++;

}

bool find (int x)
{
int k =(x %N +N )%N;
for (int i =h[k];i!= -1;i = ne[i])
{
if(e[i]==x)return true;
}
return false;
}

int main()
{
int n;
char op;
int a;
memset(h, -1, sizeof h);
cin>>n;
while(n–)
{

    cin>>op>>a;
    if(op =='I')
    {

        insert(a);

    }
    else 
    {
        if(find(a))
        {cout<<"Yes"<<endl;
        }
        else cout<<"No"<<endl;


    }


}
return 0;

}



活动打卡代码 AcWing 839. 模拟堆

NeonSean
1个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

// #include [HTML_REMOVED]
using namespace std;

// h[N]存储堆中的值, h[1]是堆顶,x的左儿子是2x, 右儿子是2x + 1
// ph[k]存储第k个插入的点在堆中的位置
// hp[k]存储堆中下标是k的点是第几个插入的
const int N =100010;
int h[N] , ph[N] , hp[N] , mysize;

void heap_swap( int a ,int b)
{
swap(ph[hp[a]], ph[hp[b]]);
swap(hp[a] ,hp[b]);
swap(h[a] ,h[b]);
}

void down(int u)
{
int t =u ;
if(u2 <= mysize &&h[u 2] < h[t]) t = u * 2 ;
if ( u * 2 + 1 <= mysize && h[u 2 +1 ]< h[t]) t = u2 + 1;
if(u != t) {
heap_swap(u , t);
down(t);
}
}

void up (int u)
{
while(u/2 &&h[u] < h[u / 2])
{
heap_swap(u ,u /2);
u = u/2;
}
}

int main()
{
int n ;
cin>> n ;
mysize =0;
string s;
int a ,k;
int cont=0;

while( n --)
{
    cin>>s;
    if(s == "I")
    {
     cin >>a ;
     h[++mysize]=a; ph[++cont]= mysize ;hp[mysize]=cont;
     up(mysize);
    //  cout<<h[mysize];
    }
   else if(s =="PM")
   {
       cout<<h[1]<<endl;
   }
   else if ( s=="DM")
   {

       heap_swap(1 ,mysize--);

       down(1);

   }
   else if ( s =="D")
   {
       cin>>k;
       int u=ph[k]; 
       heap_swap(mysize-- ,ph[k]);

       up(u) ,down(u);

   }
   else if ( s =="C")
   {
       cin >>k >>a;
       h[ph[k]]=a;
        up(ph[k]) ,down(ph[k]);
   }


}
return 0;

}



活动打卡代码 AcWing 240. 食物链

NeonSean
1个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

include [HTML_REMOVED]

// #include [HTML_REMOVED]
using namespace std;

const int N =100010;
int h[N] ,mysize;

// void swap( int a ,int b)
// {
// int c;
// c=b;
// b= a;
// a =c;
// }

void down(int u)
{
int t =u ;
if(u2 <= mysize &&h[u 2] < h[t]) t = u * 2 ;
if ( u * 2 + 1 <= mysize && h[u 2 +1 ]< h[t]) t = u2 + 1;
if(u != t) {
swap(h[u] , h[t]);
down(t);
}
}

// void up (int u)
// {
// while(u/2 &&h[u] < h[u / 2])
// {
// swap(h[u] ,h[u /2]);
// u = u/2;
// }
// }

int main()
{
int m , n ;
cin>> n >> m;
mysize =n;
for( int i=1 ; i<= n; i++) cin>> h[i];

for( int i= n/2 ;i ; i--)down(i);

while( m --)
{
    cout<<h[1]<<' ';
    h[1]=h[mysize--];
    down(1);

}

}



活动打卡代码 AcWing 756. 蛇形矩阵

NeonSean
2个月前
#include <iostream>

using namespace std;

int M[100][100];

int main() {
    int n, m;
    cin >> n >> m;

    int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1};
    for (int k = 1, t = 0, x = 0,  y = 0; k <= n * m; k++) {
        M[x][y] = k;
        int a = x + dx[t], b = y + dy[t];
        if (a < 0 || a >= n || b < 0 || b >= m || M[a][b]) {
            t = (t + 1) % 4;
            a = x + dx[t], b = y + dy[t];
        }
        x = a, y = b;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << M[i][j] << ' ';
        }
        cout << endl;
    }
    return 0;
}


活动打卡代码 AcWing 703. 数独检查

NeonSean
3个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 40;
int m, n;
int w[N][N];
bool st[N];

bool check_row () {
    for (int i = 0; i < m; i++) {
        memset(st, 0, sizeof st);
        for (int j = 0; j < m; j++) {
            int k = w[i][j];
            if (k > m || k < 1) return false;
            if (st[k] == true) return false;
            st[k] = true;
        }
    }
    return true;
}

bool check_col () {
    for (int i = 0; i < m; i++) {
        memset(st, 0, sizeof st);
        for (int j = 0; j < m; j++) {
            int k = w[j][i];
            if (k > m || k < 1) return false;
            if (st[k] == true) return false;
            st[k] = true;
        }
    }
    return true;
}

bool check_cell() {
    for (int x = 0; x < m; x += n){
        for (int y = 0; y < m; y += n) {
            memset(st, 0 , sizeof st);
            for (int dx = 0; dx < n; dx ++) {
                for (int dy = 0; dy < n; dy ++) {
                    int k = w[x + dx][y + dy];
                    if (k > m || k < 1) return false;
                    if (st[k] == true) return false;
                    st[k] = true;
                }
            }
        }
    }
    return true;
}

int main() {
    int T;
    scanf("%d", &T);

    for (int t = 1; t <= T; t++) {
        scanf("%d", &n);
        m = n * n;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                scanf("%d", &w[i][j]);
            }
        }
        if (check_row() && check_col() && check_cell()) 
            printf("Case #%d: Yes\n", t);
        else printf("Case #%d: No\n", t);
    }
    return 0;
}


活动打卡代码 AcWing 420. 火星人

NeonSean
3个月前
#include <iostream>
#include <algorithm>


using namespace std;
const int N = 10010;
int a[N];

int main() {
    int n, m;
    scanf("%d", &n);
    scanf("%d", &m);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);

    while (m--) next_permutation(a,a + n);

    for (int i = 0; i < n; i++) printf("%d ", a[i]);
    return 0;
}


活动打卡代码 AcWing 1603. 整数集合划分

NeonSean
3个月前
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 100010;
int a[N];

int sum(int a[], int l, int r)//求数组 a[l] ~ a[r] 的和
{ 
    int res = 0;
    for(int i = l; i <= r; i++)
        res += a[i];
    return res;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a,a + n);//排序

    int s1, s2;

    s1 = sum(a, 0, n/2-1);//s1 的和小
    s2 = sum(a, n/2, n - 1);//s2 的和大

    cout << n % 2 << " " << s2 - s1;
}



NeonSean
3个月前
#include <iostream>
#include <limits.h>
#include <algorithm>

using namespace std;

const int N = 1005;
int a[N];

inline int mypow(int t) {
    return t * t;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];

    int res = INT_MAX;

    for (int i = 1 ; i < 84; i++) {
        int l = i, r = i + 17;
        int temp = 0;
        for (int  j = 0; j < n; j ++) {
            if (a[j] < l) temp += mypow(a[j] - l);
            else if (a[j] > r) temp += mypow(a[j] - r);
        }
        res = min(res, temp);
    }
    cout << res << endl;
    return 0;
}



活动打卡代码 AcWing 1353. 滑雪场设计

NeonSean
3个月前
#include <iostream>
#include <limits.h>
#include <algorithm>

using namespace std;

const int N = 1005;
int a[N];

inline int mypow(int t) {
    return t * t;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];

    int res = INT_MAX;

    for (int i = 1 ; i < 84; i++) {
        int l = i, r = i + 17;
        int temp = 0;
        for (int  j = 0; j < n; j ++) {
            if (a[j] < l) temp += mypow(a[j] - l);
            else if (a[j] > r) temp += mypow(a[j] - r);
        }
        res = min(res, temp);
    }
    cout << res << endl;
    return 0;
}


活动打卡代码 LeetCode 27. 移除元素

NeonSean
7个月前
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int n = nums.size();
        int j = 0;
        for (int i = 0; i < n; i++) {
            if (nums[i] != val) nums[j++] = nums[i];
        }
        return j;
    }
};