牛客练习赛61

打怪
链接:https://ac.nowcoder.com/acm/contest/12178/A
来源:牛客网

题目描述
你是一个勇士,现在你准备去森林刷毛球怪,你有两个属性(血量,攻击力),毛球怪也有这两个属性。当你遭遇一只毛球怪时你们会进入战斗,然后你和毛球怪轮流攻击(你先手),每次使对方的血量减去自己攻击力的数值,当一方的血量小于等于 0 时死亡。现在你想知道在自己活着的前提下最多杀死几只毛球怪。

输入描述:
第一行一个正整数t,代表测试数据组数。

第二行四个正整数h,a,H,A,代表你的血量和攻击力以及毛球怪的血量和攻击力。

所有整数大小不超过1000。

输出描述:
共 t 行,每行一个整数x,代表最多能杀死多少毛球怪。如果能杀死无数只,输出-1。

示例1
输入
复制
1
5 1 2 1
输出
复制
4

一开始偷懒暴力超时了,只能找公式了,如果对方一击都扛不住,那么肯定是无数个怪物可以,首先我们先分析出打多少下对面会死,因为是我们先打的,所以对面可以少打我们一下,就可以算出我们打死一个怪物受多少伤害,然后整除即可,在这里我们要特判一下,如果可以整除的话,那么就说明没扛住最后一下,也就打不出最后一击,也就是少杀一个敌人

/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================
 
                  Please give me AC.
*/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;
using namespace __gnu_cxx;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int N = 2e5 + 10;
const int M = N * N;
const int mod = 1e9 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

signed main(){
	ios;
	int T;
	cin >> T;
	
	while(T --){
		int h, a, H, A;
		cin >> h >> a >> H >> A;
		if (a >= H){
			cout << "-1" << endl;
		}
		else{
			int temp = (H + a - 1) / a;
			temp --;
			int temp2 = A * temp;
			int ans = h / temp2;
			if (h % temp2 == 0)   ans --;
			cout << ans << endl;
		}
	}
	return 0;
}

吃水果
链接:https://ac.nowcoder.com/acm/contest/12178/B
来源:牛客网

题目描述
最近米咔买了n个苹果和m个香蕉,他每天可以选择吃掉一个苹果和一个香蕉(必须都吃一个,即如果其中一种水果的数量为0,则他不能进行这个操作),或者使用魔法将某一种水果的数量翻倍。
现在米咔想吃西瓜了,但是他的主人赛小息不让他买新水果,除非苹果和香蕉没有了,即数量都是0了。

  现在米咔想知道,最少用多少天他可以吃光苹果和香蕉。

  可以证明的是,一定存在一种方案可以让米咔在若干天后吃光苹果和香蕉。

输入描述:
第一行一个正整数T(T≤100),代表数据组数。

接下来T行每行两个正整数n,m(n,m ≤100000)。

输出描述:
共T行,每行一个正整数代表答案。
示例1
输入
复制
3
1 1
1 2
2 5
输出
复制
1
3
7
说明
对于第三组测试样例(2,5),第一天令n翻倍变成(4,5),接下来连续吃三天水果变成(1,2),第五天令n翻倍变成(2,2),接下来连续吃两天水果,在第七天时吃光苹果和香蕉。

按照惯例,我们先寻找有什么性质,我们迟早会让两种水果的数量是一样,一个会是另外一个的一半,一开始都减,然后一个变成2倍从而变成一样的,所以就考虑一开始的差,如果一开始的差小于等于那个小的值,那么先都减,然后变成一半的时候再变2倍,一起减。如果相等的话,那么直接减就行,剩下的情况时,我们把比较小的那个变成2倍,多会变到差小于等于较小的值就可以了,需要注意的是,在小的数变大的过程种,那个差也会变小

/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================
 
                  Please give me AC.
*/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;
using namespace __gnu_cxx;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int N = 2e5 + 10;
const int M = N * N;
const int mod = 1e9 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

signed main(){
	//ios;
	int T;
	cin >> T;
	
	while(T --){
		int n, m;
		cin >> n >> m;
		int cha = abs(n - m);
		
		if (cha == 0)   cout << n << endl;
		else if (min(n, m) >= cha){
			cout << max(n, m) + 1 << endl;
		}
		else if (min(n, m) < cha){
			int temp = min(n, m);
			int cnt = 1;
			while(temp < cha){
                 cha -= temp;
				temp *= 2;
               
				cnt ++;
			}
			
			cout << cnt + max(n, m) << endl;
			
		}	
	}
	
	return 0;
}

四个选项

链接:https://ac.nowcoder.com/acm/contest/12178/C
来源:牛客网

题目描述
众所周知,高考数学中有一个题目是给出12个单项选择,每一个选择的答案是 A,B,C,D 中的一个。

 网上盛传答案存在某种规律,使得蒙对的可能性大大增加。于是今年老师想让你安排这12个题的答案。但是他有一些条件,首先四个选项的数量必须分别为 na,nb,nc,nd。其次有 m 个额外条件,分别给出两个数字 x,y,代表第 x 个题和第 y 个题的答案相同。 现在你的老师想知道,有多少种可行的方案安排答案。

输入描述:
第一行五个非负整数na,nb,nc,nd,m,保证na+nb+nc+nd=12,0≤m≤1000。

接下来m行每行两个整数x,y(1≤ x,y ≤12)代表第x个题和第y个题答案必须一样。
输出描述:
仅一行一个整数,代表可行的方案数。
示例1
输入
复制
3 3 3 3 0
输出
复制
369600

两个位置的答案是固定相等的,那么就可以绑定到一块,就可以用并查集,那么我们可以这样等效,就是所有人都放到父亲节点上面,要么那个位置是0,要么那个位置就是父亲节点,他包括所有子节点的孩子的个数,只需要看每个父节点选哪个选项即可。由于数据范围非常的小,那么可以dfs搜索,于是我们就需要考虑的是搜索的顺序,我们有两种选择,一种是我们看每个选项选的是哪些父亲节点,一种是我们看每个父亲节点选哪些选项,很明显第二种更加的好考虑,于是我们从前到后看每个位置,如果那个位置是父亲节点的话,那么就枚举这个位置的父亲节点选哪个选项,如果走到了最后那么一定会满足条件

/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================
 
                  Please give me AC.
*/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;
using namespace __gnu_cxx;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int N = 30;
const int M = N * N;
const int mod = 1e9 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

int a, b, c, d, aa, bb, cc, dd, m;
int cnt[N];
int p[N];
int ans;

int find(int x){
	if (x != p[x])   p[x] = find(p[x]);
	return p[x];
}

void dfs(int u){
	if (u == 13){
		ans ++;
		return ;
	}
	
	if (cnt[u] == 0)     dfs(u + 1);
	else{
		if (aa + cnt[u] <= a){
			aa += cnt[u];
			dfs(u + 1);
			aa -= cnt[u];
		}
		if (bb + cnt[u] <= b){
			bb += cnt[u];
			dfs(u + 1);
			bb -= cnt[u];
		}
		if (cc + cnt[u] <= c){
			cc += cnt[u];
			dfs(u + 1);
			cc -= cnt[u];
		}
		if (dd + cnt[u] <= d){
			dd += cnt[u];
			dfs(u + 1);
			dd -= cnt[u];
		}
	}
}

signed main(){
	ios;
	cin >> a >> b >> c >> d >> m;
	
	for (int i = 1; i <= 12; i ++)   p[i] = i;
	
	for (int i = 1; i <= m; i ++){
		int x, y;
		cin >> x >> y;
		int pa = find(x), pb = find(y);
		p[pa] = pb;
	}
	
	for (int i = 1; i <= 12; i ++)     cnt[find(i)] ++;
	
	dfs(1);
	
	cout << ans << endl;
	
	return 0;
}

最短路变短了

链接:https://ac.nowcoder.com/acm/contest/12178/D
来源:牛客网

题目描述
给定一个有向带权图,其中包括 n个城市,城市编号从1 到n,有向边的编号从 1 到 m,现在有一个人位于城市 1,并且想要到城市n旅游。现在政府正在决定将一条道路反向,这个人想知道在某一指定道路反向的情况到达城市n最短路径长度会不会变短。

    保证开始给定的图从城市1可以到达城市n,若边反向后城市1不能到达城市n,我们视为最短路径长度没有变短。

输入描述:
第一行包括两个整数n,m(1≤n≤100000,1≤m≤200000)

接下来m行每行三个整数u,v,c (1≤u,v≤n, 1≤c≤10^9),分别代表一条有向边的起点,终点和边权。保证没有自环。

接下来一行一个整数q(1≤q≤200000),代表查询组数,查询之间是独立的。

接下来q行每行一个整数x(1≤x≤m),代表询问将第x条边反向后到达城市n的最短路长度会不会变短。

输出描述:
共q行,如果最短路变短输出YES,否则输出NO。
示例1
输入
复制
3 4
1 2 100
2 3 100
3 1 100
2 1 1
2
1
4
输出
复制
NO
YES
说明
第一条边反向后从点1无法到达点3,所以输出NO。
第四条边反向后从点1到点3的最短路长度从200减少到101,所以输出YES。

判断一个边改变后能否让距离更短,假如这个边的两个点是u和v,我们需要判断的是从起点到v加上边权再从u到终点的距离和原来的距离比是否更小,如果是的话就是YES,否则就是NO,如果现在最短路不走这条边,那么最短路肯定不会更短,如果走这条边的话,能短的话只可能是起点先到v,再到u,再到n点的距离,所以我们需要维护出从每个点到n点的距离,如何维护,我们需要把所有边都反向,重新建图,再从n号点跑一次最短路即可。然后比较即可,清边要处理h和idx

/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================
 
                  Please give me AC.
*/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;
using namespace __gnu_cxx;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"
//#define x first
//#define y second

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int N = 1e5 + 10;
const int M = 3 * N;
const int mod = 1e9 + 7;
const int PP = 131;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

int h[N], e[M], ne[M], w[M], idx;
int dist1[N], dist2[N];
bool st[N];
int n, m;

void add(int a, int b, int c){
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}

struct Edge{
	int a, b, c;
}edge[M];

void dijkstra(int s, int dist[]){
	priority_queue<PII, vector<PII>, greater<PII>> heap;
	heap.push({0, s});
//	memset(dist, 0x3f, sizeof dist);
	dist[s] = 0;
	
	while(heap.size()){
		auto t = heap.top();
		heap.pop();
		
		int ver = t.second, distance = t.first;
		
	//	cout << ver << "---" << distance << endl;
		if (st[ver])   continue;
		st[ver] = true;
		
		for (int i = h[ver]; ~i; i = ne[i]){
			int j = e[i];
		//	cout << j << "---" << dist[j] << endl;
			if (dist[j] > dist[ver] + w[i]){
			//	cout << "j:" << j << "---" << dist[j] << "dist[ver]:" << dist[ver] << " w[i]" << w[i] << endl;
				dist[j] = dist[ver] + w[i];
				heap.push({dist[j], j});
			}
		}
	}
	//cout << dist[1] << endl;
}

signed main(){ 
//	ios;
	memset(h, -1, sizeof h);
	cin >> n >> m;
	for (int i = 1; i <= m; i ++){
		int a, b, c;
		cin >> a >> b >> c;
		edge[i] = {a, b, c};
		add(a, b, c);
	}
	
	memset(dist1, 0x3f, sizeof dist1);
	memset(dist2, 0x3f, sizeof dist2);
	
	dijkstra(1, dist1);
	memset(st, 0, sizeof st);
	memset(h, -1, sizeof h);
    idx = 0;
	for (int i = 1; i <= m; i ++){
		int a = edge[i].a, b = edge[i].b, c = edge[i].c;
		add(b, a, c);
	} 
	
	dijkstra(n, dist2);
//	cout << dist1[n] << endl;
	
	int q;
	cin >> q;
	while(q --){
		int id;
		cin >> id;
		
		int a = edge[id].a, b = edge[id].b, c = edge[id].c;
	//	cout << dist1[b] << "---" << dist2[a] << "---" << c << "----" << dist1[n] << endl;
		if (dist1[b] + dist2[a] + c < dist1[n]){
			cout << "YES" << endl;
		}
		else   cout << "NO" << endl;
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值