牛客练习赛50

tokitsukaze and Connection
链接:https://ac.nowcoder.com/acm/contest/12257/A
来源:牛客网

题目描述
tokitsukaze有一个长度为n,只包含小写字母的字符串S。
对于每一种字母,若同种字母都连在一起,tokitsukaze会感到心情愉悦。
若tokitsukaze会感到心情愉悦,请输出"YES",否则输出"NO"。(均不含引号。)
输入描述:
第一行输入一个正整数n,(1≤n≤100)。
接下来一行输入一个长度为n的字符串S,‘a’≤S[i]≤’z’。
输出描述:
输出一行,“YES”,或者"NO"。
示例1
输入
复制
1
a
输出
复制
YES
示例2
输入
复制
2
ab
输出
复制
YES
示例3
输入
复制
3
aba
输出
复制
NO

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

题目描述
tokitsukaze有一个长度为n,只包含小写字母的字符串S。
对于每一种字母,若同种字母都连在一起,tokitsukaze会感到心情愉悦。
若tokitsukaze会感到心情愉悦,请输出"YES",否则输出"NO"。(均不含引号。)
输入描述:
第一行输入一个正整数n,(1≤n≤100)。
接下来一行输入一个长度为n的字符串S,‘a’≤S[i]≤’z’。
输出描述:
输出一行,“YES”,或者"NO"。
示例1
输入
复制
1
a
输出
复制
YES
示例2
输入
复制
2
ab
输出
复制
YES
示例3
输入
复制
3
aba
输出
复制
N

模拟即可

、#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 = 9e5 + 10;
const int M = 2 * 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);

string str;
int n;

signed main(){
	ios;
	cin >> n;
	cin >> str;
	
	set<int> ss;
	bool flag = false;
	for (int i = 0; i < n; i ++){
		if (ss.count(str[i]))   flag = true;
		else{
			char c = str[i];
			int j = i + 1;
			while(str[j] == str[i])    j ++;
			i = j - 1;
			ss.insert(c);
		}
	}
	
	if (flag)    cout << "NO" << endl;
	else   cout << "YES" << endl;
	
    return 0;
}

tokitsukaze and Hash Table
链接:https://ac.nowcoder.com/acm/contest/12257/B
来源:牛客网

题目描述
tokitsukaze有n个数,需要按顺序把他们插入哈希表中,哈希表的位置为0到n-1。
插入的规则是:
刚开始哈希表是空的。
对于一个数x,在哈希表中,如果(x mod n)的位置是空的,就把x放在(x mod n)的位置上。如果不是空的,就从(x mod n)往右开始找到第一个空的位置插入。若一直到n-1都不是空的,就从位置0开始继续往右找第一个空的位置插入。
因为哈希表总共有n个空位,需要插入n个数,所以每个数都能被插入。
现在tokitsukaze想知道把这n个数按顺序插入哈希表后,哈希表中的每个位置分别对应的是哪个数。
输入描述:
第一行包含一个正整数n(1≤n≤10^6)。
第二行包含n个非负整数x(0≤x≤10^9),这些数按从左到右的顺序依次插入哈希表。
输出描述:
输出一行,n个数,第i个数表示哈希表中位置为i所对应的数。(0≤i≤n-1)
示例1
输入
复制
4
1 2 6 5
输出
复制
5 1 2 6
说明
插入1时,1 mod 4=1,是空的,在位置1插入。
插入2时,2 mod 4=2,是空的,在位置2插入。
插入6时,6 mod 4=2,不是空的,找到下一个空的位置为3,所以在位置3插入。
插入5时,5 mod 4=1,不是空的,找到下一个空的位置为0,所以在位置0插入。
示例2
输入
复制
4
3 0 7 11
输出
复制
0 7 11 3
说明
插入3时,3 mod 4=3,是空的,在位置3插入。
插入0时,0 mod 4=0,是空的,在位置0插入。
插入7时,7 mod 4=3,不是空的,找到下一个空的位置为1,所以在位置1插入。
插入11时,11 mod 4=3,不是空的,找到下一个空的位置为2,所以在位置2插入。

二分查找还没有被占有的位置即可,可以用set来实现,那个位置被占用了就删了即可,然后注意的是如果迭代器到了最后一个位置,就指向开头,因为留下一定是还没有被删的

#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 = 1e6 + 100;
const int M = 2 * 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);

set<int> ss;
int n;
int a[N];

signed main(){
	ios;
	cin >> n;

	for (int i = 0; i < n; i ++)    ss.insert(i);
	
	for (int i = 1; i <= n; i ++){
		int x;
		cin >> x;
		int temp = x;
		x %= n;
		set<int> :: iterator it = ss.lower_bound(x);
		if (it == ss.end())    it = ss.begin();
 		
 		a[*it] = temp;
 		ss.erase(it);
	}
	
	for (int i = 0; i < n; i ++)   cout << a[i] << " ";
	cout << endl;
	
    return 0;
}

tokitsukaze and Soldier链接:https://ac.nowcoder.com/acm/contest/12257/C
来源:牛客网

题目描述
在一个游戏中,tokitsukaze需要在n个士兵中选出一些士兵组成一个团去打副本。
第i个士兵的战力为v[i],团的战力是团内所有士兵的战力之和。
但是这些士兵有特殊的要求:如果选了第i个士兵,这个士兵希望团的人数不超过s[i]。(如果不选第i个士兵,就没有这个限制。)
tokitsukaze想知道,团的战力最大为多少。
输入描述:
第一行包含一个正整数n(1≤n≤10^5)。
接下来n行,每行包括2个正整数v,s(1≤v≤10^9,1≤s≤n)。
输出描述:
输出一个正整数,表示团的最大战力。
示例1
输入
复制
2
1 2
2 2
输出
复制
3
示例2
输入
复制
3
1 3
2 3
100 1
输出
复制
100

我们可以想得到这种选一些人的问题可以用优先队列来实现,但是最大的一个问题是优先顺序,哪些先加进去,肯定是s大的先加进去,因为如果是小的先加进去的话,那么加入后面的还得考虑先加进去的s是否符合条件,这样的话只需要考虑当前加的这个就可以了,然后如果不符合条件的话,从先加进去的删一些就可以了,删哪些,删得是攻击力小的,这样一直取最小值就可以了

#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 + 100;
const int M = 2 * 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);

struct Node{
	int val;
	int s;
	bool operator<(const Node &W)const{
		return val > W.val;
	}
}node[N];

bool cmp(Node a, Node b){
	return a.s > b.s;
}
int n;

signed main(){
	ios;
	cin >> n;
	for (int i = 1; i <= n; i ++){
		cin >> node[i].val;
		cin >> node[i].s;
	}
	
	sort(node + 1, node + 1 + n, cmp);
	
	priority_queue<Node> heap;
	int sum = 0;
	int ans = 0;
	for (int i = 1; i <= n; i ++){
		if (heap.size() < node[i].s){
			heap.push(node[i]);
			sum += node[i].val;
		}
		else if (heap.size() >= node[i].s){
			while(heap.size() >= node[i].s){
				auto t = heap.top();
				heap.pop();
				sum -= t.val;
			}
			heap.push(node[i]);
			sum += node[i].val;
		}
		
		ans = max(ans, sum);
	}
	
	cout << ans << endl;
	
    return 0;
}

tokitsukaze and Event
链接:https://ac.nowcoder.com/acm/contest/12257/D
来源:牛客网

题目描述
这天,tokitsukaze带着她的舰队去才归一儿海探索。这个海域有n个站点,深海舰队控制着这片海域的m条航线,这些航线连接着这n个点,第i条航线连接着ui,vi两个点。航线都是正确的,也就是说没有重复的航线,也没有任何一个点与自己相连。tokitsukaze的舰队经过第i条航线时,会受到来自深海舰队的ai点伤害。

tokitsukaze可以在某个休息站点将接下来的战斗切换至夜战模式,这样在她的舰队经过第i条航线时,受到的伤害就变为bi,不过一旦切换到夜战模式就不能再次切换回来,所以她必须考虑清楚在哪里切换。

现在有个限时活动。活动难度分为1,2,3,4,…n,在难度1下,tokitsukaze可以在任意站点切换到夜战模式,而在难度2下,不能在站点1切换到夜战模式,在难度3下,不能在站点1,2切换模式…以此类推,即在难度k下,tokitsukaze不能在站点1,2,3,4,5…k-1切换模式。同时,活动还要求在游戏结束时必须处于夜战模式。

现在tokitsukaze的舰队从s点出发,要前往深海大本营所在的t点。请你告诉她,在难度为1,2,3,4,5…n时,她的舰队结束游戏时受到的最小伤害。
输入描述:
第一行包括2个正整数n,m,(2≤n≤105,1≤m≤min(n*(n-1)/2,105))。
接下来m行,每行包括4个正整数u,v,a,b,(1≤u,v≤n,1≤a,b≤10^9)。
最后一行包括2个正整数s,t,(1≤s,t≤n)。
输出描述:
请你告诉tokitsukaze,在难度为1,2,3,4,5…n时,她的舰队处于夜战模式结束游戏受到的最小伤害。
示例1
输入
复制
4 3
1 4 1 30
1 2 1 10
1 3 20 1
2 3
输出
复制
2
11
21
33
说明
活动难度为1时,在编号为1的点切换模式,受到的最小伤害为2。
活动难度为2时,在编号为2的点切换模式,受到的最小伤害为11。
活动难度为3时,在编号为3的点切换模式,受到的最小伤害为21。
活动难度为4时,在编号为4的点切换模式,受到的最小伤害为33。
示例2
输入
复制
4 3
1 4 30 1
1 2 10 1
1 3 20 1
3 1
输出
复制
1
1
1
51
说明
活动难度为1时,在编号为3的点切换模式,受到的最小伤害为1。
活动难度为2时,在编号为3的点切换模式,受到的最小伤害为1。
活动难度为3时,在编号为3的点切换模式,受到的最小伤害为1。
活动难度为4时,在编号为4的点切换模式,受到的最小伤害为51。路线是3-1-4-1。因为必须处于夜战模式结束游戏,所以在到达1后还要拐去4去切换模式。

模拟这个实际,假如在k号点切换,那么这个距离是先走到k号点,,用id为1的边长,再走到n号点,用id为2的边长,所以我们可以跑两遍最短路,先从起点跑最短路用1号点,再从n号点跑最短路用编号为2的点,然后我们就可以求出在每个点切换的最短距离,又因为等级低的可以用等级高的所有的转化,所以我们再从大到小取最小值即可

#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 + 100;
const int M = 2 * 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[N], w[3][N], ne[N], idx;
int n, m;
int dist1[N], dist2[N];
int ans[N];
int s, t;
bool st[N];

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

void dijkstra(int dist[], int id){
	//memset(dist, 0x3f, sizeof dist);
	//cout << dist[1] << "---"<< endl;
	dist[s] = 0;
	priority_queue<PII, vector<PII>, greater<PII>> heap;
	heap.push({0, s});
	
	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 << dist[j] << " "<< dist[ver]  << w[id][i] << endl;
			if (dist[j] > dist[ver] + w[id][i]){
				dist[j] = dist[ver] + w[id][i];
				heap.push({dist[j], j});
			}
		}
	}
}

signed main(){
	ios;
	cin >> n >> m;
	memset(h, -1, sizeof h);
	for (int i = 1; i <= m; i ++){
		int a, b, w1, w2;
		cin >> a >> b >> w1 >> w2;
		add(a, b, w1, w2), add(b, a, w1, w2);
	}
	
	cin >> s >> t;
	memset(dist1, 0x3f, sizeof dist1);
	memset(dist2, 0x3f, sizeof dist2);
	dijkstra(dist1, 0);
//	cout << dist1[1] << "--" << endl;
	memset(st , 0, sizeof st);
	swap(s, t);
	dijkstra(dist2, 1);
	
	for (int i = 1; i <= n; i ++){
		ans[i] = dist1[i] + dist2[i];
	}
	
	for (int i = n - 1; i >= 1; i --){
		ans[i] = min(ans[i], ans[i + 1]);
	}
	
	for (int i = 1; i <= n; i ++){
		cout << ans[i] << endl;
	}
	
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值