Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.
Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following nlines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi,yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yipackets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server b in the similar format.
2 1 5 5 2 6 4
LIVE LIVE
3 1 0 10 2 0 10 1 10 0
LIVE DEAD
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
统计第一类的是否大于等于第二类的,输出LIVE;否则输出 DEAD
#include <stdio.h>
#include <string.h>
int main()
{
int a[2][2];
memset(a,0,sizeof(a));
int n;
scanf("%d",&n);
while(n--)
{
int t,b,c;
scanf("%d%d%d",&t,&b,&c);
if(t==1)
a[0][0]+=b,a[0][1]+=c;
if(t==2)
a[1][0]+=b,a[1][1]+=c;
}
if(a[0][0]>=a[0][1])
printf("LIVE\n");
else
printf("DEAD\n");
if(a[1][0]>=a[1][1])
printf("LIVE\n");
else
printf("DEAD\n");
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
gets(s);
int l=strlen(s);
int tag,i;
if(s[0]=='h')
{
printf("http://");
tag=4;
}
else if(s[0]=='f')
{
printf("ftp://");
tag=3;
}
for(i=tag;i<l;i++)
{
if(i!=tag&&s[i]=='r'&&s[i+1]=='u')
break;
printf("%c",s[i]);
}
printf(".ru");
if(i+2!=l)
printf("/%c",s[i+2]);
for(int j=i+3;j<l;j++)
{
printf("%c",s[j]);
}
printf("\n");
return 0;
}
Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has ai coins.
Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer x (2·x + 1 ≤ n) and take a coin from each chest with numbers x, 2·x, 2·x + 1. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.
Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves.
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of chests with coins. The second line contains a sequence of space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai is the number of coins in the chest number i at the beginning of the game.
Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.
1 1
-1
3 1 2 3
3
In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.
In the second sample there is only one possible move x = 1. This move should be repeated at least 3 times to empty the third chest.
贪心,首先必须是大于2的奇数,然后从倒着贪心
#include <stdio.h>
#define max(a,b) (a>b?a:b)
int main()
{
int n;
int a[110];
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
if(n<=2||n%2==0)
printf("-1\n");
else
{
int step=0;
for(int i=n;i>=1;i--)
{
if(2*i+1<=n)
{
int sb=max(a[i*2+1],a[i*2]);
step+=sb;
a[2*i]=a[2*i+1]=0;
a[i]-=sb;
a[i]=(a[i]>0?a[i]:0);
}
}
step+=a[1];
a[1]=0;
for(int i=1;i<=n;i++)
{
if(a[i])
step--;
}
printf("%d\n",step);
}
return 0;
}
所以逆过去求就是将一排所有的全部进行 | 的位运算,就还原了
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#define N 101
int a[N][N];
int b[N];
int main()
{
int n;
scanf("%d",&n);
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
if(i!=j)
b[i]|=a[i][j];
}
}
for(int i=1;i<n;i++)
printf("%d ",b[i]);
printf("%d\n",b[n]);
return 0;
}
模拟人的进出,求某时刻人最多的时候
#include <stdio.h>
#include <string.h>
int main()
{
char str[400];
gets(str);
int l=strlen(str);
int s=0,y=0,tag=0;
for(int i=0;i<l;i++)
{
if(str[i]=='+')
{
if(s>0)
s--;
else
tag++;
y++;
}
else if(str[i]=='-')
{
if(y>0)
y--;
else
tag++;
s++;
}
}
printf("%d\n",tag);
return 0;
}
模拟题,求某时与下一事件的差值大于等于n的时候,这个题主要是时间上的控制问题,每个月啊天数不同什么的,看别人代码过的,把时间入队...
给的样例,第一组还没输完,就结束了;而第二组输出用ctrl+z结束。。。哈哈。。郁闷了好久
#include <iostream>
#include <cstdio>
#include <queue>
#include <string>
#define maxn 10000010
using namespace std;
int month[13]={0,31,29,31,30,31,30,31,31,30,31,30};
queue<long long>q;
int n,m;
char s[maxn];
int main()
{
scanf("%d%d",&n,&m);
while(scanf("%s",s)!=EOF)
{
long long time=0;
int M=(s[5]-'0')*10+(s[6]-'0');
int D=(s[8]-'0')*10+(s[9]-'0');
for(int i=1;i<M;++i)
time+=month[i];
time+=D-1;
time*=24*3600;
int hh,mm,ss;
scanf("%d:%d:%d",&hh,&mm,&ss);
gets(s);
time+=hh*3600+mm*60+ss;
q.push(time);
while(q.front()<=time-n)
q.pop();
if(q.size()>=m)
{
printf("2012-%02d-%02d %02d:%02d:%02d\n",M,D,hh,mm,ss);
return 0;
}
}
printf("-1\n");
return 0;
}
Polycarpus works as a programmer in a start-up social network. His boss gave his a task to develop a mechanism for determining suggested friends. Polycarpus thought much about the task and came to the folowing conclusion.
Let's say that all friendship relationships in a social network are given as m username pairs ai, bi (ai ≠ bi). Each pair ai, bi means that users ai and bi are friends. Friendship is symmetric, that is, if ai is friends with bi, then bi is also friends with ai. User y is a suggested friend for user x, if the following conditions are met:
- x ≠ y;
- x and y aren't friends;
- among all network users who meet the first two conditions, user y has most of all common friends with user x. User z is a common friend of user x and user y (z ≠ x, z ≠ y), if x and z are friends, and y and z are also friends.
Your task is to help Polycarpus to implement a mechanism for determining suggested friends.
The first line contains a single integer m (1 ≤ m ≤ 5000) — the number of pairs of friends in the social network. Next m lines contain pairs of names of the users who are friends with each other. The i-th line contains two space-separated names ai and bi (ai ≠ bi). The users' names are non-empty and consist of at most 20 uppercase and lowercase English letters.
It is guaranteed that each pair of friends occurs only once in the input. For example, the input can't contain x, y and y, x at the same time. It is guaranteed that distinct users have distinct names. It is guaranteed that each social network user has at least one friend. The last thing guarantees that each username occurs at least once in the input.
In the first line print a single integer n — the number of network users. In next n lines print the number of suggested friends for each user. In the i-th line print the name of the user ci and the number of his suggested friends di after a space.
You can print information about the users in any order.
5 Mike Gerald Kate Mike Kate Tank Gerald Tank Gerald David
5 Mike 1 Gerald 1 Kate 1 Tank 1 David 2
4 valera vanya valera edik pasha valera igor valera
5 valera 0 vanya 3 edik 3 pasha 3 igor 3
In the first test case consider user David. Users Mike and Tank have one common friend (Gerald) with David. User Kate has no common friends with David. That's why David's suggested friends are users Mike and Tank.
图论哈.....@黄建大神 交给你了 表示不会做 也不想做
#include <stdio.h>
#include <string.h>
#define maxn 5010
char s[maxn];
int miao[maxn][maxn],tag[maxn][maxn];
int main()
{
scanf("%s",s);
int l=strlen(s);
for(int i=1;i<=l;i++)//i表示回文串的长度
for(int j=0;j<l-i+1;j++)//j表示起始位置
{
miao[i][j]=0;
if(i==1) tag[j][j]=1;
else if(i==2) tag[j][j+i-1]=(s[j]==s[j+i-1]);
else tag[j][j+i-1]=tag[j+1][j+i-2]&&(s[j+i-1]==s[j]);
}
for(int i=0;i<l;i++)
{
miao[i][i]=tag[i][i];
for(int j=i+1;j<l;j++)
miao[i][j]=miao[i][j-1]+tag[i][j];
}
for(int i=0;i<l;i++)
{
miao[i][i]=tag[i][i];
for(int j=i-1;j>=0;j--)
miao[j][i]+=miao[j+1][i];
}
int t;
scanf("%d",&t);
while(t--)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",miao[x-1][y-1]);
}
return 0;
}