文章目录
- [A. Everyone Loves to Sleep](https://codeforces.com/contest/1714/problem/A)
- [B. Remove Prefix](https://codeforces.com/contest/1714/problem/B)
- [C. Minimum Varied Number](https://codeforces.com/contest/1714/problem/C)
- [D. Color with Occurrences](https://codeforces.com/contest/1714/problem/D)
- [E. Add Modulo 10](https://codeforces.com/contest/1714/problem/E)
A. Everyone Loves to Sleep
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
typedef pair<int,int>PII;
int n,h,w;
void solve() {
cin>>n>>h>>w;
set<PII>s;
for(int i=0;i<n;i++){
int x,y;
cin>>x>>y;
s.insert({x,y});
}
if(s.count({h,w})){
cout<<0<<' '<<0<<endl;
return;
}
int ans=0;
while(1){
ans++;
w++;
if(w==60) h++,w=0;
if(h==24) h=0;
if(s.count({h,w})){
cout<<ans/60<<' '<<ans%60<<endl;
return;
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}
B. Remove Prefix
长度为n的数组a
操作:移除最左边的元素
问最少多少次操作使得所有数不同
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
int n;
void solve() {
cin>>n;
map<int,int>mp;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=n;i>=1;i--){
if(mp[a[i]]){
cout<<i<<endl;
return;
}
mp[a[i]]=1;
}
cout<<0<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}
C. Minimum Varied Number
求各数位之和为s的最小数(所有数位要求均不同)
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int s;
void solve() {
cin>>s;
string tmp;
for(int i=9;i>=1;i--){
if(s>=i){
s-=i;
tmp=(char)(i+'0')+tmp;
}
}
cout<<tmp<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}
D. Color with Occurrences
一个由小写字母组成的文本串和n个字符串
操作:选择一个字符串,文本对应部分涂色
问将文本串全部涂色最少多少次操作,无解输出-1
数据比较小,尝试暴力
贪心,从前往后,哪个延申的长度长就选哪个,如果延申不出去,则无解
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
typedef pair<int,int>PII;
string t;
int n;
string s;
string str;
void solve() {
cin >> t;
int len = t.size();
t+=str;
cin >> n;
map<string, int>mp;
for (int i = 1; i <= n; i++) {
cin >> s;
mp[s] = i ;
}
vector<PII>ans;
int r = 0; //延申到何处
int r_i,pos=0;
for (auto v : mp) { //枚举所有字符串
string tmp;
for (int j = 0; j < (int)v.first.size(); j++) {
tmp += t[j];
}
if (tmp == v.first) {
if(r<=(int)tmp.size()-1) {
r_i=v.second;
}
r = max(r, (int)tmp.size() - 1);
}
}
ans.push_back({r_i,pos+1});
string x;
x+=t[0];
if (r == 0&&!mp[x]) { //延申不出去
cout << -1 << endl;
return;
}
// cout<<'='<<endl;
int l = 1; //起点
// cout<<r<<endl;
while (r != len - 1) {
int tt = r;
int rr=r;
for (int i = l ; i <= r + 1; i++) {
for (auto v : mp) { //枚举所有字符串
string tmp;
for (int j = 0; j < (int)v.first.size(); j++) {
tmp += t[i + j];
}
// cout<<tmp<<endl;
if (tmp == v.first) {
if(rr<i+(int)tmp.size()-1){
r_i=v.second;
pos=i;
}
rr = max(rr, i + (int)tmp.size() - 1);
}
}
}
// cout<<r_i<<' '<<pos+1<<' '<<r<<endl;
l++;
r=rr;
ans.push_back({r_i,pos+1});
if (tt == r && r != len - 1) {
cout << -1 << endl;
return;
}
}
cout<<ans.size()<<endl;
for(auto v:ans) cout<<v.first<<' '<<v.second<<endl;
cout<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
for(int i=0;i<100;i++) str+='1';
cin >> t;
while (t--) {
solve();
}
return 0;
}
E. Add Modulo 10
长度为n的数组a(数[0,1e9])
操作:ai加上ai%10(可以多次对同一下标)
操作次数不限
问是否可以使所有元素相等
每次都只加个位,所以个位上的数字只要看个位即可
0
1->2->4->8->6->2
2->4->8->6->2
3->6->2->4->8->6->2
5->0
除了0和5特殊一些,其它均可以进入到2->4->8->6的循环中,并且从2到2会增加20,比如12->14->18->26->32
trick:
每次都只加个位,所以个位上的数字只要看个位即可
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
int n;
void solve() {
cin>>n;
map<int,int>mp;
for(int i=1;i<=n;i++) cin>>a[i],mp[a[i]%10]++;
if(mp[0]||mp[5]){
for(int i=1;i<=n;i++){
if(a[i]%10!=0&&a[i]%10!=5){
cout<<"No"<<endl;
return;
}
}
for(int i=1;i<=n;i++){
if(a[i]%10==5) a[i]+=5;
}
for(int i=2;i<=n;i++){
if(a[i]!=a[i-1]){
cout<<"No"<<endl;
return;
}
}
cout<<"Yes"<<endl;
return;
}
for(int i=1;i<=n;i++){
while(a[i]%10!=2){
a[i]+=a[i]%10;
}
a[i]%=20;
}
for(int i=2;i<=n;i++){
if(a[i]!=a[i-1]){
cout<<"No"<<endl;
return;
}
}
cout<<"Yes"<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--) {
solve();
}
return 0;
}