SDUT 2021 summer team contest 3rd(for 20) A E J L

本次比赛使用文件格式的输入输出,需添加:
freopen(“file_name.in”,“r”,stdin);
freopen(“file_name.out”, “w”, stdout);

比赛链接

A. Alex Origami Squares

大意:一个长方形里面裁剪出三个正方形,使其中面积(边长)最大

/*
 * @Description: 
 * @Author: Kirie
 * @LastEditTime: 2021-08-06 21:05:35
 */
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define bug cout << "BUG HERE\n"
#define debug(x) cout << #x << " = " << x << endl
#define ll long long
typedef pair<int, int> PII;

double a, b;

void swap() {
    double t = a;
    a = b;
    b = t;
}

double Min(double x, double y) {
    return x < y ? x : y;
}

int main()
{
    ios::sync_with_stdio(false);
    freopen("alex.in", "r", stdin);
    freopen("alex.out", "w", stdout);
    cin >> a >> b;
    if(a > b) swap(); // 保证a < b
    double r = Min(a, b / 3.0);
	if(r < a / 2.0) r = a / 2.0;
    cout << fixed << setprecision(3) << r << endl;
    return 0;
}

E - Easy Arithmetic

大意:给出一个计算式,通过添加加号或者减号使其计算结果最大。计算前后都符合规则(1)没有前导0(2)没有连续符号(3)最后一位是数字

/*
* @Description: 
* @Author: Kirie
* @LastEditTime: 2021-08-06 21:13:54
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define bug cout << "BUG HERE\n"
#define debug(x) cout << #x << " = " << x << endl
#define ll long long
typedef pair<int, int> PII;

int main()
{
    ios::sync_with_stdio(false);
    freopen("easy.in","r",stdin);
    freopen("easy.out", "w", stdout);
    string a;
    cin>>a;
    for(int i=0;i<a.size();i++) {
        if(a[i]=='-'){ // 如果减号
            cout<<a[i]<<a[i+1]; // 减号和后面这个数不会变
            if(a[i+2]>='0'&&a[i+2]<='9') { // 如果后面还有数字
                int j=i+2; // 从这个数字开始
                while(j<a.size()&&a[j]=='0') { // 输出"+"号和数字,直到结尾 或者 不是0了
                    cout<<"+"<<a[j];
                    j++;
                }
                if(j!=a.size()&&a[j]>='0'&&a[j]<='9') { // 不是0的部分一次性输出
                    cout<<"+";
                }
                i=j-1; // 指针读到遍历到的位置
            } else {
                i=i+1;
            }
        } else { // 除了减号直接输出
            cout<<a[i];
        }
    }
    return 0;
}

J - Journey to the “The World’s Start”

大意:

  1. 输入n和t。1~n个车站共n个车站,某人要从第一个到最后一个车站,t为最大花费时间。
  2. 我们认为每经过一站花费1分钟,所以最大时长t要减去n-1分钟。
  3. 有n-1种票,票上有两种属性:一次最远能坐的距离(i) & 票价(p[i])。在第二行依次给出:p[1],p[2]…p[n-1]。
  4. 中间每个中转车站需要花费d[i]的时间重新进站。在第三行依次给出:d[2],d[3]…d[n-1]。第一站和最后一站不需要考虑该时间。
  5. 题目所求在不超过最大时间到达最后一站的条件下所需最小票价。
  6. 注意:票是一次性付钱,每次选择一种票,即可无限次乘车,每次在票允许的范围内。

思路:二分查找 + 单调队列优化

  1. (优化查找) 如果范围r的票可以完成任务,范围(r+1,n-1)的票也可以。所以如果有一个范围大的价格更低,那么在它之前的价格更高的都没有意义(或者说第i张票的价格p[i] = min(p[i] ~ p[n-1]))
  2. (优化查找) 二分的是票,如果满足条件,向左找看是否能找到满足的,直到找到满足题意的范围最小的票(经过上面的优化,已经保证对每张票来说价格是合理的)
  3. (优化方案) 对每一个车站,它都等于它之前的滑动窗口的最小值(队头.首元素)+该车站重新进站的时间(d[i])
/*
 * @Description: 
 * @Author: Kirie
 * @LastEditTime: 2021-08-07 09:14:35
 */
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define bug cout << "BUG HERE\n"
#define debug(x) cout << #x << " = " << x << endl
#define ll long long
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e4 + 7;

int p[N], d[N], n;
int ans, l, r;
ll t;

void init() {
    ans = 0;
    l = 1;
    r = n - 1;
}

bool solve(int x) { // x是某张票的序号,也是它能走的范围
    // 单调队列优化。队头代表出发站,first元素代表其花费时间,second元素代表站点序号
    deque < pair <ll, int> > q;
    // 插入第一个站点:从用时为0的第1站出发
    q.push_back(make_pair(0, 1));
    rep(i,2,n) {// 从第二站到最后一站
        // (1)单调队列清除不符合区间要求的元素,该元素已经滑出窗口,队首出队
        while(!q.empty() && q.front().second < i - x) q.pop_front();

        // (2)错误条件:到达某一站已经超过最大时长
        if(q.front().first > t - (n - 1)) return false;

        // (3)清除比要插入元素优先级小的元素以维护单调队列
        // 如果该元素小于队尾元素,说明队头元素无效
        while(!q.empty() && q.back().first >= q.front().first + d[i] ) q.pop_back();

        // (4)无论是否弹出队尾,都会将现有元素压入队尾
        q.push_back(make_pair(q.front().first + d[i], i));
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    freopen("journey.in", "r", stdin);
    freopen("journey.out", "w", stdout);
    cin >> n >> t;
    rep(i,1,n-1) cin >> p[i];
    rep(i,2,n-1) cin >> d[i];
    // 如果范围r的票可以完成任务,范围(r+1,n-1)的票也可以
    // 所以如果有一个范围大的价格更低,那么在它之前的价格更高的都没有意义
    for(int i = n - 2;i > 0;i --) p[i] = min(p[i], p[i + 1]);
    //令p[i]为后缀区间中的最小值,方便下一步二分
    init();//初始化
    while (r >= l) { // 二分查找
        int mid = (l + r) >> 1;
        if (solve(mid)) {
            ans = p[mid];
            r = mid - 1;
        } else l = mid + 1;
    }
    cout << ans << endl;
    return 0;
}

L. Lucky Chances

大意:一个网格内每个区域一个数字,我们称某数字“赢得一个方向”是指该数字比该方向的任意数字都要大,注意边界时直接赢得该方向。
思路:暴力(时间复杂度O(n * n * (n+n)) : 对每一个点,找它的行和列)

/*
 * @Description: 
 * @Author: Kirie
 * @LastEditTime: 2021-08-05 13:18:18
 */
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define bug cout << "BUG HERE\n"
#define debug(x) cout << #x << " = " << x << endl
#define ll long long
typedef pair<int, int> PII;

const int N = 1e2 + 7;

int mp[N][N];
int flag [5];
int cnt;
int t;

void init(){
    mem(flag, 1);
    cnt = 1;
}

int main()
{
	freopen("lucky.in","r",stdin);
	freopen("lucky.out", "w", stdout);
	ios::sync_with_stdio(false);
    int c,r;
    cin >> r >> c ;
    rep(i, 1, r)
        rep(j, 1, c) 
            cin >> mp[i][j];
    int ans = 0;
    rep(i, 1, r) {
        rep(j, 1, c) {//对每一个点
            init();
            t = mp[i][j];
            rep(k, 1, i-1) { //上 cnt = 1
                if(mp[k][j] >= t) {
                    flag[cnt] = 0;
                    break;
                }
            }
            //if(flag[cnt]) debug(cnt);
            cnt ++;
            rep(k, 1, j-1) { //左 cnt = 2
                if(mp[i][k] >= t) {
                    flag[cnt] = 0;
                    break;
                }
            }
            //if(flag[cnt]) debug(cnt);
            cnt ++;
            rep(k, i+1, r) { // 下 cnt = 3
                if(mp[k][j] >= t) {
                    flag[cnt] = 0;
                    break;
                }
            }
            //if(flag[cnt]) debug(cnt);
            cnt ++;
            rep(k, j+1, c) { //右 cnt = 4
                if(mp[i][k] >= t) {
                    flag[cnt] = 0;
                    break;
                }
            }
            //if(flag[cnt]) debug(cnt);
            rep(i, 1, 4) {
                if(flag[i]){
                    ans ++;
                }
            }
        }
    }
    cout << ans << endl;   
    return 0;
}

/*
 *
 ?                        _oo0oo_
 !                       o8888888o
 ?                       88" . "88
 !                       (| = = |)
 ?                       0\  √ /0
 !                     ___/`---'\___
 ?                   .' \\|     |// '.
 !                  / \\|||  :  |||// \
 ?                 / _||||| -:- |||||- \
 !                |   | \\\  - /// |   |
 ?                | \_|  ''\---/''  |_/ |
 !                \  .-\__  '-'  ___/-. /
 ?              ___'. .'  /--.--\  `. .'___
 !           ."" '<  `.___\_<★>_/___.' >' "".
 ?          | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 !          \  \ `_.   \_ __\ /__ _/   .-` /  /
 ?      =====`-.____`.___ \_____/___.-`___.-'=====
 !                        `=---='
 ? 
 * 
//                                                      
 * 
 todo   佛祖保佑 = 永不宕机 && 永无BUG && 不掉头发 = true
 */
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值