1010 Bragging Dice
题意
两个人玩传统的骰子游戏,现在追加三条规则:
1.如果之前没有人叫过1,则1被视为百搭;
2.如果手上的骰子点数都相同,则认为有额外的一个相同点数的骰子;
3.如果手上的骰子点数互不相同,则认为一个点数也没有。
现在双方知道对方的点数,问先手是否必胜。
分析
只要先手能叫,必胜。因此,先手叫不了的情况为必败,否则必胜。
代码如下
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
int n, a[N], b[N], c1[8], c2[8];
void solve(){
cin >> n;
for(int i = 1; i <= 6; i++) c1[i] = c2[i] = 0;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) cin >> b[i];
if(n > 6){
cout << "Win!\n";
return;
}
for(int i = 1; i <= n; i ++){
c1[a[i]] ++, c2[b[i]] ++;
if(c1[a[i]] > 1 || c2[b[i]] > 1){
cout << "Win!\n";
return;
}
}
cout << "Just a game of chance.\n";
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
cin >> t;
while(t--){
solve();
}
}
1012 Buy Figurines
题意
若干个人依次去买东西,每个人有一个购买时间。有若干个窗口,每个人到时会选择排队人最少的窗口,否则选择最左边的窗口。问需要多少时间买完。
分析
线段树维护。
代码如下
#include<iostream>
#include<algorithm>
#include<queue>
#define int long long
#define lson (p << 1)
#define rson (p << 1 | 1)
using namespace std;
const int N = 2e5 + 10;
struct people{
int s, t;
bool operator < (const people &x) const{
return s < x.s;
}
};
struct shop{
int t, i;
bool operator < (const shop &x) const{
return t > x.t;
}
};
int n, m;
int time[N], tr[N << 2];
people a[N];
void build(int l = 1, int r = m, int p = 1){
tr[p] = 0;
if(l == r) return;
int mid = (l + r) >> 1;
build(l, mid, lson);
build(mid + 1, r, rson);
}
void update(int x, int y, int l = 1, int r = m, int p = 1){
if(l == r){
tr[p] += y;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) update(x, y, l, mid, lson);
else update(x, y, mid + 1, r, rson);
tr[p] = min(tr[lson], tr[rson]);
}
int query(int l = 1, int r = m, int p = 1){
if(l == r) return l;
int mid = (l + r) >> 1;
if(tr[lson] <= tr[rson]) return query(l, mid, lson);
else return query(mid + 1, r , rson);
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= m; i++) time[i] = 0;
build();
for(int i = 1; i <= n; i++){
cin >> a[i].s >> a[i].t;
}
sort(a + 1, a + 1 + n);
priority_queue<shop> q;
for(int i = 1; i <= n; i++){
while(!q.empty() && q.top().t <= a[i].s){
update(q.top().i, -1);
q.pop();
}
int tmp = query();
update(tmp, 1);
q.push({max(time[tmp] + a[i].t, a[i].s + a[i].t), tmp});
time[tmp] = max(time[tmp] + a[i].t, a[i].s + a[i].t);
}
int ans = 0;
for(int i = 1; i <= m; i++){
ans = max(ans, time[i]);
}
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
}