codeforces 1238
A.
题意:给你两个数,问你两个数的差能否用素数的倍数表示
思路:我现在来说,A题的思路就千万不要先想算法,就直接想答案怎么得到,或者说最有可能的答案是什么,因为A题都是很简单的题目,用一些简单的判断就能得到,所以我们甚至可以先去猜测怎么做,对于这题,样例中也给出了,三个大于1的差数大小参差不齐的例子都是YES,而1是肯定不行的,那我当时直接想去试一下,只有1可以对不对,期间也用大脑想想可行性怎么样,觉得还可以,就试了试,也就ac了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int main()
{int k,c;long long x, y;cin>>k;while (k--){cin>>x>>y;if (x - y == 1)printf("NO\n");elseprintf("YES\n");}return 0;
}
B.
题意:在1往后,有n个怪物分布在一些位置,每次扔炸弹可以把仍炸弹的当前位置之前的怪物向前炸r个单位,炸弹爆炸位置直接炸死怪物。
思路:很明显要从最后一个怪物开始炸,就看炸多少次可以,B题一般不难,就看怎么用的方法比较简单,我一开始用的优先队列,实例对了,但是提交超时,就还是去用普通的数组处理去了。
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
int a[100005];
int main(){int n,r;int t,i,j;priority_queue <int,vector<int>,less<int> >q;cin>>t;while(t--){int move=0;cin>>n>>r;for(i=1;i<=n;i++) cin>>a[i];sort(a+1,a+1+n);for(i=1;i<=n;i++){bool exist;if(q.empty()) exist=0;else exist=q.top()==a[i];if(!exist) q.push(a[i]);}while(1){q.pop();move++;if(q.top()<=move*r) break;}while(!q.empty()) q.pop();cout<<move<<endl;}
}
ac:
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
long long weizhi[100010],v[100010];
int main()
{ long long t,n,r,tip=1,p,ans=0;cin>>t;while(t--){tip=1;ans=0;cin>>n>>r;for(long long i=1;i<=n;i++){cin>>p;if(v[p]==0) {v[p]=1;weizhi[tip]=p;tip++; }}sort(weizhi+1,weizhi+tip);tip-=1;for(long long i=1;i<=tip;i++)if(weizhi[tip-i]<=r*i){ans=i;break; } cout<<ans<<endl;for(int i=1;i<=n;i++)v[weizhi[i]]=0;}return 0;
}
C
题意:从高度n开始向下跳,一开始有m个板子露出,每次用魔法操作可以将当前板子抽回,下面板子抽出。
思路:就看有多少个空隙就行了,每个空隙用一个魔法就能处理,值得注意的就是,如果紧挨着的板子打开了,就要直接跳过下一个板子
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=200005;
ll p[N];
int main()
{int t;cin>>t;while(t--){ll h,n;cin>>h>>n;for(int i=1;i<=n;i++)cin>>p[i];ll cnt=0;p[n+1]=0;for(int i=2;i<=n;i++){if(p[i]-p[i+1]>1)cnt++;else i++;}cout<<cnt<<endl;}return 0;
}
D
题意:给你一个只包含AB的字符串,问你有多少个回文子字符串。
思路:比赛的时候没想出来该怎么写,子符串算法还都不太会写,然后看到大佬用排除法确实可以,毕竟字符串只含有AB非回文串只有x*n y 或 x y*n而查找这个的算法就很简单了,用总数减去这些非回文子串,不过AB这个情况会重复计算,要回加一遍,所以一共要遍历三遍
#include<bits/stdc++.h>
using namespace std;
#define ll long long
{ll n;cin>>n;string s;cin>>s;ll ans=n*(n-1)/2,cnt=1;for(int i=1;i<n;i++){if(a[i]==a[i-1]) cnt++;else ans-=cnt,cnt=1;cnt=1;for(int i=n-2;i>=0;i--){if(s[i]==s[i+1]) cnt++;else ans-=cnt,cnt=1;}for(int i=1;i<n;i++)if(s[i]!=s[i-1])ans++;cout<<ans<<endl;return 0;
}
E
题意:给你一串字母字符串,给一个整数m表示前m个字母,求打出字符串要移动手指多少个单位,假设要打abca 而m序列为abc
那么打第一个a不用移动,打b时要从a移动到b移动一格,第三个b到c也是一个,再到a就要移动两下,所以答案是0+1+1+2=4,你要做到是找到最好的m序列,让答案最下并且输出答案
思路:我们只需要考虑贡献就行了,对于新加入的字母,它与已加入的所有数字的距离都会加一,与未加入的所有的数字都会减一,
也可以认为,对于所有已加入的数字和未加入的数字,他们的距离增加了一
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;string s;
int a[20][20];
ll dp[1048576];
int main(){cin>>n>>m>>s;for(int i=1;i<n;i++){a[s[i]-'a'][s[i-1]-'a']++;a[s[i-1]-'a'][s[i]-'a']++;}for(int i=0;i<(1<<m);i++)dp[i]=1e18;dp[0]=0;for(int i=0;i<(1<<m);i++){vector<int> d,f;for(int j=0;j<m;j++)if(i>>j&1)d.push_back(j);else f.push_back(j);ll tmp=0;for(int k=0;k<d.size();k++){for(int g=0;g<f.size();g++)tmp+=a[d[k]][f[g]];}for(int j=0;j<m;j++){if(i>>j&1) continue;dp[i|1<<j]=min(dp[i|1<<j],dp[i]+tmp);}}cout<<dp[(1<<m)-1]<<endl;
}
codeforces 1238相关推荐
- CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...
- 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)
题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...
- 【codeforces 812C】Sagheer and Nubian Market
[题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...
- CodeForces 获得数据
针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...
- codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...
题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...
- CodeForces 595A
题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...
- codeforces A. Jeff and Digits 解题报告
题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...
- Codeforces Round #506 (Div. 3)
Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...
- Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)
Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...
最新文章
- You must restart adb and Eclipse问题的解决
- 轻量级NLP工具开源,中文处理更精准,超越斯坦福Stanza
- .Net异步编程知多少
- WPF仿网易云音乐系列(一、左侧菜单栏:Expander+RadioButton)
- javascript-DOM-节点
- 面试:输出循环小数的循环节
- android动画源码合集、动态主题框架、社交app源码等
- 使用Setup Factory安装包制作工具制作安装包
- 51单片机定时器流水灯控制
- 破解win7开机密码!
- 走好职场每一步:关于求职技巧、跳槽迷思、职场困惑
- Lattice PCIe 学习 1
- Elasticsearch:运用 Elasticsearch 查找类似文档:more_like_this
- Pandas里面的加权平均,我猜你不会用!
- 嘿,算法,哪里跑|你知道“时间复杂度”吗|算法初识
- ABTest流量分发和业界的一些经验
- Unity实现推箱子
- 研究生英语期末复习(Unit1)
- 为什么广告技术发展了,成本却没降
- 数据中心等级T1,T2,T3,T4,T5区别,科普数据中心等级知识