L2-046 天梯赛的赛场安排
读懂题目最重要!!!
#include<bits/stdc++.h>
//unordered_map hash
//priority_queue<int,vector<int>,greater<int>>
//deque front back
#define endl '\n'
#define PII pair<int,int>
//#define int long long
//#define int __int128
using namespace std;
inline int read();
inline void write(int x);
string s[5005];
int a[5005];
int cnt[5005];
int res[5005];
int idx;
void solve(){
int n = read(), c = read();
priority_queue<PII> q;
for(int i = 1; i <= n; ++ i){
cin >> s[i] >> a[i];
q.push({a[i], i});
}
int sum = 0;
while(q.size()){
int v = q.top().first, id = q.top().second;
q.pop();
if(v >= c){
cnt[id] ++;
v -= c;
if(v){
q.push({v, id});
}
++ sum;
}
else{
int f = 0;
for(int i = 1; i <= n; ++ i){
if(res[i] >= v){
f = 1;
res[i] -= v;
if(id != i){
cnt[id] ++;
}
break;
}
}
if(!f){
res[id] = c - v;
cnt[id] ++;
++ sum;
}
}
}
for(int i = 1; i <= n; ++ i){
cout << s[i] << ' ' << cnt[i] << endl;
}
cout << sum;
}
signed main(){
int _ = 1;
while(_ --) solve();
return 0;
}
inline int read(){
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*f;
}
inline void write(int x){
if(x < 0) putchar('-'), x = - x;
if(x > 9) write(x / 10);
putchar('0' + x % 10);
}
L2-048 寻宝图
因为图的大小满足,所以需要动态开辟空间
#include<bits/stdc++.h>
//unordered_map hash
//priority_queue<int,vector<int>,greater<int>>
//deque front back
#define endl '\n'
#define PII pair<int,int>
//#define int long long
//#define int __int128
using namespace std;
inline int read();
inline void write(int x);
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
vector<char> g[100005];
vector<int> st[100005];
int land, val;
int n, m;
void bfs(int x1, int y1, int flag){
if(flag) ++ val;
++ land;
queue<PII> q;
q.push({x1, y1});
st[x1][y1] = 1;
while(q.size()){
int x = q.front().first, y = q.front().second;
q.pop();
for(int i = 0; i < 4; ++ i){
int xx = x + dx[i];
int yy = y + dy[i];
if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && g[xx][yy] != '0' && !st[xx][yy]){
if(g[xx][yy] != '1' && !flag){
++ val;
flag = 1;
}
q.push({xx, yy});
st[xx][yy] = 1;
}
}
}
}
void solve(){
n = read(), m = read();
for(int i = 1; i <= n; ++ i){
g[i].resize(m + 1);
st[i].resize(m + 1);
for(int j = 1; j <= m; ++ j){
cin >> g[i][j];
}
}
for(int i = 1; i <= n; ++ i){
for(int j = 1; j <= m; ++ j){
if(!st[i][j] && g[i][j] != '0'){
if(g[i][j] == '1'){
bfs(i, j, 0);
}
else{
bfs(i, j, 1);
}
// cout << i << ' ' << j << endl;
}
}
}
write(land);
putchar(' ');
write(val);
}
signed main(){
int _ = 1;
while(_ --) solve();
return 0;
}
inline int read(){
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*f;
}
inline void write(int x){
if(x < 0) putchar('-'), x = - x;
if(x > 9) write(x / 10);
putchar('0' + x % 10);
}