URAL
输入x,y,c, 找到任意一对a,b 使得a+b==c&& 0<=a<=x && 0<=b<=y
注意后两个条件,顺序搞错wa几次
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
int x,y,c;
while(~scanf("%d%d%d",&x,&y,&c)){
if(x+y<c){
puts("Impossible");
continue;
}
int a,b;
bool s=false;
if(x>y){
s=true;
swap(x,y);
}
if(x>=c){
a=c;
b=;
}
else{
a=x;
b=c-x;
}
if(s) swap(a,b);
printf("%d %d\n",a,b);
}
return ;
}
无向图,边权都是1,人从s到f,走最短路,最短路可能有多条。强盗在r,强盗要抢劫的距离是从r到人走的路径的最近距离。因为可能有多条最短路,所以要求强盗到达每条最短路到最近距离,输出其中最大的。
做法 预处理出s到每个点到距离,r到每个点到距离。然后从s开始bfs,step记录的是步数,val记录的是这条路径上距离r的最近距离。
#include<cstdio>
#include<cstring>
#include<queue>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M=1e5+;
struct G {
struct E {
int v,next;
} e[M<<];
int le,head[M];
void init() {
le=;
mt(head,-);
}
void add(int u,int v) {
e[le].v=v;
e[le].next=head[u];
head[u]=le++;
}
} g;
bool vis[M];
void bfs(int s,int d[]) {
mt(vis,);
vis[s]=true;
queue<int> q;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty()) {
int u=q.front();
q.pop();
for(int i=g.head[u]; ~i; i=g.e[i].next) {
int v=g.e[i].v;
if(!vis[v]) {
vis[v]=true;
d[v]=d[u]+;
q.push(v);
}
}
}
}
int sd[M],rd[M],d[M],ans;
struct Q {
int step,val,id;
} now,pre;
queue<Q> q;
void solve(int s,int f) {
ans=;
mt(d,);
now.step=;
now.id=s;
now.val=rd[s];
while(!q.empty()) q.pop();
q.push(now);
while(!q.empty()) {
pre=q.front();
q.pop();
if(pre.id==f) {
ans=max(ans,pre.val);
continue;
}
int u=pre.id;
for(int i=g.head[u]; ~i; i=g.e[i].next) {
int v=g.e[i].v;
now.val=min(pre.val,rd[v]);
now.step=pre.step+;
if(now.step==sd[v]&&d[v]<now.val) {
d[v]=now.val;
now.id=v;
q.push(now);
}
}
}
}
int main() {
int n,m,u,v,s,f,r;
while(~scanf("%d%d",&n,&m)) {
g.init();
while(m--) {
scanf("%d%d",&u,&v);
g.add(u,v);
g.add(v,u);
}
scanf("%d%d%d",&s,&f,&r);
bfs(s,sd);
bfs(r,rd);
solve(s,f);
printf("%d\n",ans);
}
return ;
}
vector存图会慢一些
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M=1e5+;
vector<int> g[M];
bool vis[M];
void bfs(int s,int d[]) {
mt(vis,);
vis[s]=true;
queue<int> q;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty()) {
int u=q.front();
q.pop();
int len=g[u].size();
for(int i=; i<len; i++) {
int v=g[u][i];
if(!vis[v]) {
vis[v]=true;
d[v]=d[u]+;
q.push(v);
}
}
}
}
int sd[M],rd[M],d[M],ans;
struct Q {
int step,val,id;
} now,pre;
queue<Q> q;
void solve(int s,int f) {
ans=;
mt(d,);
now.step=;
now.id=s;
now.val=rd[s];
while(!q.empty()) q.pop();
q.push(now);
while(!q.empty()) {
pre=q.front();
q.pop();
if(pre.id==f) {
ans=max(ans,pre.val);
continue;
}
int u=pre.id;
int len=g[u].size();
for(int i=; i<len; i++) {
int v=g[u][i];
now.val=min(pre.val,rd[v]);
now.step=pre.step+;
if(now.step==sd[v]&&d[v]<now.val) {
d[v]=now.val;
now.id=v;
q.push(now);
}
}
}
}
int main() {
int n,m,u,v,s,f,r;
while(~scanf("%d%d",&n,&m)) {
for(int i=;i<=n;i++) g[i].clear();
while(m--) {
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
scanf("%d%d%d",&s,&f,&r);
bfs(s,sd);
bfs(r,rd);
solve(s,f);
printf("%d\n",ans);
}
return ;
}
手机排名按照出现次数,次数相同按照最低价格
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
string a,b;
map<string,int> num,val;
map<string,int>::iterator it;
struct G{
string a;
int num,val;
friend bool operator <(const G &a,const G &b){
return a.num>b.num||(a.num==b.num&&a.val<b.val);
}
}now;
vector<G> g;
int cost;
int main(){
num.clear();
val.clear();
for(int i=;i<;i++){
cin>>a>>b>>cost;
num[b]++;
if(val[b]){
val[b]=min(val[b],cost);
}
else{
val[b]=cost;
}
}
g.clear();
for(it=num.begin();it!=num.end();it++){
now.a=it->first;
now.num=it->second;
now.val=val[now.a];
g.push_back(now);
}
sort(g.begin(),g.end());
cout<<g[].a<<endl;
return ;
}
最多4个连续的能满足的 88 89 90 91
#include<cstdio>
int main(){
int n,ans[]={,,,};
while(~scanf("%d",&n)){
if(n<){
for(int i=;i<n;i++){
printf("%02d ",ans[i]);
}
}
else{
printf("Glupenky Pierre");
}
puts("");
}
return ;
}
汗诺塔,一开始都在A ,要移动成输入的状态,需要几步,每次都是考虑最下面的一个,然后相当于把n-1个移开,然后把第n个移动到目标处,移动n个需要2^n-1,把最后一个放到目标需要1
#include<cstdio>
typedef long long LL;
const int M=;
char a[M];
LL p[M];
int main(){
p[]=;
for(int i=;i<M;i++){
p[i]=p[i-]*;
}
int n;
while(~scanf("%d%s",&n,a)){
LL ans=;
int id=;
for(int i=n;i>=;i--){
int his=a[i-]-'A';
if(his==id) continue;
ans+=p[i-];
id=^^^id^his;
}
printf("%lld\n",ans);
}
return ;
}
n人分k队,不同队之间的人要打一场比赛,问最多能打几场,把人平均分k份打得最多
#include<cstdio>
int main(){
int t,n,k,ans;
while(~scanf("%d",&t)){
while(t--){
scanf("%d%d",&n,&k);
int x=n/k;
int y=n%k;
ans=y*(x+)*(n-x-)+(k-y)*x*(n-x);
printf("%d\n",ans/);
}
}
return ;
}
最多存在k种不同字母,问最多几个,有几种方法达到最多
最多几个就是把前k种加起来
方法数就是有几种个数和第k种一样,那么从中选几种
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M=1e5+;
char a[M];
int num[];
int C[][];
int main(){
for(int i=;i<;i++){
C[i][i]=;
C[i][]=;
}
for(int i=;i<;i++){
for(int j=;j<i;j++){
C[i][j]=C[i-][j]+C[i-][j-];
}
}
int k;
while(~scanf("%s%d",a,&k)){
mt(num,);
for(int i=;a[i];i++){
num[a[i]-'a']++;
}
sort(num,num+);
int sum=;
int kk=;
for(int i=;i>=;i--){
sum+=num[i];
kk++;
if(kk==k) break;
}
printf("%d ",sum);
if(k==){
puts("");
continue;
}
if(num[-k]==){
puts("");
continue;
}
if(num[-k-]!=num[-k]){
puts("");
continue;
}
int cn=,cm=;
for(int i=;i<;i++){
if(num[i]==num[-k]){
cn++;
}
}
for(int i=-k;i<;i++){
if(num[i]==num[-k]){
cm++;
}
}
printf("%d\n",C[cn][cm]);
}
return ;
}
end
URAL的更多相关文章
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- ural 2069. Hard Rock
2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...
- ural 2068. Game of Nuts
2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...
- ural 2067. Friends and Berries
2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...
- ural 2066. Simple Expression
2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...
- ural 2065. Different Sums
2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...
- ural 2064. Caterpillars
2064. Caterpillars Time limit: 3.0 secondMemory limit: 64 MB Young gardener didn’t visit his garden ...
随机推荐
- PHP file_get_contents于curl性能效率比较
说明大部分内容整理来源于网络,期待你的补充.及不当之处的纠正: 1)fopen/file_get_contents 每次请求远程URL中的数据都会重新做DNS查询,并不对DNS信息进行缓存.但是CUR ...
- psd图片到html
正确的做法是:拿到psd后,先不要做别的,直接在文本编辑器中将网页的框架写出来,不要假设这块将来css要去怎么渲染,完全自然化的标签,不加任何的css.写完之后在各个浏览器运行之后确保大体定位都没有问 ...
- DevExpress BarManager 部分用法
1.创建一个BarManager会默认产生三个菜单.BarManager右键ShowDesignTime enhancements会显示[add]按钮,可对菜单进行编辑. 2.其中比较有用的属性: 选 ...
- MapReduce实现的Join
MapReduce Join 对两份数据data1和data2进行关键词连接是一个很通用的问题,如果数据量比较小,可以在内存中完成连接. 如果数据量比较大,在内存进行连接操会发生OOM.mapredu ...
- 十天学会单片机Day3 D/A与A/D转换器
D/A转换器 1.二进制权电阻网络型D/A转换器 基准电压Vref 数据D(d3d2d1d0) 输出模拟电压V0 i0 = Vref/8R i1 = Vref/4R i2 = Vref/ ...
- Linux获取用户主目录
#!/usr/bin/python# -*- coding:utf-8 -*-import sysimport osclass get_home_path(object): def __init__( ...
- salt-ssh安装及简单使用
需要 salt-master 0.17以上版本支持 1.安装 相关依赖包可查看requirements.txt Jinja2 M2Crypto msgpack-python pycrypto PyYA ...
- java-脚本-编译-注解
有注解没注解生成字节码一样 ,只对处理它的工具有用通过注解接口定义@interface 元注解(4个)@Target ANNOTATION_TYPE/PACKAGE/TYPE/METHOD/CONST ...
- 关于activity_main.xml与fragment_main.xml
第一种解决办法 新版安装SDK文件一开始有两个XML文件,activity_main.xml和fragment_main.xml,不习惯的可以这样处理:1.删除fragment_main.xml整个文 ...
- springMVC+jpa配置之简单案例
搭建springMVC+jpa的亲身经历,看着网上的博客,自己摸索着搭建框架结果错误一大堆.现在把流程走一遍,方便以后查看. 其中我遇到这样的一个问题:直接启动tomcat运行保存实体能通过,但是通过 ...