2022杭电联赛2补题

目录

1002 C++ to Python(7151)

1007 Snatch Groceries(7156)

1009 ShuanQ(7158)

1012 Luxury cruise ship(7161)

1002 C++ to Python(7151)

Problem Description

Kayzin's repository has lots of code in C++, but Aba aba can only understand Python code. So Aba aba calls you for help.

The only things you should do is using (...) in Python to match std::make_tuple(...) in C++.

Input

First line has one integer T, indicates T test cases. In each case:

First line has string s, indicates C++ code.

The C++ code only contains std::make_tuple, "(", ")", "," and integers, without space.

1≤T≤100, the length of s not exceeds 1000.

Output

Each test cases print one line of Python code, do not print any space.

Sample Input

2 
std::make_tuple(-2,3,3,std::make_tuple(3,3)) 
std::make_tuple(std::make_tuple(1,1,4,5,1,4))

Sample Output

(-2,3,3,(3,3)) ((1,1,4,5,1,4))

#include<bits/stdc++.h>
using namespace std;
int t;
char s[1001];
int main()
{
	scanf("%d",&t);
	while(t--){
		scanf("%s",s);
		for(int i=0;s[i];i++){
			if(s[i]!=':'&&s[i]!='_'&&!isalpha(s[i]))
			    putchar(s[i]);
		}printf("\n");
	}
	return 0;
}

1007 Snatch Groceries(7156)

Problem Description

TL;DR: Spanner implements snapshot isolation across data centers in this way. It uses the clock’s confidence interval as reported by the TrueTime API, and is based on the following observation: if you have two confidence intervals, each consisting of an earliest and latest possible timestamp (A=[Aearliest,Alatest],B=[Bearliest,Blatest]), and those two intervals do not overlap (i.e.,Aearliest<Alatest<Bearliest<Blatest), then B definitely happened after A——there can be no doubt. Only if the intervals overlap are we unsure in which order A and B happened.

Now we use Spanner as a solution, there are millions of people snatching groceries, and everyone is given the clock’s confidence interval. The server executes each request in chronological order, and terminate in case of intervals overlap. Here is the question, how many people can get their food before the server is terminated.

Input

First line has one integer T, indicating there are T test cases. In each case:

First line has one integers n, indicating there are n people.

For next n lines, each line has 2 integers earliest,latest, indicates the clock’s confidence interval.

T≤10,1≤n≤105,0≤earliesti<latesti≤109

Output

In each case, print one integer, indicates the answer.

Sample Input

2
3
1 2
3 4
5 6
3
1 2
2 3
1 5

Sample Output

3

0

#include<iostream>
#include<algorithm>
using namespace std;
int t, n, e, l, ans = 0,x=0,y=0;
struct tim{
    int x,y;
    bool operator<(const tim A) {
        return x<A.x;
    }
};
int main()
{
    scanf("%d", &t);
    for (int i = 1; i <= t; i++) {
        scanf("%d", &n);
        ans = 0;
        tim a[100001];
        for (int j = 1; j <= n; j++) {
            scanf("%d%d", &a[j].x, &a[j].y);
        }sort(a + 1, a + n + 1);
        for (int i = 1; i <= n; i++) {
             if (a[i].y>=a[i + 1].x&&i!=n)
             	break;
            else ans++;
        }printf("%d\n", ans);
    }
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> >a;
int t,n;
int main()
{
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		a.resize(n+1);
		for(int i=0;i<n;i++){
			scanf("%d%d",&a[i].first,&a[i].second);
		}a[n]=make_pair(1234567890,0);
		sort(a.begin(),a.end());
		int ans=0;
		for(int i=0;i<n;i++){
			if(a[i].second>=a[i+1].first)break;
			ans++;
		}printf("%d\n",ans);
	}
	return 0; 
}

反思:1.每个样例需重置a[]

           2.排序后只需比较a[i].y<a[i+1].x,最后一个a[n]需要特判

           3.用vector套pair不用手写比较函数,可直接sort

1009 ShuanQ(7158)

Problem Description

CX is a programmer of a mooc company. A few days ago, he took the blame for leakage of users' data. As a result, he has to develop an encryption algorithm, here is his genius idea.

First, the protocol specifies a prime modulus M, then the server generates a private key P, and sends the client a public key Q. Here Q=P−1,P×Q≡1modM.

Encryption formula: encrypted_data=raw_data×PmodM

Decryption formula: raw_data=encrypted_data×QmodM

It do make sense, however, as a master of number theory, you are going to decrypt it.You have intercepted information about P,Q,encrypted_data, and M keeps unknown. If you can decrypt it, output raw_data, else, say "shuanQ" to CX.

Input

First line has one integer T(T≤20), indicating there are T test cases. In each case:

One line has three integers P,Q,encrypted_data. (1<P,Q,encrypted_data≤2×106)

It's guaranteed that P,Q,encrypted_data<M.

Output

In each case, print an integer raw_data, or a string "shuanQ".

Sample Input

2 
5 5 5 
6 6 6

Sample Output

shuanQ

1

#include<bits/stdc++.h>
using namespace std;
int t,p,q,ed;
typedef long long ll;
ll rd,km,m;
int main()
{
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&p,&q,&ed);
		km=1ll*p*q-1,rd=-1,m=0;
		for(ll i=2;i*i<=km;i++){
			if(km%i)continue;
			while(km%i==0)km/=i;
			m=i;
		}if(km>1)m=km;
		if(p<m&&q<m){
			rd=1ll*ed*q%m;
		    printf("%lld\n",rd);
		}
		else printf("shuanQ\n");
	}
	return 0;
}

反思:1:1ll*格式化

1012 Luxury cruise ship(7161)

Problem Description

Kayzin plans to buy a luxury cruise ship which will cost N coins. But Kayzin didn't have enough coins, so Kayzin decided to start saving coins.

Kayzin will put 7 or 31 or 365 coins into his piggy bank each day. Now Kayzin wants to know at least how many days he will need to "exactly"(No more and no less) scrape together the money for buying a luxury cruise.

If Kayzin can't "exactly" scrape together the money for buying a luxury cruise, print -1.

Input

The first line contains an integer T(T≤1000) . Then T test cases follow.

For one case, the first line contains an integer N (1≤N≤1018) , N denotes the coins that a luxury cruise ship will cost.

Output

Print an integer for each case, indicating the minimum number of days Kayzin needs.

If Kayzin can't "exactly" scrape together the money for buying a luxury cruise, print -1.

Sample Input

5 
14 
38  
55 
403 
996

Sample Output

2 2 -1 3 16

佬的讲解:

2555既是7的倍数,又是365的倍数
对于一个n,我们可以考虑把它分成三部分
一部分由365解决,一部分由31解决,一部分由7解决
显然,尽量用365解决,答案会更优
如果用7解决的部分大于了2555,那我们就完全可以把其中的2555拿过来用365解决对吧
所以,思考一下,是不是n大于2555时,我们只需要考虑每天拿365就行了
这样就得出大规模数据的解法了:
在n大于2555时,我们每天都拿365,直到n小于2555,这时候我们只需要用上面的三重循环枚举
这其实就是题解里面所谓的“n较大时,用365将n缩小到背包的范围内”

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t,b[3]={7,31,365},m=2555;
ll a,ans,Max=1234567890,f[2557];
int main()
{
	//memset(f,1234567890,sizeof(f));//错 
	for(int i=0;i<2557;i++)f[i]=Max;
	f[0]=0;
	for(int i=0;i<3;i++){
		for(int j=0;j+b[i]<=m;j++){
			if(f[j]!=Max)
			    f[j+b[i]]=min(f[j+b[i]],f[j]+1);
		}
	}
	scanf("%d",&t);
	while(t--){
		ans=0;
		scanf("%lld",&a);
		if(a>m){
			ans=(a-m)/365;//向上取整 
			if((a-m)%365)ans++;
			a-=ans*365;
		}if(f[a]!=Max)
		printf("%lld\n",ans+f[a]);
		else printf("-1\n");
	}
	return 0;
}

还找到一种方法:直接算出7*31*365内的值,比起大的缩小范围

2022“杭电杯”中国大学生算法设计超级联赛 (2) 杭电多校第二场_Nebula_xuan的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值