Codeforces Round 918 (Div. 4)(AK)

A、模拟

B、模拟

C、模拟

D、模拟

E、思维,前缀和

F、思维、逆序对

G、最短路

A - Odd One Out 

    题意:给定三个数字,有两个相同,输出那个不同的数字。

    直接傻瓜写法

void solve() 
{
	int a , b , c;
	cin >> a >> b >> c;
	if(a == b){
		cout << c << endl;
	}	
	else if(a == c){
		cout << b << endl;
	}
	else
		cout << a << endl;
}    

 B - Not Quite Latin Square

        题意:给定一个3*3的矩阵,每一行每一列都有且仅有A、B、C三个字母组成。现在给出矩阵中有一个? , 求这个?代表哪个字母。

        可以用二进制表示三个字母是否存在

void solve() 
{
	string s[3];
	for(int i = 0 ; i < 3; i ++)
		cin >> s[i];
	for(int i = 0 ; i < 3 ;i ++){
		int mask = 0;
		for(int j = 0 ; j < 3 ; j ++){
			mask += (1 << (s[i][j] - 'A')) * (s[i][j] != '?');
		}
		if(mask != 7){
			for(int j = 0 ; j < 3 ; j ++){
				if((mask >> j) & 1){
					continue; 
				}
				else{
					char c = j + 'A';
					cout << c <

C - Can I Square? 

        题意:给定一个数组,求数组之和能否形成完全平方数。

        注意:直接用sqrt会因为精度问题而出错,所以在[sqrt - 2 , sqrt + 2]之间都试一遍即可。

        

void solve() 
{
	LL sum = 0;
	cin >> n;
	for(int i = 0 ; i < n ; i ++){
		int x;
		cin >> x;
		sum += x;
	}	
	LL t = sqrt(sum);
	for(LL i = t - 1 ; i <= t + 1 ; i ++){
		if(i < 0)
			continue;
		if(i * i == sum){
			cout <<"YES\n";
			return;
		}
	}
	cout <<"NO\n";
}    

 D - Unnatural Language Processing 

        题意:Codeforces Round 918 (Div. 4)(AK)_第1张图片

        思路:将a、e看成0,b、c、d看成1。整个单词变成了一个01串,然后发现:当连续的两个1出现时,前一个1需要放到前面的音节结尾。当只有一个连续的1,那么这个1就是音节的开头。然后模拟整个过程就行。

        

// Problem: D. Unnatural Language Processing
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/contest/1915/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pairpl;
priority_queue, greater >mi;//小根堆
priority_queue ma;//大根堆
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
vectora(N , 0);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
//10 101
void solve() 
{
	cin >> n;
	string s;
	cin >> s;
	for(int i = 0 ; i < n ; i ++){
		if(s[i] == 'a' || s[i] == 'e'){
			a[i] = 0;
		}
		else{
			a[i] = 1;
		}
	}
	for(int i = 0 ; i < n ; i ++){
		if(a[i] == 1){
			cout << s[i];
		}
		else if(a[i] == 0){
			if(i < n - 3){
				if(a[i + 1] == 1 && a[i + 2] == 1){
					cout << s[i] << s[i + 1] <<"."; 
					i++;
				}
				else{
					cout << s[i] <<".";
				}
			}
			else if(i == n - 3){
				cout << s[i] <<".";
			}
			else if(i == n - 2){
				cout << s[i] << s[i + 1];
				i++;
			}
			else{
				cout << s[i];
			}
		}
	}
	cout << endl;
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

E - Romantic Glasses 

        题意:给定一数组,求其中是否存在某个连续子序列是否满足\sum _{i = l}^{r}a[i](i\ mod\ 2==0) = \sum _{i = l}^{r}a[i](i\ mod\ 2 == 1)

        思路:转移之后有公式\sum _{i = l}^{r}a[i](i\ mod\ 2==0) + \sum _{i = l}^{r}-a[i](i\ mod\ 2 == 1) = 0,也就是对于原数组的奇数项都乘 -1 之后,求是否存在某个区间之和为0。

        用前缀和sum数组来表示区间,也就是存在sum(r) - sum(l) = 0 , 也就是sum(r) = sum(l)。因此我们可以逐步递增 r,然后看之前是否出现过sum(l)sum(r)相等。可用map或者set来存之前出现过的前缀和情况。这样整个复杂度为O(NlogN)

        

// Problem: E. Romantic Glasses
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/contest/1915/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
#define int long long
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pairpl;
priority_queue, greater >mi;//小根堆
priority_queue ma;//大根堆
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
vectora(N , 0);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
void solve() 
{
	cin >> n;
	for(int i = 0 ; i < n ; i ++){
		cin >> a[i];
		if(i % 2 == 1){
			a[i] *= -1;
		}
	}	
	setpre;
	pre.insert(0);
	int sum = 0;
	for(int i = 0 ; i < n ; i ++){
		sum += a[i];
		//cout << sum << endl;
		if(pre.count(sum)){
			cout <<"YES\n";
			return;
		}
		pre.insert(sum);
	}
	cout <<"NO\n";
	return;
}            
signed main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

 F - Greetings 

        题意:

        思路:将所有人的起点按照从小到大进行排序,这样就满足了后面的人不会撞到前面的人(只存在后面的人已经到达终点了,然后被前面的人撞)。然后再考虑能够撞多少个人:对于排完序以后的第i个人而言,他能撞到的人是i以后的,终点小于等于b_{i}的人。因此也就是(b_{i} \geq b_{j})(i < j)的个数,也就是按照起点排完序之后的b数组的逆序对数量,然后套一遍逆序对的板子即可。

// Problem: F. Greetings
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/contest/1915/problem/F
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pairpl;
priority_queue, greater >mi;//小根堆
priority_queue ma;//大根堆
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
vectora(N , 0);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
int tmp[N];
LL merge_sort(int q[], int l, int r)
{
    if (l >= r) return 0;
    
    int mid = (l + r) >> 1; // 二分区间
    
    LL res = merge_sort(q, l, mid) + merge_sort(q, mid + 1, r);
    //归并
    int i = l, j = mid + 1, k = 0;
    
    while (i <= mid && j <= r)
    {
        if (q[i] <= q[j]) tmp[k ++] = q[i ++]; // 前面的排序正常,注意`=` 说明不是逆序对
        else
        {
            res += mid - i + 1;
            tmp[k ++] = q[j ++];
        }
    }
    // 扫尾工作
    while (i <= mid) tmp[k ++] = q[i ++];
    while (j <= r) tmp[k ++] = q[j ++];
    
    for (int i = l, j = 0; i <= r; i ++ , j ++) q[i] = tmp[j];
    
    return res;
}
void solve() 
{
	cin >> n;
	pairpo[n];
	for(int i = 0 ; i < n ; i++){
		cin >> po[i].x >> po[i].y;
	}	
	sort(po , po + n);
	int a[n];
	for(int i = 0 ; i < n ; i ++){
		a[i] = po[i].y;
	}
	LL ans = merge_sort(a , 0 , n  - 1);
	cout << ans <>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

 G - Bicycles 

        题意:Codeforces Round 918 (Div. 4)(AK)_第2张图片

       Codeforces Round 918 (Div. 4)(AK)_第3张图片

        思路:观察到数据不大。因此直接考虑最短路算法。需要注意的是,整个过程不仅仅有点这一个条件,还有自行车速度系数这个限制。因此需要将这两个限制都表示出来,具体看代码注释

// Problem: G. Bicycles
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/contest/1915/problem/G
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
#define int long long
const LL maxn = 4e05+7;
const LL N = 1010;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pairpl;
priority_queue, greater >mi;//小根堆
priority_queue ma;//大根堆
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
vectora(N , 0);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
struct node{
	int num;
	int dis;
	bool operator > (const node &t) const
	{
		return dis > t.dis;
	}
	int own;
}tmp;
int dis[N][N];//到达i点,且拥有自行车系数j的最短距离
int vis[N][N];//到达i点,且拥有自行车系数j的可能性
vectortr[N];
int cost[N];
void dij(int s)
{
	for(int i = 1 ; i <= n ; i ++){
		for(int j = 0 ; j <= 1000 ; j ++){
			dis[i][j] = llinf;
		}
	}
	priority_queue , greater > q;
	q.push({s , 0 , cost[1]});
	dis[s][cost[1]] = 0;
	while(!q.empty())
	{
		tmp = q.top();
		int x = tmp.num;//所在地
		int y = tmp.own;//拥有的自行车系数
		q.pop();
		if(vis[x][y] == 1)
			continue;
		vis[x][y] = 1;
		for(int i = 0 ; i < (int)tr[x].size() ; i ++ )
		{
			node now = tr[x][i];
			int len = tr[x][i].dis;//距离
			int e = tr[x][i].num;//目标地
			int pp = min(y , cost[e]);//到达目的地之后所拥有的自行车系数
			if(dis[e][pp] > dis[x][y] + len * y)
			{
				dis[e][pp] = dis[x][y] + len * y;
				q.push({e , dis[e][pp] , pp});
			}
		}
	}
}
void solve() 
{
	cin >> n >> m;
	for(int i = 1 ; i <= n ; i ++){
		tr[i].clear();
		for(int j = 0 ; j <= 1000 ; j ++){
			vis[i][j] = 0;
		}
	}
	for(int i = 0 ; i < m ; i ++){
		int u , v , dis;
		cin >> u >> v >> dis;
		tr[u].pb({v , dis , 0});
		tr[v].pb({u , dis , 0});
	}
	for(int i = 1 ; i <= n ; i ++){
		cin >> cost[i];
	}
	dij(1);
	int ans = llinf;
	for(int i = 0 ;i <= 1000 ;  i++){
		ans = min(ans , dis[n][i]);
	}
	cout << ans << endl;
}            
signed main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

        

        

你可能感兴趣的:(Codeforces,c++,算法,数据结构)