2.27-小六集训队-407
P15413 [CCC 2019 J4/S1] Flipper 翻转
题意
有一个数字表,如下图
1 2
3 4
可以对他进行水平翻转或垂直翻转,请问多次操作后,表格成了什么样子
思路
开始定义好表格,水平翻转交换每行,否则交换每列
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[3][3];
string s;
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
a[1][1] = 1;
a[1][2] = 2;
a[2][1] = 3;
a[2][2] = 4;
cin >> s;
for(int i = 0;i < s.size();i++){
if(s[i] == 'H'){
swap(a[1][1], a[2][1]);
swap(a[1][2], a[2][2]);
}else if(s[i] == 'V'){
swap(a[1][1], a[1][2]);
swap(a[2][1], a[2][2]);
}
}
for(int i = 1;i <= 2;i++){
for(int j = 1;j <= 2;j++){
cout << a[i][j]<< ' ';
}
cout << '\n';
}
return 0;
}
/*
HV
*/
/*
VVHH
*/
P15413 [CCC 2019 J4/S1] Flipper 翻转
题意
有
思路
输入完
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 60005;
int n, m;
int k[N];
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m;
for(int i = 1;i <= m;i++){
cin >> k[i];
}
sort(k + 1, k + 1 + m);
int cnt = 0;
for(int i = 1;i <= m;i++){
if(n >= k[i]){
n -= k[i];
cnt++;
}
}
cout << cnt;
return 0;
}
/*
10 4
3
9
4
2
*/
P15024 [UOI 2020 II Stage] 修理帕萨特
题意
有
思路
可以用二分锁定休息的天数,然后用一个函数
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
const int N = 2e5 + 5;
pair <int, int> a[N];
pair <int, int> tmp[N];
bool check(int k){//判断是否能在规定时间内做完这些任务
int sum = k;
for(int i = 1;i <= n;i++){
sum += a[i].second;
if(sum > a[i].first){
return false;
}
}
return true;
}
int minn = LLONG_MAX;
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i].second>> a[i].first;
minn = min(minn, a[i].first);//求出最⼤可以休息的天数
}
sort(a + 1, a + 1 + n);//要二分,需要数组是有序的,而且优先要处理截至早的广告
int l = -1, r = minn + 1;//最多休息minn天,最少0天,但二分的l要在最小值-1,r要在最大值+1的位置
while(l + 1 < r){
int mid = (l + r) / 2;
if(check(mid)){
l = mid;
}else{
r = mid;
}
}
cout << l;
return 0;
}
/*
4
1 3
1 10
50 100
9 13
*/
/*
10
1 91
5 96
5 62
3 67
5 57
1 93
4 67
4 23
1 53
1 76
*/
P6473 [NOI Online #2 入门组] 未了
题意
有一条长度为
思路
我们可以准备一个前缀和数组,起算从大到小使用魔法,可以拖延的时间。因为越从厉害的魔法用,拖延的时间更久。我们可以先确定一个最小的使用魔法的数量,使
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, len, v;
const int N = 2e5 + 5;
int a[N];
int sum[N];
bool cmp(int x, int y){
return x > y;
}
bool check(int k, int t){
int tot = len;
tot += sum[k];
return tot > v * t;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> len>> v;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
sort(a + 1, a + 1 + n, cmp);
for(int i = 1;i <= n;i++){
sum[i] = sum[i - 1] + a[i];
}
int q;
cin >> q;
while(q--){
int t;
cin >> t;
int l = -1, r = n + 1;
while(l + 1 < r){
int mid = (l + r) / 2;
if(check(mid, t)){
r = mid;
}else{
l = mid;
}
}
if(sum[r] + len > t * v){
cout << r<< '\n';
}else{
cout << -1<< '\n';
}
}
return 0;
}
P3611 [USACO17JAN] Cow Dance Show S
题意
有
思路
先二分查找枚举出最小的打开数量,然后判断是否可以再
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, t;
const int N = 2e5 + 5;
int a[N];
bool check(int k){
priority_queue<int, vector<int>, greater<int> > q;
for(int i = 1;i <= k;i++){
q.push(a[i]);
}
for(int i = k + 1;i <= n;i++){
q.push(q.top() + a[i]);
q.pop();
}
int ans = 0;
while(q.size()){
ans = q.top();
q.pop();
}
return ans <= t;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> t;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
int l = 0, r = n + 1;
while(l + 1 < r){
int mid = (l + r) / 2;
if(check(mid)){
r = mid;
}else{
l = mid;
}
}
cout << r;
return 0;
}