数星星 Stars

题目链接
直接二维树状数组会超内存,利用贪心排序降维(题目数据已经有序)

#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set> 
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=15010;
const int M=32010;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

struct Node{
	int x,y;
}node[N];   //N个节点数 
int n,m=32001;  
int c[M],ans[M];   //树状数组要超过x的范围 
int lowbit(int x){
	return x&(-x);
}
void update(int x,int v){
	while(x<=m){
		c[x]+=v;  
		x+=lowbit(x);
	}
}
int sum(int x){
	int res=0;
	while(x>0){
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int a,b;
		cin>>a>>b;
		a++,b++;
		node[i].x=a,node[i].y=b;
	}
	for(int i=1;i<=n;i++){
		int v=sum(node[i].x);
		update(node[i].x,1);
		ans[v]++;
	}
	for(int i=0;i<n;i++){
		cout<<ans[i]<<endl;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用数组实现星星下落的简单示例程序: ``` #include <iostream> #include <vector> using namespace std; const int ROWS = 8; const int COLS = 8; // 星星类型 enum StarType { STAR_RED = 1, STAR_BLUE, STAR_GREEN, STAR_PURPLE, STAR_YELLOW }; // 游戏区域 int board[ROWS][COLS] = { {0, 0, STAR_RED, STAR_BLUE, STAR_GREEN, STAR_RED, 0, 0}, {0, 0, STAR_GREEN, STAR_PURPLE, STAR_YELLOW, STAR_BLUE, 0, 0}, {0, 0, STAR_YELLOW, STAR_PURPLE, STAR_RED, STAR_GREEN, 0, 0}, {0, 0, STAR_RED, STAR_BLUE, STAR_GREEN, STAR_RED, 0, 0}, {0, 0, STAR_BLUE, STAR_PURPLE, STAR_YELLOW, STAR_BLUE, 0, 0}, {0, 0, STAR_YELLOW, STAR_PURPLE, STAR_RED, STAR_GREEN, 0, 0}, {0, 0, STAR_RED, STAR_BLUE, STAR_GREEN, STAR_RED, 0, 0}, {0, 0, STAR_BLUE, STAR_PURPLE, STAR_YELLOW, STAR_BLUE, 0, 0} }; // 显示游戏区域 void show_board() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { cout << board[i][j] << " "; } cout << endl; } } // 消除星星 void remove_stars(int row, int col) { // TODO: 实现消除星星的逻辑 } // 处理星星下落 void handle_falling_stars() { // 处理每一列 for (int j = 0; j < COLS; j++) { int empty_row = -1; // 第一个空行的位置 for (int i = ROWS - 1; i >= 0; i--) { if (board[i][j] == 0 && empty_row == -1) { empty_row = i; } else if (board[i][j] != 0 && empty_row != -1) { board[empty_row][j] = board[i][j]; board[i][j] = 0; empty_row--; } } } // 处理每一行 for (int i = ROWS - 1; i >= 0; i--) { int empty_col = -1; // 第一个空列的位置 for (int j = 0; j < COLS; j++) { if (board[i][j] == 0 && empty_col == -1) { empty_col = j; } else if (board[i][j] != 0 && empty_col != -1) { board[i][empty_col] = board[i][j]; board[i][j] = 0; empty_col++; } } } } // 主函数 int main() { cout << "初始状态:" << endl; show_board(); // 消除 (2, 4) 处的星星 remove_stars(2, 4); // 显示消除后的状态 cout << "消除后状态:" << endl; show_board(); // 处理星星下落 handle_falling_stars(); // 显示下落后的状态 cout << "星星下落后状态:" << endl; show_board(); return 0; } ``` 这个程序中,我们定义了一个 8x8 的游戏区域数组 `board`,其中每个元素是一个星星的类型(用数字表示)。在 `handle_falling_stars` 函数中,我们实现了星星下落的逻辑,包括处理每一列和每一行的情况。我们还定义了一个 `remove_stars` 函数来实现消除星星的逻辑,但这个函数的具体实现留作练习。在主函数中,我们演示了如何调用这些函数来模拟星星下落的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值