URAL 2035

输入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 ;
}

URAL 2034

无向图,边权都是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 ;
}

URAL 2033

手机排名按照出现次数,次数相同按照最低价格

 #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 ;
}

URAL 2031

最多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 ;
}

URAL 2029

汗诺塔,一开始都在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 ;
}

URAL 2025

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 ;
}

URAL 2024

最多存在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的更多相关文章

  1. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  2. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  3. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  4. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  5. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  6. 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 ...

  7. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  8. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

  9. ural 2065. Different Sums

    2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...

  10. ural 2064. Caterpillars

    2064. Caterpillars Time limit: 3.0 secondMemory limit: 64 MB Young gardener didn’t visit his garden ...

随机推荐

  1. ThinkPHP之中的事务回滚

    小李子 获取thinkphp之中执行的SQL: 1.用调试模式的追踪trace功能: 2.代码: $user_type=D('user_type'); $datass=array('school_id ...

  2. Creating an API-Centric Web Application[转]

    Creating an API-Centric Web Application 转自 http://hub.tutsplus.com/tutorials/creating-an-api-centric ...

  3. onActivityResult无法调用

    最新项目中使用到了Fragment.在fragment中重载了onActivityResult方法,始终无法调用到. 大体是这样:选择图片的功能纠结了很久,能正常发送选择图片,但是选择后无法调用到on ...

  4. Delphi CxGrid 汇总(4)

    1.     CxGrid汇总功能 ① OptionsView-Footer设置为True,显示页脚   ② CxGrid的Summary选项卡定义要汇总的列和字段名及汇总方式,Footer选项卡定义 ...

  5. sliding windows (poj 2823) 题解

    [问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: [样例输入] 8 3 1 3 -1 -3 5 3 6 7 [样例输 ...

  6. 第二十二章 数据访问(In .net4.5) 之 集合

    1. 概述 本章内容包括 .net平台中的集合.如何选择集合 以及 如何实现自定义集合. 2. 主要内容 2.1 使用数组(Array) ]; ; x < arrayOfInt.Length;  ...

  7. 5.html5中的路径表示

    路径在html中的作用主要是进行外部资源的引入,如css文件,js文件,媒体文件等. 而路径本身有分为相对路径和绝对路径.所谓相对路径,就是相对于链接页面而言的另一个页面的路径.而绝对路径,就是直接从 ...

  8. WPF中线性渐变画刷的一个小窍门

    最近被项目里面控件的设计搞的死去活来的,大部分的设计都会需要使用进度条的功能,因为UI形状的变态,使用ProgressBar不能满足需求,没办法就自己想办法实现进度显示.折腾的多了发现一个很不错的方法 ...

  9. C#模糊查询绑定datagridview

    private CollectionViewSource wgdData = new CollectionViewSource(); private DataTable Ds_wgd { get { ...

  10. Ajax-goahead局部刷新页面

    软件开发最常用的方法是:C/S,B/S.如果嵌入式设备中使用Ajax,那么既可以使用C/S方式,也可以使用B/S开发上位机.最近公司的一个项目需要异步获取后台数据,使用form更新数据时会有空白卡顿不 ...